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