set and return read values from config file

This commit is contained in:
lakinduakash 2019-04-14 15:36:12 +05:30
parent 24862a9798
commit 84a2828f97
2 changed files with 37 additions and 1 deletions

View File

@ -7,18 +7,28 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include "read_config.h"
#include "h_prop.h"
#define CONFIG_KEY_COUNT 30
#define STRING_MAX_LENGTH 100
extern "C" {
ConfigValues configValues;
char configs[CONFIG_KEY_COUNT][STRING_MAX_LENGTH];
int read_config_file() {
// std::ifstream is RAII, i.e. no need to call close
std::ifstream cFile(CONFIG_FILE);
if (cFile.is_open()) {
std::string line;
int i=0;
while (getline(cFile, line)) {
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
@ -27,7 +37,11 @@ int read_config_file() {
auto delimiterPos = line.find('=');
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
strcpy(configs[i],value.c_str());
setConfigValues(name.c_str(),configs[i]);
std::cout << name << " " << value << '\n';
++i;
}
} else {
@ -36,4 +50,23 @@ int read_config_file() {
return 0;
}
ConfigValues* getConfigValues(){
return &configValues;
}
static void setConfigValues(const char * key, char *value){
if( !strcmp ( SSID, key ))
configValues.ssid = value;
if( !strcmp ( PASSPHRASE, key ))
configValues.pass = value;
}
}

View File

@ -5,12 +5,15 @@
#ifndef WIHOTSPOT_READ_CONFIG_H
#define WIHOTSPOT_READ_CONFIG_H
#include "h_prop.h"
#ifdef __cplusplus
extern "C" {
#endif
int read_config_file();
static void setConfigValues(const char * key, char *value);
ConfigValues* getConfigValues(void);
#ifdef __cplusplus
}
#endif