inicpp
C++ parser of INI files with schema validation.
config.cpp
1 #include "config.h"
2 
3 namespace inicpp
4 {
6  {
7  }
8 
9  config::config(const config &source)
10  {
11  // we have to do deep copies of sections
12  sections_.reserve(source.sections_.size());
13  for (auto &sect : source.sections_) {
14  sections_.push_back(std::make_shared<section>(*sect));
15  }
16 
17  // we already have constructed sections... now push them into map
18  for (auto &sect : sections_) {
19  sections_map_.insert(sections_map_pair(sect->get_name(), sect));
20  }
21  }
22 
24  {
25  if (this != &source) {
26  // make copy of input source config and swap it with this
27  config new_src(source);
28  std::swap(*this, new_src);
29  }
30 
31  return *this;
32  }
33 
35  {
36  operator=(std::move(source));
37  }
38 
40  {
41  if (this != &source) {
42  sections_ = std::move(source.sections_);
43  sections_map_ = std::move(source.sections_map_);
44  }
45  return *this;
46  }
47 
48  void config::add_section(const section &sect)
49  {
50  auto add_it = sections_map_.find(sect.get_name());
51  if (add_it == sections_map_.end()) {
52  std::shared_ptr<section> add = std::make_shared<section>(sect);
53  sections_.push_back(add);
54  sections_map_.insert(sections_map_pair(add->get_name(), add));
55  } else {
56  throw ambiguity_exception(sect.get_name());
57  }
58  }
59 
60  void config::add_section(const std::string &section_name)
61  {
62  auto add_it = sections_map_.find(section_name);
63  if (add_it == sections_map_.end()) {
64  std::shared_ptr<section> add = std::make_shared<section>(section_name);
65  sections_.push_back(add);
66  sections_map_.insert(sections_map_pair(add->get_name(), add));
67  } else {
68  throw ambiguity_exception(section_name);
69  }
70  }
71 
72  void config::remove_section(const std::string &section_name)
73  {
74  auto del_it = sections_map_.find(section_name);
75  if (del_it != sections_map_.end()) {
76  // remove from map
77  sections_map_.erase(del_it);
78  // remove from vector
79  sections_.erase(
80  std::remove_if(sections_.begin(),
81  sections_.end(),
82  [&](std::shared_ptr<section> sect) { return (sect->get_name() == section_name ? true : false); }),
83  sections_.end());
84  } else {
85  throw not_found_exception(section_name);
86  }
87  }
88 
89  void config::add_option(const std::string &section_name, const option &opt)
90  {
91  auto sect_it = sections_map_.find(section_name);
92  if (sect_it != sections_map_.end()) {
93  sect_it->second->add_option(opt);
94  } else {
95  throw not_found_exception(section_name);
96  }
97  }
98 
99  void config::remove_option(const std::string &section_name, const std::string &option_name)
100  {
101  auto sect_it = sections_map_.find(section_name);
102  if (sect_it != sections_map_.end()) {
103  sect_it->second->remove_option(option_name);
104  } else {
105  throw not_found_exception(section_name);
106  }
107  }
108 
109  size_t config::size() const
110  {
111  return sections_.size();
112  }
113 
115  {
116  if (index >= sections_.size()) {
117  throw not_found_exception(index);
118  }
119 
120  return *sections_[index];
121  }
122 
123  const section &config::operator[](size_t index) const
124  {
125  if (index >= sections_.size()) {
126  throw not_found_exception(index);
127  }
128 
129  return *sections_[index];
130  }
131 
132  section &config::operator[](const std::string &section_name)
133  {
134  std::shared_ptr<section> result;
135  try {
136  result = sections_map_.at(section_name);
137  } catch (std::out_of_range) {
138  throw not_found_exception(section_name);
139  }
140  return *result;
141  }
142 
143  const section &config::operator[](const std::string &section_name) const
144  {
145  std::shared_ptr<section> result;
146  try {
147  result = sections_map_.at(section_name);
148  } catch (std::out_of_range) {
149  throw not_found_exception(section_name);
150  }
151  return *result;
152  }
153 
154  bool config::contains(const std::string &section_name) const
155  {
156  try {
157  sections_map_.at(section_name);
158  return true;
159  } catch (std::out_of_range) {
160  return false;
161  }
162  }
163 
164  void config::validate(const schema &schm, schema_mode mode)
165  {
166  schm.validate_config(*this, mode);
167  }
168 
169  bool config::operator==(const config &other) const
170  {
171  return std::equal(sections_.begin(),
172  sections_.end(),
173  other.sections_.begin(),
174  [](const std::shared_ptr<section> &first, const std::shared_ptr<section> &second) {
175  return *first == *second;
176  });
177  }
178 
179  bool config::operator!=(const config &other) const
180  {
181  return !(*this == other);
182  }
183 
185  {
186  return iterator(*this);
187  }
188 
190  {
191  return iterator(*this, sections_.size());
192  }
193 
195  {
196  return const_iterator(const_cast<config &>(*this));
197  }
198 
200  {
201  return const_iterator(const_cast<config &>(*this), sections_.size());
202  }
203 
205  {
206  return const_iterator(const_cast<config &>(*this));
207  }
208 
210  {
211  return const_iterator(const_cast<config &>(*this), sections_.size());
212  }
213 
214  std::ostream &operator<<(std::ostream &os, const config &conf)
215  {
216  for (auto &sect : conf.sections_) {
217  os << *sect;
218  }
219 
220  return os;
221  }
222 }
bool operator!=(const config &other) const
Definition: config.cpp:179
config_iterator< const section > const_iterator
Definition: config.h:47
bool operator==(const config &other) const
Definition: config.cpp:169
config & operator=(const config &source)
Definition: config.cpp:23
section & operator[](size_t index)
Definition: config.cpp:114
void remove_option(const std::string &section_name, const std::string &option_name)
Definition: config.cpp:99
void add_option(const std::string &section_name, const option &opt)
Definition: config.cpp:89
config_iterator< section > iterator
Definition: config.h:45
Definition: config.h:15
size_t size() const
Definition: config.cpp:109
const_iterator cbegin() const
Definition: config.cpp:204
iterator end()
Definition: config.cpp:189
void validate_config(config &cfg, schema_mode mode) const
Definition: schema.cpp:137
INICPP_API friend std::ostream & operator<<(std::ostream &os, const config &conf)
Definition: config.cpp:214
bool contains(const std::string &section_name) const
Definition: config.cpp:154
iterator begin()
Definition: config.cpp:184
const_iterator cend() const
Definition: config.cpp:209
void validate(const schema &schm, schema_mode mode)
Definition: config.cpp:164
void remove_section(const std::string &section_name)
Definition: config.cpp:72
const std::string & get_name() const
Definition: section.cpp:49
void add_section(const section &sect)
Definition: config.cpp:48