File: config_parser.cpp

package info (click to toggle)
yrmcds 1.0.4-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 924 kB
  • ctags: 1,346
  • sloc: cpp: 9,634; sh: 133; makefile: 97
file content (56 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (4)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// (C) 2013 Cybozu.

#include "config_parser.hpp"

#include <fstream>

namespace {

const char white_spaces[] = " \f\n\r\t\v";

inline void rtrim(std::string& s) {
    s.erase( s.find_last_not_of(white_spaces) + 1 );
}

inline void ltrim(std::string& s) {
    s.erase( 0, s.find_first_not_of(white_spaces) );
}

} // anonymous namespace

namespace cybozu {

void config_parser::load(const std::string& path) {
    m_config.clear();

    std::ifstream is(path);
    if( !is )
        throw std::runtime_error("failed to open " + path);
    unsigned int lineno = 0;

    for( std::string l; std::getline(is, l); ) {
        lineno++;

        ltrim(l);
        rtrim(l);
        if( l.empty() || l[0] == '#' ) continue;

        std::size_t n = l.find('=');
        if( n == std::string::npos )
            throw parse_error(path, lineno);
        std::string key = l.substr(0, n);
        rtrim(key);
        std::string value = l.substr(n+1);
        ltrim(value);

        // unquote the value
        std::size_t len = value.size();
        if( len >= 2 && value[0] == '"' && value[len-1] == '"' ) {
            value = value.substr(1, len-2);
        }

        set(key, value);
    }
}

} // namespace cybozu