1 #ifndef CONFIG_UTILS_H_ 2 #define CONFIG_UTILS_H_ 6 #include <boost/property_tree/ini_parser.hpp> 9 #include "logging/logging.h" 27 inline T StripQuotesFromStrings(
const T& value);
30 inline std::string StripQuotesFromStrings<std::string>(
const std::string& value) {
31 return Utils::stripQuotes(value);
35 inline T StripQuotesFromStrings(
const T& value) {
40 inline T addQuotesToStrings(
const T& value);
43 inline std::string addQuotesToStrings<std::string>(
const std::string& value) {
44 return Utils::addQuotes(value);
48 inline T addQuotesToStrings(
const T& value) {
53 inline void writeOption(std::ostream& sink,
const T&
data,
const std::string& option_name) {
54 sink << option_name <<
" = " << addQuotesToStrings(data) <<
"\n";
58 inline void CopyFromConfig(T& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
59 boost::optional<T> value = pt.get_optional<T>(option_name);
60 if (value.is_initialized()) {
61 dest = StripQuotesFromStrings(value.get());
66 inline void CopyFromConfig(StorageType& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
67 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
68 if (value.is_initialized()) {
69 std::string storage_type{StripQuotesFromStrings(value.get())};
70 if (storage_type ==
"sqlite") {
71 dest = StorageType::kSqlite;
73 dest = StorageType::kFileSystem;
79 inline void CopyFromConfig(KeyType& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
80 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
81 if (value.is_initialized()) {
82 std::string key_type{StripQuotesFromStrings(value.get())};
83 if (key_type ==
"RSA2048") {
84 dest = KeyType::kRSA2048;
85 }
else if (key_type ==
"RSA3072") {
86 dest = KeyType::kRSA3072;
87 }
else if (key_type ==
"RSA4096") {
88 dest = KeyType::kRSA4096;
89 }
else if (key_type ==
"ED25519") {
90 dest = KeyType::kED25519;
92 dest = KeyType::kUnknown;
98 inline void CopyFromConfig(CryptoSource& dest,
const std::string& option_name,
const boost::property_tree::ptree& pt) {
99 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
100 if (value.is_initialized()) {
101 std::string crypto_source{StripQuotesFromStrings(value.get())};
102 if (crypto_source ==
"pkcs11") {
103 dest = CryptoSource::kPkcs11;
105 dest = CryptoSource::kFile;
111 inline void CopyFromConfig(
utils::BasedPath& dest,
const std::string& option_name,
112 const boost::property_tree::ptree& pt) {
113 boost::optional<std::string> value = pt.get_optional<std::string>(option_name);
114 if (value.is_initialized()) {
120 template <
typename T>
121 inline void CopySubtreeFromConfig(T& dest,
const std::string& subtree_name,
const boost::property_tree::ptree& pt) {
122 auto subtree = pt.get_child_optional(subtree_name);
123 if (subtree.is_initialized()) {
124 dest.updateFromPropertyTree(subtree.get());
127 dest.updateFromPropertyTree(boost::property_tree::ptree());
131 template <
typename T>
132 inline void WriteSectionToStream(T& sec,
const std::string& section_name, std::ostream& os) {
133 os << std::boolalpha;
134 os <<
"[" << section_name <<
"]\n";
135 sec.writeToStream(os);
139 #endif // CONFIG_UTILS_H_
The BasedPath class Can represent an absolute or relative path, only readable through the BasePath::g...