| 50 | } |
| 51 | |
| 52 | bool TEngineConfig::Load(const std::string filename, const char delimiter, const char comment) |
| 53 | { |
| 54 | std::fstream cfgfile(filename.c_str()); |
| 55 | if(!cfgfile) |
| 56 | { |
| 57 | LOG_ERROR() << "Can not open the config file: " << filename << "\n"; |
| 58 | LOG_ERROR() << "Please cp ./etc/config.example ./etc/config\n"; |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Set the seperators |
| 63 | delim_ch = delimiter; |
| 64 | commt_ch = comment; |
| 65 | |
| 66 | // Read the keys and values from the config file |
| 67 | typedef std::string::size_type pos; |
| 68 | ConfManager* manager = GetConfManager(); |
| 69 | while(!cfgfile.eof()) |
| 70 | { |
| 71 | std::string line; |
| 72 | getline(cfgfile, line); |
| 73 | |
| 74 | // Ignore comments |
| 75 | line = line.substr(0, line.find(commt_ch)); |
| 76 | if(!line.length()) |
| 77 | continue; |
| 78 | |
| 79 | // Parse the line if it contains a delimiter |
| 80 | pos delim_pos = line.find(delim_ch); |
| 81 | if(delim_pos != std::string::npos) |
| 82 | { |
| 83 | // Get the key and the value |
| 84 | std::string key = line.substr(0, delim_pos); |
| 85 | line.replace(0, delim_pos + 1, ""); |
| 86 | // Remove the leading and trailing whitespaces |
| 87 | Trim(key); |
| 88 | Trim(line); |
| 89 | // Store the key and the value, overwrites if the key is repeated |
| 90 | // Set the config manager |
| 91 | manager->SetAttr(key, line); |
| 92 | } |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | void TEngineConfig::Remove(const std::string& key) |
| 98 | { |
no test coverage detected