View difference between Paste ID: bUB90WBg and 2F6BG2Lr
SHOW: | | - or go back to the newest paste.
1
-----------------------------------------------------------------------
2
main.cpp
3
-----------------------------------------------------------------------
4
#include <cassert>
5
#include <sstream>
6
7
#include "ini.h"
8
9
using namespace std;
10
11
int main() {
12
    std::istringstream input{
13
        "[vegetables]\n"
14
        "potatoes=10\n"
15
        "onions=1 \n"
16
        "\n"
17
        "cucumbers=12\n"
18
        "\n"
19
        "[guests] \n"
20
        "guest1_name = Ivan Durak\n"
21
        "guest2_name =  Vasilisa Premudraya\n"
22
        "[guest black list]"};
23
    ini::Document doc = ini::Load(input);
24
25
    assert(doc.GetSectionCount() == 3);
26
    assert((doc.GetSection("vegetables"s)
27
            == ini::Section{
28
                {"potatoes"s, "10"s},
29
                {"onions"s, "1"s},
30
                {"cucumbers"s, "12"s},
31
            }));
32
    assert((doc.GetSection("guests"s)
33
            == ini::Section{{"guest1_name"s, "Ivan Durak"s}, {"guest2_name"s, "Vasilisa Premudraya"s}}));
34
    assert((doc.GetSection("dragons"s) == ini::Section{}));
35
    assert((doc.GetSection("guest black list"s) == ini::Section{}));
36
37
    doc.AddSection("pets"s) = ini::Section{{"nasty"s, "rat"s}};
38
    assert(doc.GetSectionCount() == 4);
39
}
40
-----------------------------------------------------------------------
41
ini.h
42
-----------------------------------------------------------------------
43
#include <iostream>
44
#include <string>
45
#include <unordered_map>
46
47
namespace ini {
48
    
49
using Section = std::unordered_map<std::string, std::string>;
50
51
class Document {
52
public:
53
    Section& AddSection(std::string name);
54
    const Section& GetSection(const std::string& name) const;
55
    std::size_t GetSectionCount() const;
56
57
private:
58
    std::unordered_map<std::string, Section> sections_;
59
};
60
61
Document Load(std::istream& input);
62
    
63
}
64
-----------------------------------------------------------------------
65
ini.cpp
66
-----------------------------------------------------------------------
67
#include "ini.h"
68
69
using namespace std;
70
71
namespace ini {
72
    
73
Section& Document::AddSection(std::string name) {
74
        return sections_[name];
75
}
76
77
const Section& Document::GetSection(const std::string& name) const {
78
    
79
    static Section section_;
80
    
81
    if (!(sections_.end() == sections_.find(name))) {
82
        return sections_.at(name);
83
    } else {
84
        return section_;
85
    }
86
}
87
88
std::size_t Document::GetSectionCount() const {
89
    return sections_.size();
90
}
91
92
Document Load(std::istream& input) {
93
    
94
    Document result;
95
    Section* section;
96
    
97
    for (string str; 
98
         getline(input, str);) {
99
        
100
        auto it = str.find_first_not_of(' ');
101
        
102
        if (str.npos == it) {
103
            continue;
104
        }
105
        
106
        str = str.substr(it);
107
        
108
        if (str[0] == '[') {
109
            auto section_ = str.substr(1, str.find(']') - 1);
110
            section = &result.AddSection(section_);
111
            
112
        } else {
113
            it = str.find('=');
114
            
115
            string name_ = str.substr(0, str.find_last_not_of(' ', it - 1) + 1);
116
            str = str.substr(str.find_first_not_of(' ', it + 1));
117
            
118
            string value_ = str.substr(0, str.find_last_not_of(' ') + 1);
119
            section->insert({name_, 
120
                             value_ });
121
        } 
122
    }
123
    return result;
124
}
125
        
126
}