This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial revision of the universal value and JSON library
- Loading branch information
Jeff Garzik
committed
Jun 12, 2014
0 parents
commit 55ef0b0
Showing
13 changed files
with
694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.deps/ | ||
INSTALL | ||
Makefile | ||
Makefile.in | ||
aclocal.m4 | ||
autom4te.cache/ | ||
compile | ||
config.log | ||
config.status | ||
configure | ||
depcomp | ||
install-sh | ||
missing | ||
stamp-h1 | ||
univalue-config.h* | ||
|
||
*.o | ||
libunivalue.a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Jeff Garzik |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See git changelog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
include_HEADERS = univalue.h | ||
|
||
lib_LIBRARIES = libunivalue.a | ||
|
||
libunivalue_a_SOURCES = \ | ||
univalue.cpp \ | ||
univalue_read.cpp \ | ||
univalue_write.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No news is good news. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
univalue library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/sh | ||
set -e | ||
srcdir="$(dirname $0)" | ||
cd "$srcdir" | ||
autoreconf --install --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
AC_INIT([univalue], [0.0.1], | ||
[http://github.com/jgarzik/univalue/]) | ||
|
||
AC_PREREQ(2.52) | ||
AC_CONFIG_SRCDIR([univalue.cpp]) | ||
AM_INIT_AUTOMAKE([gnu]) | ||
AC_CONFIG_HEADERS([univalue-config.h]) | ||
|
||
|
||
AC_PROG_CXX | ||
AC_PROG_CC | ||
AC_PROG_CPP | ||
AC_PROG_CXXCPP | ||
AC_PROG_INSTALL | ||
AC_PROG_OBJC | ||
AC_PROG_LN_S | ||
AC_PROG_MKDIR_P | ||
AC_PROG_SED | ||
AC_PATH_TOOL(AR, ar) | ||
AC_PATH_TOOL(RANLIB, ranlib) | ||
AC_PATH_TOOL(STRIP, strip) | ||
PKG_PROG_PKG_CONFIG | ||
|
||
AC_LANG_PUSH([C++]) | ||
|
||
AC_CONFIG_FILES([Makefile]) | ||
AC_OUTPUT | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
|
||
#include <stdint.h> | ||
#include <ctype.h> | ||
#include <sstream> | ||
#include "univalue.h" | ||
|
||
using namespace std; | ||
|
||
void UniValue::clear() | ||
{ | ||
typ = VNULL; | ||
val.clear(); | ||
keys.clear(); | ||
values.clear(); | ||
} | ||
|
||
bool UniValue::setNull() | ||
{ | ||
clear(); | ||
return true; | ||
} | ||
|
||
bool UniValue::setBool(bool val) | ||
{ | ||
clear(); | ||
typ = (val ? VTRUE : VFALSE); | ||
return true; | ||
} | ||
|
||
static bool validNumStr(const string& s) | ||
{ | ||
bool seenDec = false; | ||
for (unsigned int i = 0; i < s.size(); i++) { | ||
switch (s[i]) { | ||
case '0': | ||
case '1': | ||
case '2': | ||
case '3': | ||
case '4': | ||
case '5': | ||
case '6': | ||
case '7': | ||
case '8': | ||
case '9': | ||
// do nothing | ||
break; | ||
|
||
case '.': | ||
if (seenDec) | ||
return false; | ||
seenDec = true; | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool UniValue::setNumStr(string val_) | ||
{ | ||
if (!validNumStr(val)) | ||
return false; | ||
|
||
clear(); | ||
typ = VNUM; | ||
val = val_; | ||
return true; | ||
} | ||
|
||
bool UniValue::setInt(int64_t val) | ||
{ | ||
string s; | ||
ostringstream oss; | ||
|
||
oss << val; | ||
|
||
return setNumStr(oss.str()); | ||
} | ||
|
||
bool UniValue::setFloat(double val) | ||
{ | ||
string s; | ||
ostringstream oss; | ||
|
||
oss << val; | ||
|
||
return setNumStr(oss.str()); | ||
} | ||
|
||
bool UniValue::setStr(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(UniValue& val) | ||
{ | ||
if (typ != VARR) | ||
return false; | ||
|
||
values.push_back(val); | ||
return true; | ||
} | ||
|
||
bool UniValue::pushKV(string key, UniValue& val) | ||
{ | ||
if (typ != VOBJ) | ||
return false; | ||
|
||
keys.push_back(key); | ||
values.push_back(val); | ||
return true; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef __BITCOIN_UNIVALUE_H__ | ||
#define __BITCOIN_UNIVALUE_H__ | ||
|
||
#include <stdint.h> | ||
#include <string> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
class UniValue { | ||
public: | ||
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VTRUE, VFALSE, }; | ||
|
||
UniValue() { typ = VNULL; } | ||
UniValue(UniValue::VType initialType, const std::string& initialStr = "") { | ||
typ = initialType; | ||
val = initialStr; | ||
} | ||
UniValue(int64_t val_) { | ||
setInt(val_); | ||
} | ||
~UniValue() {} | ||
|
||
void clear(); | ||
|
||
bool setNull(); | ||
bool setBool(bool val); | ||
bool setNumStr(std::string val); | ||
bool setInt(int64_t val); | ||
bool setFloat(double val); | ||
bool setStr(std::string val); | ||
bool setArray(); | ||
bool setObject(); | ||
|
||
enum VType getType() { return typ; } | ||
std::string getValStr() { return val; } | ||
|
||
bool isNull() { return (typ == VNULL); } | ||
bool isTrue() { return (typ == VTRUE); } | ||
bool isFalse() { return (typ == VFALSE); } | ||
bool isBool() { return (typ == VTRUE || typ == VFALSE); } | ||
bool isStr() { return (typ == VSTR); } | ||
bool isNum() { return (typ == VNUM); } | ||
bool isArray() { return (typ == VARR); } | ||
bool isObject() { return (typ == VOBJ); } | ||
|
||
bool push(UniValue& val); | ||
bool push(const std::string& val_) { | ||
UniValue tmpVal(VSTR, val_); | ||
return push(tmpVal); | ||
} | ||
|
||
bool pushKV(std::string key, UniValue& val); | ||
bool pushKV(std::string key, const std::string val) { | ||
UniValue tmpVal(VSTR, val); | ||
return pushKV(key, tmpVal); | ||
} | ||
bool pushKV(std::string key, int64_t val) { | ||
UniValue tmpVal(val); | ||
return pushKV(key, tmpVal); | ||
} | ||
|
||
std::string write(unsigned int prettyIndent = 0, | ||
unsigned int indentLevel = 0); | ||
|
||
void read(const char *raw); | ||
|
||
private: | ||
UniValue::VType typ; | ||
std::string val; // numbers are stored as C++ strings | ||
std::vector<std::string> keys; | ||
std::vector<UniValue> values; | ||
|
||
void writeOpen(unsigned int prettyIndent, unsigned int indentLevel, std::string& s); | ||
void writeClose(unsigned int prettyIndent, unsigned int indentLevel, std::string& s); | ||
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s); | ||
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s); | ||
}; | ||
|
||
#endif // __BITCOIN_UNIVALUE_H__ |
Oops, something went wrong.