wangzhengquan
2020-06-12 9f2f9eed5ca905c4641b296773ac21aaa735624d
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
#include "properties_config.h"
 
PropertiesConfig::PropertiesConfig(std::string __propertiesFile) : propertiesFile(__propertiesFile) {
 
    std::ifstream fin(propertiesFile);
    char line[1024];
    //std::string line;
    char *key, *value;
    const char *delim = "=";
    while(fin.getline(line, 1024)) {
        // printf("line=%s\n", line);
        if(strlen(trim(line, NULL))== 0)
            continue;
        if(*line == '#') {
            continue;
        }
 
        key = trim(strtok(line, delim), 0);
        value = trim(strtok(NULL, delim), 0);
        propertiesMap.insert({key, value});
        // printf("key = %s, value=%s\n", key, value);
    }
    fin.close();
 
}
 
 
std::string PropertiesConfig::get(std::string name) {
  std::map<std::string, std::string>::iterator propertiesIter = propertiesMap.find(name);
  if( propertiesIter != propertiesMap.end() ) {
     return propertiesIter->second;
  }
  return "";
}
 
int PropertiesConfig::getInt(std::string name) {
  std::map<std::string, std::string>::iterator propertiesIter = propertiesMap.find(name);
  if( propertiesIter != propertiesMap.end() ) {
     return  std::stoi(propertiesIter->second);
  }
  return 0;
}