Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

univalue.cpp
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Copyright 2015 Bitcoin Core Developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <stdint.h>
7 #include <iomanip>
8 #include <sstream>
9 #include <stdlib.h>
10 
11 #include "univalue.h"
12 
13 using namespace std;
14 
16 
18 {
19  typ = VNULL;
20  val.clear();
21  keys.clear();
22  values.clear();
23 }
24 
26 {
27  clear();
28  return true;
29 }
30 
31 bool UniValue::setBool(bool val_)
32 {
33  clear();
34  typ = VBOOL;
35  if (val_)
36  val = "1";
37  return true;
38 }
39 
40 static bool validNumStr(const string& s)
41 {
42  string tokenVal;
43  unsigned int consumed;
44  enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
45  return (tt == JTOK_NUMBER);
46 }
47 
48 bool UniValue::setNumStr(const string& val_)
49 {
50  if (!validNumStr(val_))
51  return false;
52 
53  clear();
54  typ = VNUM;
55  val = val_;
56  return true;
57 }
58 
59 bool UniValue::setInt(uint64_t val_)
60 {
61  ostringstream oss;
62 
63  oss << val_;
64 
65  return setNumStr(oss.str());
66 }
67 
68 bool UniValue::setInt(int64_t val_)
69 {
70  ostringstream oss;
71 
72  oss << val_;
73 
74  return setNumStr(oss.str());
75 }
76 
77 bool UniValue::setFloat(double val_)
78 {
79  ostringstream oss;
80 
81  oss << std::setprecision(16) << val_;
82 
83  bool ret = setNumStr(oss.str());
84  typ = VNUM;
85  return ret;
86 }
87 
88 bool UniValue::setStr(const string& val_)
89 {
90  clear();
91  typ = VSTR;
92  val = val_;
93  return true;
94 }
95 
97 {
98  clear();
99  typ = VARR;
100  return true;
101 }
102 
104 {
105  clear();
106  typ = VOBJ;
107  return true;
108 }
109 
110 bool UniValue::push_back(const UniValue& val_)
111 {
112  if (typ != VARR)
113  return false;
114 
115  values.push_back(val_);
116  return true;
117 }
118 
119 bool UniValue::push_backV(const std::vector<UniValue>& vec)
120 {
121  if (typ != VARR)
122  return false;
123 
124  values.insert(values.end(), vec.begin(), vec.end());
125 
126  return true;
127 }
128 
129 void UniValue::__pushKV(const std::string& key, const UniValue& val_)
130 {
131  keys.push_back(key);
132  values.push_back(val_);
133 }
134 
135 bool UniValue::pushKV(const std::string& key, const UniValue& val_)
136 {
137  if (typ != VOBJ)
138  return false;
139 
140  size_t idx;
141  if (findKey(key, idx))
142  values[idx] = val_;
143  else
144  __pushKV(key, val_);
145  return true;
146 }
147 
148 bool UniValue::pushKVs(const UniValue& obj)
149 {
150  if (typ != VOBJ || obj.typ != VOBJ)
151  return false;
152 
153  for (size_t i = 0; i < obj.keys.size(); i++)
154  __pushKV(obj.keys[i], obj.values.at(i));
155 
156  return true;
157 }
158 
159 void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
160 {
161  if (typ != VOBJ)
162  return;
163 
164  kv.clear();
165  for (size_t i = 0; i < keys.size(); i++)
166  kv[keys[i]] = values[i];
167 }
168 
169 bool UniValue::findKey(const std::string& key, size_t& retIdx) const
170 {
171  for (size_t i = 0; i < keys.size(); i++) {
172  if (keys[i] == key) {
173  retIdx = i;
174  return true;
175  }
176  }
177 
178  return false;
179 }
180 
181 bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
182 {
183  if (typ != VOBJ)
184  return false;
185 
186  for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
187  it != t.end(); ++it) {
188  size_t idx = 0;
189  if (!findKey(it->first, idx))
190  return false;
191 
192  if (values.at(idx).getType() != it->second)
193  return false;
194  }
195 
196  return true;
197 }
198 
199 const UniValue& UniValue::operator[](const std::string& key) const
200 {
201  if (typ != VOBJ)
202  return NullUniValue;
203 
204  size_t index = 0;
205  if (!findKey(key, index))
206  return NullUniValue;
207 
208  return values.at(index);
209 }
210 
211 const UniValue& UniValue::operator[](size_t index) const
212 {
213  if (typ != VOBJ && typ != VARR)
214  return NullUniValue;
215  if (index >= values.size())
216  return NullUniValue;
217 
218  return values.at(index);
219 }
220 
222 {
223  switch (t) {
224  case UniValue::VNULL: return "null";
225  case UniValue::VBOOL: return "bool";
226  case UniValue::VOBJ: return "object";
227  case UniValue::VARR: return "array";
228  case UniValue::VSTR: return "string";
229  case UniValue::VNUM: return "number";
230  }
231 
232  // not reached
233  return NULL;
234 }
235 
236 const UniValue& find_value(const UniValue& obj, const std::string& name)
237 {
238  for (unsigned int i = 0; i < obj.keys.size(); i++)
239  if (obj.keys[i] == name)
240  return obj.values.at(i);
241 
242  return NullUniValue;
243 }
244 
bool setNumStr(const std::string &val)
Definition: univalue.cpp:48
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:221
bool findKey(const std::string &key, size_t &retIdx) const
Definition: univalue.cpp:169
Definition: box.hpp:161
std::vector< UniValue > values
Definition: univalue.h:155
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:148
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:199
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
const char * name
Definition: rest.cpp:36
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:159
jtokentype
Definition: univalue.h:245
bool setFloat(double val)
Definition: univalue.cpp:77
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool setNull()
Definition: univalue.cpp:25
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:181
bool setStr(const std::string &val)
Definition: univalue.cpp:88
UniValue::VType typ
Definition: univalue.h:152
bool setInt(uint64_t val)
Definition: univalue.cpp:59
static bool validNumStr(const string &s)
Definition: univalue.cpp:40
bool setArray()
Definition: univalue.cpp:96
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:236
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:119
bool setObject()
Definition: univalue.cpp:103
bool setBool(bool val)
Definition: univalue.cpp:31
std::vector< std::string > keys
Definition: univalue.h:154
void clear()
Definition: univalue.cpp:17
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:129
const UniValue NullUniValue
Definition: univalue.cpp:15
Released under the MIT license