]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/str_map.cc
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.
19 #include "include/str_map.h"
20 #include "include/str_list.h"
22 #include "json_spirit/json_spirit.h"
29 map
<string
,string
> *str_map
,
30 bool fallback_to_plain
)
32 json_spirit::mValue json
;
34 // try json parsing first
36 json_spirit::read_or_throw(str
, json
);
38 if (json
.type() != json_spirit::obj_type
) {
39 ss
<< str
<< " must be a JSON object but is of type "
40 << json
.type() << " instead";
44 json_spirit::mObject o
= json
.get_obj();
46 for (map
<string
, json_spirit::mValue
>::iterator i
= o
.begin();
49 (*str_map
)[i
->first
] = i
->second
.get_str();
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 ");
61 string
trim(const string
& str
) {
63 size_t end
= str
.size() - 1;
64 while (isspace(str
[start
]) != 0 && start
<= end
) {
67 while (isspace(str
[end
]) != 0 && start
<= end
) {
71 return str
.substr(start
, end
- start
+ 1);
78 map
<string
,string
> *str_map
,
82 get_str_list(str
, delims
, pairs
);
83 for (list
<string
>::iterator i
= pairs
.begin(); i
!= pairs
.end(); ++i
) {
84 size_t equal
= i
->find('=');
85 if (equal
== string::npos
)
86 (*str_map
)[*i
] = string();
88 const string key
= trim(i
->substr(0, equal
));
90 const string value
= trim(i
->substr(equal
));
91 (*str_map
)[key
] = value
;
97 string
get_str_map_value(
98 const map
<string
,string
> &str_map
,
100 const string
*def_val
)
102 map
<string
,string
>::const_iterator p
= str_map
.find(key
);
104 // key exists in str_map
105 if (p
!= str_map
.end()) {
106 // but value is empty
107 if (p
->second
.empty())
109 // and value is not empty
113 // key DNE in str_map and def_val was specified
117 // key DNE in str_map, no def_val was specified
121 string
get_str_map_key(
122 const map
<string
,string
> &str_map
,
124 const string
*fallback_key
)
126 map
<string
,string
>::const_iterator p
= str_map
.find(key
);
127 if (p
!= str_map
.end())
130 if (fallback_key
!= NULL
) {
131 p
= str_map
.find(*fallback_key
);
132 if (p
!= str_map
.end())
138 // This function's only purpose is to check whether a given map has only
139 // ONE key with an empty value (which would mean that 'get_str_map()' read
140 // a map in the form of 'VALUE', without any KEY/VALUE pairs) and, in such
141 // event, to assign said 'VALUE' to a given 'def_key', such that we end up
142 // with a map of the form "m = { 'def_key' : 'VALUE' }" instead of the
143 // original "m = { 'VALUE' : '' }".
144 int get_conf_str_map_helper(
147 map
<string
,string
> *m
,
148 const string
&def_key
)
150 int r
= get_str_map(str
, m
);
156 if (r
>= 0 && m
->size() == 1) {
157 map
<string
,string
>::iterator p
= m
->begin();
158 if (p
->second
.empty()) {