MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
ini_parser.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <sstream>
11
12#include "util/exception.h"
13#include "util/strings.h"
14#include "util/ini_parser.h"
15
17
18namespace
19{
21 get_exception (int line_number, char const* message)
22 {
23 std::stringstream ss;
24 ss << "Line " << line_number << ": " << message;
25 return ss.str();
26 }
27}
28
29void
30parse_ini (std::istream& stream, std::map<std::string, std::string>* map)
31{
32 std::string section_name;
33 int line_number = 0;
34 while (true)
35 {
36 /* Read line from input. */
37 line_number += 1;
38 std::string line;
39 std::getline(stream, line);
40 if (stream.eof())
41 break;
42
45
46 /* Skip empty lines and comments. */
47 if (line.empty())
48 continue;
49 if (line[0] == '#')
50 continue;
51
52 /* Read section name. */
53 if (line[0] == '[' && line[line.size() - 1] == ']')
54 {
55 section_name = line.substr(1, line.size() - 2);
56 continue;
57 }
58
59 /* Read key/value pair. */
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");
63
64 std::string key = line.substr(0, sep_pos);
67
68 std::string value = line.substr(sep_pos + 1);
71
72 if (key.empty())
73 throw get_exception(line_number, "Empty key");
74 if (section_name.empty())
75 throw get_exception(line_number, "No section");
76
77 key = section_name + "." + key;
78 (*map)[key] = value;
79 }
80}
81
82void
83write_ini (std::map<std::string, std::string> const& map, std::ostream& stream)
84{
85 std::string last_section;
86 std::map<std::string, std::string>::const_iterator iter;
87 for (iter = map.begin(); iter != map.end(); iter++)
88 {
89 std::string key = iter->first;
90 std::string value = iter->second;
95
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);
101
102 if (section != last_section)
103 {
104 stream << "\n[" << section << "]\n";
105 last_section = section;
106 }
107
108 stream << key << " = " << value << std::endl;
109 }
110}
111
Universal, simple exception class.
Definition exception.h:24
void clip_newlines(std::string *str)
Clips newlines from the end of the string, in-place.
Definition strings.h:317
void clip_whitespaces(std::string *str)
Clips whitespaces from the front and end of the string, in-place.
Definition strings.h:299
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.
Definition ini_parser.cc:83
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.
Definition ini_parser.cc:30
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14