/src/Botan-3.4.0/src/lib/utils/read_cfg.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Simple config/test file reader |
3 | | * (C) 2013,2014,2015 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/parsing.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | |
12 | | namespace Botan { |
13 | | |
14 | | namespace { |
15 | | |
16 | 0 | std::string clean_ws(std::string_view s) { |
17 | 0 | const char* ws = " \t\n"; |
18 | 0 | auto start = s.find_first_not_of(ws); |
19 | 0 | auto end = s.find_last_not_of(ws); |
20 | |
|
21 | 0 | if(start == std::string::npos) { |
22 | 0 | return ""; |
23 | 0 | } |
24 | | |
25 | 0 | if(end == std::string::npos) { |
26 | 0 | return std::string(s.substr(start, end)); |
27 | 0 | } else { |
28 | 0 | return std::string(s.substr(start, start + end + 1)); |
29 | 0 | } |
30 | 0 | } |
31 | | |
32 | | } // namespace |
33 | | |
34 | 0 | std::map<std::string, std::string> read_cfg(std::istream& is) { |
35 | 0 | std::map<std::string, std::string> kv; |
36 | 0 | size_t line = 0; |
37 | |
|
38 | 0 | while(is.good()) { |
39 | 0 | std::string s; |
40 | |
|
41 | 0 | std::getline(is, s); |
42 | |
|
43 | 0 | ++line; |
44 | |
|
45 | 0 | if(s.empty() || s[0] == '#') { |
46 | 0 | continue; |
47 | 0 | } |
48 | | |
49 | 0 | s = clean_ws(s.substr(0, s.find('#'))); |
50 | |
|
51 | 0 | if(s.empty()) { |
52 | 0 | continue; |
53 | 0 | } |
54 | | |
55 | 0 | auto eq = s.find('='); |
56 | |
|
57 | 0 | if(eq == std::string::npos || eq == 0 || eq == s.size() - 1) { |
58 | 0 | throw Decoding_Error("Bad read_cfg input '" + s + "' on line " + std::to_string(line)); |
59 | 0 | } |
60 | | |
61 | 0 | const std::string key = clean_ws(s.substr(0, eq)); |
62 | 0 | const std::string val = clean_ws(s.substr(eq + 1, std::string::npos)); |
63 | |
|
64 | 0 | kv[key] = val; |
65 | 0 | } |
66 | | |
67 | 0 | return kv; |
68 | 0 | } |
69 | | |
70 | | } // namespace Botan |