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