This is a parser which can be used to parse configuration files with INI format.(INI文件解析器)
Structure of INI files:
;this is a comment
[section]
key = value
The above file structure can be mapped to the following data structures:
key: std::string
value: std::string // value's type can also be converted to int/unsigned/bool.
section: INIMap<std::string> // vector< pair< string, string >, ... >
file: INIMap<INIMap<std::string>> // vector< section, ... >
If you're not familiar with the INI format, please refer to INI_file.
The parser conforms to the following format:
- Names of section & key are both case insensitive.😘
- Whitespace delimiters around sections, keys & values are all ignored.😘
- Keys which do not belong to a section are ignored.😘
- Each entry exists on a single line.(Multi-line is not supported.)😘
ns_INI::INIReader reader("../test_data.ini");
ns_INI::INIStructure fileData;
reader >> fileData; // parse contents in test_data.ini to fileData
/* You can get a section by two ways. */
std::string sectionName = "ama_all_in_000";
// The first way: **INIMap::get()**
if(fileData.has(sectionName)){ // Make sure the section exists
auto section = fileData.get(sectionName);
}
// The second way: **INIMap::operator[]**
if(fileData.has(sectionName)){
auto section = fileData[sectionName];
}
/* You can get a value by two ways. */
std::string key = "enable";
// The first way: **INIMap::get(), get_int()/get_unsigned()/get_bool() also provided**
if(section.has(key)){
std::string val_str = section.get(key); // val_str = "true", equivalent to: val_str = fileData.get(sectionName).get(key);
bool val_bool = section.get_bool(key); // val_bool = true;
}
// The second way: **INIMap::operator[]**
if(section.has(key)){
std::string val = section[key]; // equivalent to: val = fileData[sectionName][key];
}
- Design the data structure of INI
- Write the INI reader
- Write the INI writer
- More possible features?