[Pkg-bitcoin-commits] [libunivalue] 14/76: Remove use of exceptions. Not all projects use them.
Jonas Smedegaard
dr at jones.dk
Mon Apr 4 09:18:28 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libunivalue.
commit 43af485c4677c551122e2b3b1de3695fa091f1a8
Author: Jeff Garzik <jgarzik at bitpay.com>
Date: Thu Jun 12 22:02:30 2014 -0400
Remove use of exceptions. Not all projects use them.
---
unitester.cpp | 17 ++---------------
univalue.h | 6 +++---
univalue_read.cpp | 33 +++++++++++++++------------------
3 files changed, 20 insertions(+), 36 deletions(-)
diff --git a/unitester.cpp b/unitester.cpp
index 9eb5a33..926fd73 100644
--- a/unitester.cpp
+++ b/unitester.cpp
@@ -26,21 +26,8 @@ static void runtest(string filename, const string& jdata)
bool wantFail = (prefix == "fail");
assert(wantPass || wantFail);
- bool testResult = true;
- try {
- UniValue val;
- val.read(jdata);
- }
- catch (std::exception& e) {
- string strPrint = string("error: ") + e.what();
- fprintf(stderr, "%s\n", strPrint.c_str());
- testResult = false;
- }
- catch (...) {
- string strPrint = string("unknown exception");
- fprintf(stderr, "%s\n", strPrint.c_str());
- testResult = false;
- }
+ UniValue val;
+ bool testResult = val.read(jdata);
if (wantPass) {
assert(testResult == true);
diff --git a/univalue.h b/univalue.h
index f98d5bc..0cbe24b 100644
--- a/univalue.h
+++ b/univalue.h
@@ -62,9 +62,9 @@ public:
std::string write(unsigned int prettyIndent = 0,
unsigned int indentLevel = 0);
- void read(const char *raw);
- void read(std::string rawStr) {
- read(rawStr.c_str());
+ bool read(const char *raw);
+ bool read(std::string rawStr) {
+ return read(rawStr.c_str());
}
private:
diff --git a/univalue_read.cpp b/univalue_read.cpp
index 77db512..428df71 100644
--- a/univalue_read.cpp
+++ b/univalue_read.cpp
@@ -2,8 +2,6 @@
#include <string.h>
#include <vector>
#include <stdio.h>
-#include <cassert>
-#include <stdexcept>
#include "univalue.h"
using namespace std;
@@ -254,7 +252,7 @@ enum tokentype getJsonToken(string& tokenVal, unsigned int& consumed,
}
}
-void UniValue::read(const char *raw)
+bool UniValue::read(const char *raw)
{
clear();
@@ -302,12 +300,12 @@ void UniValue::read(const char *raw)
case TOK_OBJ_CLOSE:
case TOK_ARR_CLOSE: {
if (!stack.size() || expectColon || (last_tok == TOK_COMMA))
- throw runtime_error("json parse: unexpected }]");
+ return false;
VType utyp = (tok == TOK_OBJ_CLOSE ? VOBJ : VARR);
UniValue *top = stack.back();
if (utyp != top->getType())
- throw runtime_error("json parse: mismatched }]");
+ return false;
stack.pop_back();
expectName = false;
@@ -316,11 +314,11 @@ void UniValue::read(const char *raw)
case TOK_COLON: {
if (!stack.size() || expectName || !expectColon)
- throw runtime_error("json parse: : stack empty or want name");
+ return false;
UniValue *top = stack.back();
if (top->getType() != VOBJ)
- throw runtime_error("json parse: : parent not object");
+ return false;
expectColon = false;
break;
@@ -329,7 +327,7 @@ void UniValue::read(const char *raw)
case TOK_COMMA: {
if (!stack.size() || expectName || expectColon ||
(last_tok == TOK_COMMA) || (last_tok == TOK_ARR_OPEN))
- throw runtime_error("json parse: , stack empty or want name");
+ return false;
UniValue *top = stack.back();
if (top->getType() == VOBJ)
@@ -341,14 +339,14 @@ void UniValue::read(const char *raw)
case TOK_KW_TRUE:
case TOK_KW_FALSE: {
if (!stack.size() || expectName || expectColon)
- throw runtime_error("json parse: ntf stack empty or want name");
+ return false;
VType utyp;
switch (tok) {
case TOK_KW_NULL: utyp = VNULL; break;
case TOK_KW_TRUE: utyp = VTRUE; break;
case TOK_KW_FALSE: utyp = VFALSE; break;
- default: assert(0); break;
+ default: /* impossible */ break;
}
UniValue tmpVal(utyp);
@@ -360,7 +358,7 @@ void UniValue::read(const char *raw)
case TOK_NUMBER: {
if (!stack.size() || expectName || expectColon)
- throw runtime_error("json parse digits: stack empty or want name");
+ return false;
UniValue tmpVal(VNUM, tokenVal);
UniValue *top = stack.back();
@@ -371,7 +369,7 @@ void UniValue::read(const char *raw)
case TOK_STRING: {
if (!stack.size())
- throw runtime_error("json parse string: stack empty");
+ return false;
UniValue *top = stack.back();
@@ -388,14 +386,13 @@ void UniValue::read(const char *raw)
}
default:
- throw runtime_error("json parse string: illegal expression");
+ return false;
}
}
- if (stack.size() != 0) {
- char msg[64];
- sprintf(msg, "json parse: too many toplevel obj, %lu", stack.size());
- throw runtime_error(msg);
- }
+ if (stack.size() != 0)
+ return false;
+
+ return true;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/libunivalue.git
More information about the Pkg-bitcoin-commits
mailing list