Botan 3.4.0
Crypto and TLS for C&
read_cfg.cpp
Go to the documentation of this file.
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
12namespace Botan {
13
14namespace {
15
16std::string clean_ws(std::string_view s) {
17 const char* ws = " \t\n";
18 auto start = s.find_first_not_of(ws);
19 auto end = s.find_last_not_of(ws);
20
21 if(start == std::string::npos) {
22 return "";
23 }
24
25 if(end == std::string::npos) {
26 return std::string(s.substr(start, end));
27 } else {
28 return std::string(s.substr(start, start + end + 1));
29 }
30}
31
32} // namespace
33
34std::map<std::string, std::string> read_cfg(std::istream& is) {
35 std::map<std::string, std::string> kv;
36 size_t line = 0;
37
38 while(is.good()) {
39 std::string s;
40
41 std::getline(is, s);
42
43 ++line;
44
45 if(s.empty() || s[0] == '#') {
46 continue;
47 }
48
49 s = clean_ws(s.substr(0, s.find('#')));
50
51 if(s.empty()) {
52 continue;
53 }
54
55 auto eq = s.find('=');
56
57 if(eq == std::string::npos || eq == 0 || eq == s.size() - 1) {
58 throw Decoding_Error("Bad read_cfg input '" + s + "' on line " + std::to_string(line));
59 }
60
61 const std::string key = clean_ws(s.substr(0, eq));
62 const std::string val = clean_ws(s.substr(eq + 1, std::string::npos));
63
64 kv[key] = val;
65 }
66
67 return kv;
68}
69
70} // namespace Botan
std::map< std::string, std::string > read_cfg(std::istream &is)
Definition read_cfg.cpp:34