helpers.cpp: Add new functions

- readFile()
 - parseJsonFromFile()
 - parseJsonFromString()

 parseJsonFromString() and parseJsonFromFile() use Json::CharReader
 instead of the obsolete function Json::Reader


Origin commit data
------------------
Branch: ni/coolstream
Commit: 05d8ed4105
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2017-09-19 (Tue, 19 Sep 2017)

Origin message was:
------------------
helpers.cpp: Add new functions

 - readFile()
 - parseJsonFromFile()
 - parseJsonFromString()

 parseJsonFromString() and parseJsonFromFile() use Json::CharReader
 instead of the obsolete function Json::Reader


------------------
This commit was generated by Migit
This commit is contained in:
Michael Liebmann
2017-09-19 21:38:38 +02:00
parent 8296b29529
commit fa80597608
3 changed files with 83 additions and 0 deletions

31
src/system/helpers-json.h Normal file
View File

@@ -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 <json/json.h>
using namespace std;
bool parseJsonFromFile(string& jFile, Json::Value *root, string *errMsg);
bool parseJsonFromString(string& jData, Json::Value *root, string *errMsg);
#endif

View File

@@ -42,6 +42,7 @@
#include <stdarg.h>
#include <algorithm>
#include <mntent.h>
#include <fstream>
#include <linux/hdreg.h>
#include <linux/fs.h>
#include "debug.h"
@@ -49,6 +50,7 @@
#include <driver/fontrenderer.h>
//#include <driver/framebuffer.h>
#include <system/helpers.h>
#include <system/helpers-json.h>
#include <gui/update_ext.h>
#include <libmd5sum.h>
#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;
}

View File

@@ -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