diff --git a/src/system/helpers-json.h b/src/system/helpers-json.h new file mode 100644 index 000000000..0e4923649 --- /dev/null +++ b/src/system/helpers-json.h @@ -0,0 +1,31 @@ +#ifndef __system_helpers_json__ +#define __system_helpers_json__ + +/* + Neutrino-HD + + License: GPL + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include + +using namespace std; + +bool parseJsonFromFile(string& jFile, Json::Value *root, string *errMsg); +bool parseJsonFromString(string& jData, Json::Value *root, string *errMsg); + +#endif diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index cc0f01325..d47485a62 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include "debug.h" @@ -49,6 +50,7 @@ #include //#include #include +#include #include #include #define MD5_DIGEST_LENGTH 16 @@ -1445,3 +1447,51 @@ string readLink(string lnk) return ""; } + +string readFile(string file) +{ + string ret_s; + ifstream tmpData(file.c_str(), ifstream::binary); + if (tmpData.is_open()) { + tmpData.seekg(0, tmpData.end); + int length = tmpData.tellg(); + tmpData.seekg(0, tmpData.beg); + char* buffer = new char[length+1]; + tmpData.read(buffer, length); + tmpData.close(); + buffer[length] = '\0'; + ret_s = (string)buffer; + delete [] buffer; + } + else { + cerr << "Error read " << file << endl; + return ""; + } + + return ret_s; +} + +bool parseJsonFromFile(string& jFile, Json::Value *root, string *errMsg) +{ + string jData = readFile(jFile); + bool ret = parseJsonFromString(jData, root, errMsg); + jData.clear(); + return ret; +} + +bool parseJsonFromString(string& jData, Json::Value *root, string *errMsg) +{ + Json::CharReaderBuilder builder; + Json::CharReader* reader(builder.newCharReader()); + JSONCPP_STRING errs = ""; + const char* jData_c = jData.c_str(); + + bool ret = reader->parse(jData_c, jData_c + strlen(jData_c), root, &errs); + if (!ret || (!errs.empty())) { + ret = false; + if (errMsg != NULL) + *errMsg = errs; + } + delete reader; + return ret; +} diff --git a/src/system/helpers.h b/src/system/helpers.h index 7bd7afb2d..910f9ea3c 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -152,4 +152,6 @@ std::string filehash(const char * file); std::string get_path(const char * path); inline bool file_exists(const std::string file) { return file_exists(file.c_str()); } +std::string readFile(std::string file); + #endif