File tree Expand file tree Collapse file tree 7 files changed +16
-19
lines changed Expand file tree Collapse file tree 7 files changed +16
-19
lines changed Original file line number Diff line number Diff line change 1
1
{
2
- "version" : 1 ,
2
+ "version" : 2 ,
3
3
"cmakeMinimumRequired" : {
4
4
"major" : 3 ,
5
5
"minor" : 19 ,
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class pini {
15
15
16
16
map_type const & get_pairs () const { return key_value_pairs; }
17
17
bool load_file (std::filesystem::path const & filename);
18
- bool load_text (std::string_view text);
18
+ bool load_text (std::string const & text);
19
19
20
20
// iteration
21
21
Original file line number Diff line number Diff line change @@ -17,9 +17,9 @@ std::vector<std::string> get_lines(std::filesystem::path const& filename);
17
17
18
18
// extract key-value pairs from given vector of strings and insert them in an
19
19
// unordered_map
20
- std::unordered_map<std::string, std::string> insert_pairs (std::vector<std::string> file_lines);
20
+ std::unordered_map<std::string, std::string> insert_pairs (std::vector<std::string> const & file_lines);
21
21
22
- std::string_view trim_whitespace (std::string_view str);
22
+ std::string trim_whitespace (std::string const & str);
23
23
24
24
// data conversions:
25
25
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ pini::map_type read_file(std::filesystem::path const& filename) {
13
13
return util::insert_pairs (lines);
14
14
}
15
15
16
- pini::map_type read_text (std::string_view text) {
16
+ pini::map_type read_text (std::string const & text) {
17
17
std::string str;
18
18
std::stringstream str_stream;
19
19
str_stream << text;
@@ -29,7 +29,7 @@ bool pini::load_file(std::filesystem::path const& filename) {
29
29
return !key_value_pairs.empty ();
30
30
}
31
31
32
- bool pini::load_text (std::string_view text) {
32
+ bool pini::load_text (std::string const & text) {
33
33
key_value_pairs = read_text (text);
34
34
return !key_value_pairs.empty ();
35
35
}
Original file line number Diff line number Diff line change 1
- #include < cassert>
2
1
#include < cctype>
3
- #include < cstddef>
4
- #include < string_view>
2
+
5
3
#include < pini/pini.hpp>
6
4
#include < pini/util.hpp>
7
5
namespace pn {
@@ -20,29 +18,29 @@ std::vector<std::string> util::get_lines(std::filesystem::path const& filename)
20
18
return ret;
21
19
}
22
20
23
- std::unordered_map<std::string, std::string> util::insert_pairs (std::vector<std::string> file_lines) {
21
+ std::unordered_map<std::string, std::string> util::insert_pairs (std::vector<std::string> const & file_lines) {
24
22
std::unordered_map<std::string, std::string> key_value_pairs;
25
23
std::size_t line_number = 0 ;
26
24
for (const auto & str : file_lines) {
27
25
++line_number;
28
26
if (auto delimeter_pos = str.find (' =' ); delimeter_pos != std::string::npos) {
29
- std::string_view const key = trim_whitespace (str.substr (0 , delimeter_pos));
27
+ std::string const key = trim_whitespace (str.substr (0 , delimeter_pos));
30
28
if (key.empty ()) {
31
29
if (pn::pini::on_msg) {
32
30
std::stringstream str;
33
31
str << " Line number " << line_number << " is not valid because the key is empty.\n " ;
34
32
(*pn::pini::on_msg)(str.str (), pn::pini::severity::warn);
35
33
}
36
34
} else {
37
- std::string_view const value = trim_whitespace (str.substr (delimeter_pos + 1 ));
35
+ std::string const value = trim_whitespace (str.substr (delimeter_pos + 1 ));
38
36
if (key[0 ] != ' #' ) { key_value_pairs.insert ({std::string (key), std::string (value)}); }
39
37
}
40
38
}
41
39
}
42
40
return key_value_pairs;
43
41
}
44
42
45
- std::string_view util::trim_whitespace (std::string_view str) {
43
+ std::string util::trim_whitespace (std::string const & str) {
46
44
if (str.empty ()) { return {}; }
47
45
std::size_t start = 0 ;
48
46
std::size_t finish = str.size ();
Original file line number Diff line number Diff line change 5
5
int main () {
6
6
pn::pini pin;
7
7
8
- // std::string_view raw_input{"= 5\n#b=10\n c = 23"};
9
- std::filesystem::path filename{" /home/nikolaj/projects/cpp/pini/test/test.pini" };
8
+ std::string raw_input{" health = 9\n attack = 4.5\n b=32\n #comment\n def = -9.2314\n skill points = 8\n " };
10
9
static int ret = 0 ;
11
- pin. on_msg = [](std::string_view msg, pn::pini::severity level) {
10
+ pn::pini:: on_msg = [](std::string_view msg, pn::pini::severity level) {
12
11
auto & out = level == pn::pini::severity::error ? std::cerr : std::cout;
13
12
out << " Custom: || " << msg << ' \n ' ;
14
13
if (level == pn::pini::severity::error) { ret = 1 ; }
15
14
};
16
- if (!pin.load_file (filename )) { return 1 ; }
15
+ if (!pin.load_text (raw_input )) { return 1 ; }
17
16
if (pin.empty ()) { return 1 ; }
18
17
if (pin.contains ({})) { return 1 ; }
19
18
if (pin.contains (" #comment" )) { return 1 ; }
20
19
if (pin.get_int32 (" health" ) != 9 ) { return 1 ; }
21
20
22
21
for (auto & pair : pin) { std::cout << pair.first << " " << pair.second << ' \n ' ; }
23
- std::cout << pin.size () << " | " << pin.empty ();
22
+ std::cout << pin.size () << " | " << pin.empty () << ' \n ' ;
24
23
pin.erase (" def" );
25
24
for (auto & pair : pin) { std::cout << pair.first << " " << pair.second << ' \n ' ; }
26
25
return ret;
Original file line number Diff line number Diff line change 1
1
health = 9
2
2
attack = 4.5
3
- = 32
3
+ b= 32
4
4
#comment
5
5
def = -9.2314
6
6
skill points = 8
You can’t perform that action at this time.
0 commit comments