Botan
1.11.15
|
00001 /* 00002 * Simple config/test file reader 00003 * (C) 2013,2014,2015 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #include <botan/parsing.h> 00009 00010 namespace Botan { 00011 00012 std::string clean_ws(const std::string& s) 00013 { 00014 const char* ws = " \t\n"; 00015 auto start = s.find_first_not_of(ws); 00016 auto end = s.find_last_not_of(ws); 00017 00018 if(start == std::string::npos) 00019 return ""; 00020 00021 if(end == std::string::npos) 00022 return s.substr(start, end); 00023 else 00024 return s.substr(start, start + end + 1); 00025 } 00026 00027 std::map<std::string, std::string> read_cfg(std::istream& is) 00028 { 00029 std::map<std::string, std::string> kv; 00030 size_t line = 0; 00031 00032 while(is.good()) 00033 { 00034 std::string s; 00035 00036 std::getline(is, s); 00037 00038 ++line; 00039 00040 if(s == "" || s[0] == '#') 00041 continue; 00042 00043 s = clean_ws(s.substr(0, s.find('#'))); 00044 00045 if(s == "") 00046 continue; 00047 00048 auto eq = s.find("="); 00049 00050 if(eq == std::string::npos || eq == 0 || eq == s.size() - 1) 00051 throw std::runtime_error("Bad read_cfg input '" + s + "' on line " + std::to_string(line)); 00052 00053 const std::string key = clean_ws(s.substr(0, eq)); 00054 const std::string val = clean_ws(s.substr(eq + 1, std::string::npos)); 00055 00056 kv[key] = val; 00057 } 00058 00059 return kv; 00060 } 00061 00062 }