c++ implementation of read conf file

(Reason:libconfig error without double quotes)
This commit is contained in:
lakinduakash 2019-04-14 13:35:49 +05:30
parent 49c9aed7e4
commit 583dd49f2f
2 changed files with 57 additions and 0 deletions

37
src/ui/read_config.cpp Normal file
View File

@ -0,0 +1,37 @@
//
// Created by lakinduakash on 14/04/19.
//
#include "read_config.h"
#include <iostream>
#include <fstream>
#include <algorithm>
extern "C" {
int read_config_1() {
// std::ifstream is RAII, i.e. no need to call close
std::ifstream cFile("/home/lakinduakash/wh.config");
if (cFile.is_open()) {
std::string line;
while (getline(cFile, line)) {
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
if (line[0] == '#' || line.empty())
continue;
auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
std::cout << name << " " << value << '\n';
}
} else {
std::cerr << "Couldn't open config file for reading.\n";
}
return 0;
}
}

20
src/ui/read_config.h Normal file
View File

@ -0,0 +1,20 @@
//
// Created by lakinduakash on 14/04/19.
//
#ifndef WIHOTSPOT_READ_CONFIG_H
#define WIHOTSPOT_READ_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
int read_config_1();
#ifdef __cplusplus
}
#endif
#endif //WIHOTSPOT_READ_CONFIG_H