-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigreader.cpp
43 lines (36 loc) · 1.07 KB
/
configreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <libconfig.h>
#include <string.h>
using namespace std;
string parser(const char* file, const char* block, const char* key)
{
config_t cfg; /*Returns all parameters in this structure */
config_setting_t *setting;
const char *str1;
const char *str2;
int tmp;
const char *config_file_name = file;
string value;
/*Initialization */
config_init(&cfg);
/* Read the file. If there is an error, report it and exit. */
if (!config_read_file(&cfg, config_file_name))
{
printf("\n%s:%d - %s", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return "Error";
}
/*Read the parameter group*/
setting = config_lookup(&cfg, block);
if (setting != NULL){
/*Read the string*/
if (config_setting_lookup_string(setting, key, &str2))
value = (const char*) str2;
else{
printf("\nNo setting in configuration file.");
value = "";
}
}
config_destroy(&cfg);
return value;
}