1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
8 * Author: Loic Dachary <loic@dachary.org>
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.
17 #include "include/str_map.h"
18 #include "include/str_list.h"
20 #include "json_spirit/json_spirit.h"
27 map<string,string> *str_map,
28 bool fallback_to_plain)
30 json_spirit::mValue json;
32 // try json parsing first
34 json_spirit::read_or_throw(str, json);
36 if (json.type() != json_spirit::obj_type) {
37 ss << str << " must be a JSON object but is of type "
38 << json.type() << " instead";
42 json_spirit::mObject o = json.get_obj();
44 for (map<string, json_spirit::mValue>::iterator i = o.begin();
47 (*str_map)[i->first] = i->second.get_str();
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 ");
59 string trim(const string& str) {
61 size_t end = str.size() - 1;
62 while (isspace(str[start]) != 0 && start <= end) {
65 while (isspace(str[end]) != 0 && start <= end) {
69 return str.substr(start, end - start + 1);
76 map<string,string> *str_map,
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();
86 const string key = trim(i->substr(0, equal));
88 const string value = trim(i->substr(equal));
89 (*str_map)[key] = value;
95 string get_str_map_value(
96 const map<string,string> &str_map,
98 const string *def_val)
100 map<string,string>::const_iterator p = str_map.find(key);
102 // key exists in str_map
103 if (p != str_map.end()) {
104 // but value is empty
105 if (p->second.empty())
107 // and value is not empty
111 // key DNE in str_map and def_val was specified
115 // key DNE in str_map, no def_val was specified
119 string get_str_map_key(
120 const map<string,string> &str_map,
122 const string *fallback_key)
124 map<string,string>::const_iterator p = str_map.find(key);
125 if (p != str_map.end())
128 if (fallback_key != NULL) {
129 p = str_map.find(*fallback_key);
130 if (p != str_map.end())
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(
145 map<string,string> *m,
146 const string &def_key)
148 int r = get_str_map(str, m);
154 if (r >= 0 && m->size() == 1) {
155 map<string,string>::iterator p = m->begin();
156 if (p->second.empty()) {