]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com> | |
7 | * | |
8 | * Author: Loic Dachary <loic@dachary.org> | |
9 | * | |
10 | * This library is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2.1 of the License, or (at your option) any later version. | |
14 | * | |
15 | */ | |
16 | ||
7c673cae FG |
17 | #include "include/str_map.h" |
18 | #include "include/str_list.h" | |
19 | ||
a8e16298 TL |
20 | #include <boost/algorithm/string.hpp> |
21 | ||
7c673cae FG |
22 | #include "json_spirit/json_spirit.h" |
23 | ||
24 | using namespace std; | |
25 | ||
26 | int get_json_str_map( | |
27 | const string &str, | |
28 | ostream &ss, | |
29 | map<string,string> *str_map, | |
30 | bool fallback_to_plain) | |
31 | { | |
32 | json_spirit::mValue json; | |
33 | try { | |
34 | // try json parsing first | |
35 | ||
36 | json_spirit::read_or_throw(str, json); | |
37 | ||
38 | if (json.type() != json_spirit::obj_type) { | |
39 | ss << str << " must be a JSON object but is of type " | |
40 | << json.type() << " instead"; | |
41 | return -EINVAL; | |
42 | } | |
43 | ||
44 | json_spirit::mObject o = json.get_obj(); | |
45 | ||
46 | for (map<string, json_spirit::mValue>::iterator i = o.begin(); | |
47 | i != o.end(); | |
48 | ++i) { | |
49 | (*str_map)[i->first] = i->second.get_str(); | |
50 | } | |
51 | } catch (json_spirit::Error_position &e) { | |
52 | if (fallback_to_plain) { | |
53 | // fallback to key=value format | |
54 | get_str_map(str, str_map, "\t\n "); | |
55 | } else { | |
56 | return -EINVAL; | |
57 | } | |
58 | } | |
59 | return 0; | |
60 | } | |
a8e16298 | 61 | |
7c673cae | 62 | string trim(const string& str) { |
a8e16298 TL |
63 | return boost::algorithm::trim_copy_if( |
64 | str, | |
65 | [](unsigned char c) { | |
66 | return std::isspace(c); | |
67 | }); | |
7c673cae FG |
68 | } |
69 | ||
70 | int get_str_map( | |
71 | const string &str, | |
72 | map<string,string> *str_map, | |
73 | const char *delims) | |
74 | { | |
75 | list<string> pairs; | |
76 | get_str_list(str, delims, pairs); | |
77 | for (list<string>::iterator i = pairs.begin(); i != pairs.end(); ++i) { | |
78 | size_t equal = i->find('='); | |
79 | if (equal == string::npos) | |
80 | (*str_map)[*i] = string(); | |
81 | else { | |
82 | const string key = trim(i->substr(0, equal)); | |
83 | equal++; | |
84 | const string value = trim(i->substr(equal)); | |
85 | (*str_map)[key] = value; | |
86 | } | |
87 | } | |
88 | return 0; | |
89 | } | |
90 | ||
91 | string get_str_map_value( | |
92 | const map<string,string> &str_map, | |
93 | const string &key, | |
94 | const string *def_val) | |
95 | { | |
96 | map<string,string>::const_iterator p = str_map.find(key); | |
97 | ||
98 | // key exists in str_map | |
99 | if (p != str_map.end()) { | |
100 | // but value is empty | |
101 | if (p->second.empty()) | |
102 | return p->first; | |
103 | // and value is not empty | |
104 | return p->second; | |
105 | } | |
106 | ||
107 | // key DNE in str_map and def_val was specified | |
108 | if (def_val != NULL) | |
109 | return *def_val; | |
110 | ||
111 | // key DNE in str_map, no def_val was specified | |
112 | return string(); | |
113 | } | |
114 | ||
115 | string get_str_map_key( | |
116 | const map<string,string> &str_map, | |
117 | const string &key, | |
118 | const string *fallback_key) | |
119 | { | |
120 | map<string,string>::const_iterator p = str_map.find(key); | |
121 | if (p != str_map.end()) | |
122 | return p->second; | |
123 | ||
124 | if (fallback_key != NULL) { | |
125 | p = str_map.find(*fallback_key); | |
126 | if (p != str_map.end()) | |
127 | return p->second; | |
128 | } | |
129 | return string(); | |
130 | } | |
131 | ||
132 | // This function's only purpose is to check whether a given map has only | |
133 | // ONE key with an empty value (which would mean that 'get_str_map()' read | |
134 | // a map in the form of 'VALUE', without any KEY/VALUE pairs) and, in such | |
135 | // event, to assign said 'VALUE' to a given 'def_key', such that we end up | |
136 | // with a map of the form "m = { 'def_key' : 'VALUE' }" instead of the | |
137 | // original "m = { 'VALUE' : '' }". | |
138 | int get_conf_str_map_helper( | |
139 | const string &str, | |
140 | ostringstream &oss, | |
141 | map<string,string> *m, | |
142 | const string &def_key) | |
143 | { | |
144 | int r = get_str_map(str, m); | |
145 | ||
146 | if (r < 0) { | |
147 | return r; | |
148 | } | |
149 | ||
150 | if (r >= 0 && m->size() == 1) { | |
151 | map<string,string>::iterator p = m->begin(); | |
152 | if (p->second.empty()) { | |
153 | string s = p->first; | |
154 | m->erase(s); | |
155 | (*m)[def_key] = s; | |
156 | } | |
157 | } | |
158 | return r; | |
159 | } |