mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-13 08:58:52 +00:00
a44caf65fe Merge bitcoin-core/univalue-subtree#28: Import fixes for sanitizer reported issues 135254331e Import fixes for sanitizer reported issues d5fb86940e refactor: use c++11 range based for loop in checkObject ff9c379304 refactor: Use nullptr (c++11) instead of NULL 08a99754d5 build: use ax_cxx_compile_stdcxx.m4 to check for C++11 support 66d3713ce7 Merge bitcoin-core/univalue#29: ci: travis -> cirrus 808d487292 ci: travis -> cirrus c390ac375f Merge bitcoin-core/univalue#19: Split sources for easier buildsystem integration 4a5b0a1c65 build: Move source entries out to sources.mk 6c7d94b33c build: cleanup wonky gen usage a222637c6d Merge #23: Merge changes from jgarzik/univalue@1ae6a23 f77d0f718d Merge commit '1ae6a231a0169938eb3972c1d48dd17cba5947e1' into HEAD 1ae6a231a0 Merge pull request #57 from MarcoFalke/test_fix 92bdd11f0b univalue_write: remove unneeded sstream.h include ffb621c130 Merge pull request #56 from drodil/remove_sstream_header f33acf9fe8 Merge commit '7890db9~' into HEAD 66e0adec4d Remove unnecessary sstream header from univalue.h 88967f6586 Version 1.0.4 1dc113dbef Merge pull request #50 from luke-jr/pushKV_bool 72392fb227 [tests] test pushKV for boolean values c23132bcf4 Pushing boolean value to univalue correctly 81faab26a1 Merge pull request #48 from fwolfst/47-UPDATE_MIT_LINK_TO_HTTPS b17634ef24 Update URLs to MIT license. 88ab64f6b5 Merge pull request #46 from jasonbcox/master 35ed96da31 Merge pull request #44 from MarcoFalke/Mf1709-univalue-cherrypick-explicit 420c226290 Merge pull request #45 from MarcoFalke/Mf1710-univalue-revert-test git-subtree-dir: src/univalue git-subtree-split: a44caf65fe55b9dd8ddb08f04c0f70409efd53b3
245 lines
4.4 KiB
C++
245 lines
4.4 KiB
C++
// Copyright 2014 BitPay Inc.
|
|
// Copyright 2015 Bitcoin Core Developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or https://opensource.org/licenses/mit-license.php.
|
|
|
|
#include <stdint.h>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <stdlib.h>
|
|
|
|
#include "univalue.h"
|
|
|
|
const UniValue NullUniValue;
|
|
|
|
void UniValue::clear()
|
|
{
|
|
typ = VNULL;
|
|
val.clear();
|
|
keys.clear();
|
|
values.clear();
|
|
}
|
|
|
|
bool UniValue::setNull()
|
|
{
|
|
clear();
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::setBool(bool val_)
|
|
{
|
|
clear();
|
|
typ = VBOOL;
|
|
if (val_)
|
|
val = "1";
|
|
return true;
|
|
}
|
|
|
|
static bool validNumStr(const std::string& s)
|
|
{
|
|
std::string tokenVal;
|
|
unsigned int consumed;
|
|
enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
|
|
return (tt == JTOK_NUMBER);
|
|
}
|
|
|
|
bool UniValue::setNumStr(const std::string& val_)
|
|
{
|
|
if (!validNumStr(val_))
|
|
return false;
|
|
|
|
clear();
|
|
typ = VNUM;
|
|
val = val_;
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::setInt(uint64_t val_)
|
|
{
|
|
std::ostringstream oss;
|
|
|
|
oss << val_;
|
|
|
|
return setNumStr(oss.str());
|
|
}
|
|
|
|
bool UniValue::setInt(int64_t val_)
|
|
{
|
|
std::ostringstream oss;
|
|
|
|
oss << val_;
|
|
|
|
return setNumStr(oss.str());
|
|
}
|
|
|
|
bool UniValue::setFloat(double val_)
|
|
{
|
|
std::ostringstream oss;
|
|
|
|
oss << std::setprecision(16) << val_;
|
|
|
|
bool ret = setNumStr(oss.str());
|
|
typ = VNUM;
|
|
return ret;
|
|
}
|
|
|
|
bool UniValue::setStr(const std::string& val_)
|
|
{
|
|
clear();
|
|
typ = VSTR;
|
|
val = val_;
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::setArray()
|
|
{
|
|
clear();
|
|
typ = VARR;
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::setObject()
|
|
{
|
|
clear();
|
|
typ = VOBJ;
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::push_back(const UniValue& val_)
|
|
{
|
|
if (typ != VARR)
|
|
return false;
|
|
|
|
values.push_back(val_);
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::push_backV(const std::vector<UniValue>& vec)
|
|
{
|
|
if (typ != VARR)
|
|
return false;
|
|
|
|
values.insert(values.end(), vec.begin(), vec.end());
|
|
|
|
return true;
|
|
}
|
|
|
|
void UniValue::__pushKV(const std::string& key, const UniValue& val_)
|
|
{
|
|
keys.push_back(key);
|
|
values.push_back(val_);
|
|
}
|
|
|
|
bool UniValue::pushKV(const std::string& key, const UniValue& val_)
|
|
{
|
|
if (typ != VOBJ)
|
|
return false;
|
|
|
|
size_t idx;
|
|
if (findKey(key, idx))
|
|
values[idx] = val_;
|
|
else
|
|
__pushKV(key, val_);
|
|
return true;
|
|
}
|
|
|
|
bool UniValue::pushKVs(const UniValue& obj)
|
|
{
|
|
if (typ != VOBJ || obj.typ != VOBJ)
|
|
return false;
|
|
|
|
for (size_t i = 0; i < obj.keys.size(); i++)
|
|
__pushKV(obj.keys[i], obj.values.at(i));
|
|
|
|
return true;
|
|
}
|
|
|
|
void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
|
|
{
|
|
if (typ != VOBJ)
|
|
return;
|
|
|
|
kv.clear();
|
|
for (size_t i = 0; i < keys.size(); i++)
|
|
kv[keys[i]] = values[i];
|
|
}
|
|
|
|
bool UniValue::findKey(const std::string& key, size_t& retIdx) const
|
|
{
|
|
for (size_t i = 0; i < keys.size(); i++) {
|
|
if (keys[i] == key) {
|
|
retIdx = i;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
|
|
{
|
|
if (typ != VOBJ) {
|
|
return false;
|
|
}
|
|
|
|
for (const auto& object: t) {
|
|
size_t idx = 0;
|
|
if (!findKey(object.first, idx)) {
|
|
return false;
|
|
}
|
|
|
|
if (values.at(idx).getType() != object.second) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const UniValue& UniValue::operator[](const std::string& key) const
|
|
{
|
|
if (typ != VOBJ)
|
|
return NullUniValue;
|
|
|
|
size_t index = 0;
|
|
if (!findKey(key, index))
|
|
return NullUniValue;
|
|
|
|
return values.at(index);
|
|
}
|
|
|
|
const UniValue& UniValue::operator[](size_t index) const
|
|
{
|
|
if (typ != VOBJ && typ != VARR)
|
|
return NullUniValue;
|
|
if (index >= values.size())
|
|
return NullUniValue;
|
|
|
|
return values.at(index);
|
|
}
|
|
|
|
const char *uvTypeName(UniValue::VType t)
|
|
{
|
|
switch (t) {
|
|
case UniValue::VNULL: return "null";
|
|
case UniValue::VBOOL: return "bool";
|
|
case UniValue::VOBJ: return "object";
|
|
case UniValue::VARR: return "array";
|
|
case UniValue::VSTR: return "string";
|
|
case UniValue::VNUM: return "number";
|
|
}
|
|
|
|
// not reached
|
|
return nullptr;
|
|
}
|
|
|
|
const UniValue& find_value(const UniValue& obj, const std::string& name)
|
|
{
|
|
for (unsigned int i = 0; i < obj.keys.size(); i++)
|
|
if (obj.keys[i] == name)
|
|
return obj.values.at(i);
|
|
|
|
return NullUniValue;
|
|
}
|
|
|