add default json file

This commit is contained in:
xueyan hu
2022-01-21 11:06:10 +08:00
parent 54e1539783
commit b53ce5caf8
3 changed files with 28 additions and 4 deletions

View File

@@ -9,7 +9,7 @@
#include <QApplication> #include <QApplication>
#include "json/cJSON.h" #include "json/cJSON.h"
const char* strProductFileName = "./cfgs/usct-product.json"; const char* strProductFileName = "./cfgs/usct-product.json";
const char* strProductFileNameDefault = "./cfgs/usct-product";
JsonObject::JsonObject() JsonObject::JsonObject()
{ {
loadcfg(); loadcfg();
@@ -243,8 +243,32 @@ bool JsonObject::loadcfg()
{ {
if (m_bLoaded) if (m_bLoaded)
return true; return true;
std::ifstream inFile(strProductFileName); std::ifstream inFile(strProductFileName);
if (!inFile.is_open()) if (!inFile.is_open())
{
return loadcfgDefault();
}
std::stringstream ss;
ss << inFile.rdbuf();
std::string strJsonData = ss.str();
json_root = cJSON_Parse(strJsonData.c_str());
inFile.close();
if (!json_root)
return loadcfgDefault();
m_bLoaded = true;
return true;
}
bool JsonObject::loadcfgDefault()
{
std::ifstream inFile(strProductFileNameDefault);
if (!inFile.is_open())
{ {
return false; return false;
} }
@@ -252,8 +276,8 @@ bool JsonObject::loadcfg()
ss << inFile.rdbuf(); ss << inFile.rdbuf();
std::string strJsonData = ss.str(); std::string strJsonData = ss.str();
json_root = cJSON_Parse(strJsonData.c_str()); json_root = cJSON_Parse(strJsonData.c_str());
inFile.close(); inFile.close();
if (!json_root) if (!json_root)
return false; return false;
@@ -261,7 +285,6 @@ bool JsonObject::loadcfg()
return true; return true;
} }
bool JsonObject::savecfg() bool JsonObject::savecfg()
{ {
if (!m_bLoaded) if (!m_bLoaded)

View File

@@ -110,6 +110,7 @@ private:
bool loadcfg(); bool loadcfg();
bool loadcfgDefault();
bool savecfg(); bool savecfg();
JsonObject(); JsonObject();

View File

@@ -92,7 +92,7 @@ int main(int argc, char* argv[])
redir.moveToThread(&thread); redir.moveToThread(&thread);
QObject::connect(&Timer, SIGNAL(timeout()), &redir, SLOT(readOutsToTF())); QObject::connect(&Timer, SIGNAL(timeout()), &redir, SLOT(readOutsToTF()));
fflush(stdout); fflush(stdout);
Timer.start(10000); Timer.start(1000);
thread.start(); thread.start();