irccd  3.0.3
ini.hpp
1 /*
2  * ini.hpp -- extended .ini file parser
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_INI_HPP
20 #define IRCCD_INI_HPP
21 
108 #include "sysconfig.hpp"
109 
110 #include <algorithm>
111 #include <exception>
112 #include <stdexcept>
113 #include <string>
114 #include <string_view>
115 #include <vector>
116 
117 namespace irccd {
118 
122 namespace ini {
123 
124 class document;
125 
129 class exception : public std::exception {
130 private:
131  unsigned line_;
132  unsigned column_;
133  std::string message_;
134 
135 public:
143  exception(unsigned line, unsigned column, std::string msg) noexcept;
144 
150  auto line() const noexcept -> unsigned;
151 
157  auto column() const noexcept -> unsigned;
158 
164  auto what() const noexcept -> const char* override;
165 };
166 
174 class token {
175 public:
179  enum type {
188  comma
189  };
190 
191 private:
192  type type_;
193  unsigned line_;
194  unsigned column_;
195  std::string value_;
196 
197 public:
206  token(type type, unsigned line, unsigned column, std::string value = "") noexcept;
207 
213  auto get_type() const noexcept -> type;
214 
220  auto get_line() const noexcept -> unsigned;
221 
227  auto get_column() const noexcept -> unsigned;
228 
235  auto get_value() const noexcept -> const std::string&;
236 };
237 
241 using tokens = std::vector<token>;
242 
246 class option : public std::vector<std::string> {
247 private:
248  std::string key_;
249 
250 public:
257  option(std::string key) noexcept;
258 
266  option(std::string key, std::string value) noexcept;
267 
275  option(std::string key, std::vector<std::string> values) noexcept;
276 
282  auto get_key() const noexcept -> const std::string&;
283 
289  auto get_value() const noexcept -> const std::string&;
290 };
291 
295 class section : public std::vector<option> {
296 private:
297  std::string key_;
298 
299 public:
306  section(std::string key) noexcept;
307 
313  auto get_key() const noexcept -> const std::string&;
314 
321  auto contains(std::string_view key) const noexcept -> bool;
322 
329  auto get(std::string_view key) const noexcept -> option;
330 
337  auto find(std::string_view key) noexcept -> iterator;
338 
345  auto find(std::string_view key) const noexcept -> const_iterator;
346 
354  auto operator[](std::string_view key) -> option&;
355 
363  auto operator[](std::string_view key) const -> const option&;
364 
368  using std::vector<option>::operator[];
369 };
370 
376 class document : public std::vector<section> {
377 public:
384  auto contains(std::string_view key) const noexcept -> bool;
385 
392  auto get(std::string_view key) const noexcept -> section;
393 
400  auto find(std::string_view key) noexcept -> iterator;
401 
408  auto find(std::string_view key) const noexcept -> const_iterator;
409 
417  auto operator[](std::string_view key) -> section&;
418 
426  auto operator[](std::string_view key) const -> const section&;
427 
431  using std::vector<section>::operator[];
432 };
433 
446 auto analyse(std::istreambuf_iterator<char> it, std::istreambuf_iterator<char> end) -> tokens;
447 
455 auto analyse(std::istream& stream) -> tokens;
456 
465 auto parse(const tokens& tokens, const std::string& path = ".") -> document;
466 
474 auto read_file(const std::string& filename) -> document;
475 
486 auto read_string(const std::string& buffer) -> document;
487 
493 void dump(const tokens& tokens);
494 
495 } // !ini
496 
497 } // !irccd
498 
499 #endif // !IRCCD_INI_HPP
Ini document description.
Definition: ini.hpp:376
auto operator[](std::string_view key) const -> const section &
auto find(std::string_view key) noexcept -> iterator
auto operator[](std::string_view key) -> section &
auto get(std::string_view key) const noexcept -> section
auto contains(std::string_view key) const noexcept -> bool
auto find(std::string_view key) const noexcept -> const_iterator
exception in a file.
Definition: ini.hpp:129
auto what() const noexcept -> const char *override
auto line() const noexcept -> unsigned
exception(unsigned line, unsigned column, std::string msg) noexcept
auto column() const noexcept -> unsigned
option definition.
Definition: ini.hpp:246
option(std::string key, std::string value) noexcept
auto get_key() const noexcept -> const std::string &
option(std::string key, std::vector< std::string > values) noexcept
option(std::string key) noexcept
Section that contains one or more options.
Definition: ini.hpp:295
auto get_key() const noexcept -> const std::string &
section(std::string key) noexcept
Describe a token read in the .ini source.
Definition: ini.hpp:174
token(type type, unsigned line, unsigned column, std::string value="") noexcept
type
token type.
Definition: ini.hpp:179
@ section
[section]
Definition: ini.hpp:182
@ tryinclude
tryinclude statement
Definition: ini.hpp:181
@ list_end
end of list )
Definition: ini.hpp:187
@ include
include statement
Definition: ini.hpp:180
@ list_begin
begin of list (
Definition: ini.hpp:186
@ quoted_word
word with quotes
Definition: ini.hpp:184
@ assign
= assignment
Definition: ini.hpp:185
@ word
word without quotes
Definition: ini.hpp:183
Abstract stream interface.
Definition: stream.hpp:58
auto read_string(const std::string &buffer) -> document
void dump(const tokens &tokens)
auto analyse(std::istreambuf_iterator< char > it, std::istreambuf_iterator< char > end) -> tokens
auto read_file(const std::string &filename) -> document
auto parse(const tokens &tokens, const std::string &path=".") -> document
std::vector< token > tokens
Definition: ini.hpp:241
Parent namespace.
Definition: acceptor.hpp:43
Definition: bot.hpp:253