30parse_ini (std::istream& stream, std::map<std::string, std::string>* map)
32 std::string section_name;
39 std::getline(stream, line);
53 if (line[0] ==
'[' && line[line.size() - 1] ==
']')
55 section_name = line.substr(1, line.size() - 2);
60 std::size_t sep_pos = line.find_first_of(
'=');
61 if (sep_pos == std::string::npos)
62 throw get_exception(line_number,
"Invalid line");
64 std::string key = line.substr(0, sep_pos);
68 std::string value = line.substr(sep_pos + 1);
73 throw get_exception(line_number,
"Empty key");
74 if (section_name.empty())
75 throw get_exception(line_number,
"No section");
77 key = section_name +
"." + key;
83write_ini (std::map<std::string, std::string>
const& map, std::ostream& stream)
85 std::string last_section;
86 std::map<std::string, std::string>::const_iterator iter;
87 for (iter = map.begin(); iter != map.end(); iter++)
89 std::string key = iter->first;
90 std::string value = iter->second;
96 std::size_t section_pos = key.find_first_of(
'.');
97 if (section_pos == std::string::npos)
98 throw std::runtime_error(
"Key/value pair without section");
99 std::string section = key.substr(0, section_pos);
100 key = key.substr(section_pos + 1);
102 if (section != last_section)
104 stream <<
"\n[" << section <<
"]\n";
105 last_section = section;
108 stream << key <<
" = " << value << std::endl;
void write_ini(std::map< std::string, std::string > const &map, std::ostream &stream)
Writes an INI file for the key/value pairs in the map.
void parse_ini(std::istream &stream, std::map< std::string, std::string > *map)
Parses a file in INI format and places key/value pairs in the map.