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