irccd  3.0.3
ini_util.hpp
1 /*
2  * ini_util.hpp -- ini utilities
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_UTIL_HPP
20 #define IRCCD_INI_UTIL_HPP
21 
27 #include <optional>
28 
29 #include "ini.hpp"
30 #include "string_util.hpp"
31 
32 namespace irccd {
33 
37 namespace ini_util {
38 
46 template <typename Int>
47 inline auto get_uint(const ini::section& sc, std::string_view name) noexcept -> std::optional<Int>
48 {
49  return string_util::to_uint<Int>(sc.get(name).get_value());
50 }
51 
60 inline auto optional_string(const ini::section& sc,
61  std::string_view name,
62  std::string_view def) noexcept -> std::string
63 {
64  const auto it = sc.find(name);
65 
66  if (it == sc.end())
67  return std::string(def);
68 
69  return it->get_value();
70 }
71 
80 template <typename Int>
81 inline auto optional_uint(const ini::section& sc,
82  std::string_view name,
83  Int def) noexcept -> std::optional<Int>
84 {
85  const auto it = sc.find(name);
86 
87  if (it == sc.end())
88  return def;
89 
90  return string_util::to_uint<Int>(it->get_value());
91 }
92 
93 } // !ini_util
94 
95 } // !irccd
96 
97 #endif // !IRCCD_INI_UTIL_HPP
Section that contains one or more options.
Definition: ini.hpp:295
auto optional_uint(const ini::section &sc, std::string_view name, Int def) noexcept -> std::optional< Int >
Definition: ini_util.hpp:81
auto get_uint(const ini::section &sc, std::string_view name) noexcept -> std::optional< Int >
Definition: ini_util.hpp:47
auto optional_string(const ini::section &sc, std::string_view name, std::string_view def) noexcept -> std::string
Definition: ini_util.hpp:60
Parent namespace.
Definition: acceptor.hpp:43