]>
Commit | Line | Data |
---|---|---|
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 | ||
17 | #include "include/str_map.h" | |
18 | #include "include/str_list.h" | |
19 | ||
20 | #include "json_spirit/json_spirit.h" | |
21 | ||
22 | using namespace std; | |
23 | ||
24 | int get_json_str_map( | |
25 | const string &str, | |
26 | ostream &ss, | |
27 | map<string,string> *str_map, | |
28 | bool fallback_to_plain) | |
29 | { | |
30 | json_spirit::mValue json; | |
31 | try { | |
32 | // try json parsing first | |
33 | ||
34 | json_spirit::read_or_throw(str, json); | |
35 | ||
36 | if (json.type() != json_spirit::obj_type) { | |
37 | ss << str << " must be a JSON object but is of type " | |
38 | << json.type() << " instead"; | |
39 | return -EINVAL; | |
40 | } | |
41 | ||
42 | json_spirit::mObject o = json.get_obj(); | |
43 | ||
44 | for (map<string, json_spirit::mValue>::iterator i = o.begin(); | |
45 | i != o.end(); | |
46 | ++i) { | |
47 | (*str_map)[i->first] = i->second.get_str(); | |
48 | } | |
49 | } catch (json_spirit::Error_position &e) { | |
50 | if (fallback_to_plain) { | |
51 | // fallback to key=value format | |
52 | get_str_map(str, str_map, "\t\n "); | |
53 | } else { | |
54 | return -EINVAL; | |
55 | } | |
56 | } | |
57 | return 0; | |
58 | } | |
59 | string trim(const string& str) { | |
60 | size_t start = 0; | |
61 | size_t end = str.size() - 1; | |
62 | while (isspace(str[start]) != 0 && start <= end) { | |
63 | ++start; | |
64 | } | |
65 | while (isspace(str[end]) != 0 && start <= end) { | |
66 | --end; | |
67 | } | |
68 | if (start <= end) { | |
69 | return str.substr(start, end - start + 1); | |
70 | } | |
71 | return string(); | |
72 | } | |
73 | ||
74 | int get_str_map( | |
75 | const string &str, | |
76 | map<string,string> *str_map, | |
77 | const char *delims) | |
78 | { | |
79 | list<string> pairs; | |
80 | get_str_list(str, delims, pairs); | |
81 | for (list<string>::iterator i = pairs.begin(); i != pairs.end(); ++i) { | |
82 | size_t equal = i->find('='); | |
83 | if (equal == string::npos) | |
84 | (*str_map)[*i] = string(); | |
85 | else { | |
86 | const string key = trim(i->substr(0, equal)); | |
87 | equal++; | |
88 | const string value = trim(i->substr(equal)); | |
89 | (*str_map)[key] = value; | |
90 | } | |
91 | } | |
92 | return 0; | |
93 | } | |
94 | ||
95 | string get_str_map_value( | |
96 | const map<string,string> &str_map, | |
97 | const string &key, | |
98 | const string *def_val) | |
99 | { | |
100 | map<string,string>::const_iterator p = str_map.find(key); | |
101 | ||
102 | // key exists in str_map | |
103 | if (p != str_map.end()) { | |
104 | // but value is empty | |
105 | if (p->second.empty()) | |
106 | return p->first; | |
107 | // and value is not empty | |
108 | return p->second; | |
109 | } | |
110 | ||
111 | // key DNE in str_map and def_val was specified | |
112 | if (def_val != NULL) | |
113 | return *def_val; | |
114 | ||
115 | // key DNE in str_map, no def_val was specified | |
116 | return string(); | |
117 | } | |
118 | ||
119 | string get_str_map_key( | |
120 | const map<string,string> &str_map, | |
121 | const string &key, | |
122 | const string *fallback_key) | |
123 | { | |
124 | map<string,string>::const_iterator p = str_map.find(key); | |
125 | if (p != str_map.end()) | |
126 | return p->second; | |
127 | ||
128 | if (fallback_key != NULL) { | |
129 | p = str_map.find(*fallback_key); | |
130 | if (p != str_map.end()) | |
131 | return p->second; | |
132 | } | |
133 | return string(); | |
134 | } | |
135 | ||
136 | // This function's only purpose is to check whether a given map has only | |
137 | // ONE key with an empty value (which would mean that 'get_str_map()' read | |
138 | // a map in the form of 'VALUE', without any KEY/VALUE pairs) and, in such | |
139 | // event, to assign said 'VALUE' to a given 'def_key', such that we end up | |
140 | // with a map of the form "m = { 'def_key' : 'VALUE' }" instead of the | |
141 | // original "m = { 'VALUE' : '' }". | |
142 | int get_conf_str_map_helper( | |
143 | const string &str, | |
144 | ostringstream &oss, | |
145 | map<string,string> *m, | |
146 | const string &def_key) | |
147 | { | |
148 | int r = get_str_map(str, m); | |
149 | ||
150 | if (r < 0) { | |
151 | return r; | |
152 | } | |
153 | ||
154 | if (r >= 0 && m->size() == 1) { | |
155 | map<string,string>::iterator p = m->begin(); | |
156 | if (p->second.empty()) { | |
157 | string s = p->first; | |
158 | m->erase(s); | |
159 | (*m)[def_key] = s; | |
160 | } | |
161 | } | |
162 | return r; | |
163 | } |