From a0e3339bf76cc05a849aff0bb3749874af8b3737 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Mon, 1 Apr 2024 19:33:28 +0800 Subject: [PATCH] prevent json file read error --- RedPandaIDE/autolinkmanager.cpp | 4 +++- RedPandaIDE/codesnippetsmanager.cpp | 4 +++- RedPandaIDE/colorscheme.cpp | 4 +++- RedPandaIDE/iconsmanager.cpp | 4 +++- RedPandaIDE/project.cpp | 4 +++- RedPandaIDE/shortcutmanager.cpp | 4 +++- RedPandaIDE/symbolusagemanager.cpp | 4 +++- RedPandaIDE/thememanager.cpp | 4 +++- RedPandaIDE/toolsmanager.cpp | 4 +++- RedPandaIDE/visithistorymanager.cpp | 5 ++++- RedPandaIDE/widgets/ojproblemsetmodel.cpp | 4 +++- 11 files changed, 34 insertions(+), 11 deletions(-) diff --git a/RedPandaIDE/autolinkmanager.cpp b/RedPandaIDE/autolinkmanager.cpp index f2d9864d..7d96cb6f 100644 --- a/RedPandaIDE/autolinkmanager.cpp +++ b/RedPandaIDE/autolinkmanager.cpp @@ -81,7 +81,9 @@ void AutolinkManager::load() #endif } if (file.open(QFile::ReadOnly)) { - QByteArray content = file.readAll(); + QByteArray content = file.readAll().trimmed(); + if (content.isEmpty()) + return; QJsonDocument doc(QJsonDocument::fromJson(content)); fromJson(doc.array()); file.close(); diff --git a/RedPandaIDE/codesnippetsmanager.cpp b/RedPandaIDE/codesnippetsmanager.cpp index 55e46117..2791cac4 100644 --- a/RedPandaIDE/codesnippetsmanager.cpp +++ b/RedPandaIDE/codesnippetsmanager.cpp @@ -82,7 +82,9 @@ void CodeSnippetsManager::loadSnippets() return; } - QByteArray json = file.readAll(); + QByteArray json = file.readAll().trimmed(); + if (json.isEmpty()) + return; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(json,&error); if (error.error != QJsonParseError::NoError) { diff --git a/RedPandaIDE/colorscheme.cpp b/RedPandaIDE/colorscheme.cpp index 8c3c14ae..0c26be21 100644 --- a/RedPandaIDE/colorscheme.cpp +++ b/RedPandaIDE/colorscheme.cpp @@ -68,7 +68,9 @@ PColorScheme ColorScheme::load(const QString &filename) qDebug()< IconsManager::listIconSets() pSet->displayName = pSet->name; QFile infoFile(includeTrailingPathDelimiter(fileInfo.absoluteFilePath())+"info.json"); if (infoFile.exists() && infoFile.open(QFile::ReadOnly)) { - QByteArray content = infoFile.readAll(); + QByteArray content = infoFile.readAll().trimmed(); + if (content.isEmpty()) + return result; QJsonParseError error; QJsonDocument doc(QJsonDocument::fromJson(content,&error)); if (error.error == QJsonParseError::NoError) { diff --git a/RedPandaIDE/project.cpp b/RedPandaIDE/project.cpp index d9191567..57e75786 100644 --- a/RedPandaIDE/project.cpp +++ b/RedPandaIDE/project.cpp @@ -1928,7 +1928,9 @@ QHash Project::loadLayout() QFile file(jsonFilename); if (!file.open(QIODevice::ReadOnly)) return layouts; - QByteArray content = file.readAll(); + QByteArray content = file.readAll().trimmed(); + if (content.isEmpty()) + return layouts; QJsonParseError parseError; QJsonDocument doc(QJsonDocument::fromJson(content,&parseError)); file.close(); diff --git a/RedPandaIDE/shortcutmanager.cpp b/RedPandaIDE/shortcutmanager.cpp index 9d355dc9..0b71d56c 100644 --- a/RedPandaIDE/shortcutmanager.cpp +++ b/RedPandaIDE/shortcutmanager.cpp @@ -46,7 +46,9 @@ void ShortcutManager::load() return; } - QByteArray json = file.readAll(); + QByteArray json = file.readAll().trimmed(); + if (json.isEmpty()) + return; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(json,&error); if (error.error != QJsonParseError::NoError) { diff --git a/RedPandaIDE/symbolusagemanager.cpp b/RedPandaIDE/symbolusagemanager.cpp index 70cc723a..ff6e16bf 100644 --- a/RedPandaIDE/symbolusagemanager.cpp +++ b/RedPandaIDE/symbolusagemanager.cpp @@ -42,7 +42,9 @@ void SymbolUsageManager::load() tr("Can't open symbol usage file '%1' for read.") .arg(filename)); } - QByteArray contents = file.readAll(); + QByteArray contents = file.readAll().trimmed(); + if (contents.isEmpty()) + return; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(contents,&error); if (error.error != QJsonParseError::NoError) { diff --git a/RedPandaIDE/thememanager.cpp b/RedPandaIDE/thememanager.cpp index 9b308f56..baaa7c5d 100644 --- a/RedPandaIDE/thememanager.cpp +++ b/RedPandaIDE/thememanager.cpp @@ -204,7 +204,9 @@ AppTheme::AppTheme(const QString &filename, ThemeType type, QObject *parent):QOb .arg(filename)); } if (file.open(QFile::ReadOnly)) { - QByteArray content = file.readAll(); + QByteArray content = file.readAll().trimmed(); + if (content.isEmpty()) + return; QJsonObject obj; switch (type) { diff --git a/RedPandaIDE/toolsmanager.cpp b/RedPandaIDE/toolsmanager.cpp index 7421dfd1..d3075291 100644 --- a/RedPandaIDE/toolsmanager.cpp +++ b/RedPandaIDE/toolsmanager.cpp @@ -69,7 +69,9 @@ void ToolsManager::load() return; } - QByteArray json = file.readAll(); + QByteArray json = file.readAll().trimmed(); + if (json.isEmpty()) + return; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(json,&error); if (error.error != QJsonParseError::NoError) { diff --git a/RedPandaIDE/visithistorymanager.cpp b/RedPandaIDE/visithistorymanager.cpp index 47f5573a..034fd43e 100644 --- a/RedPandaIDE/visithistorymanager.cpp +++ b/RedPandaIDE/visithistorymanager.cpp @@ -96,8 +96,11 @@ PVisitHistory VisitHistoryManager::doLoad(const QString &filename, qint64 criter QFile file(filename); if (!file.open(QFile::ReadOnly)) return pHistory; + QByteArray content = file.readAll().trimmed(); + if (content.isEmpty()) + return pHistory; QJsonParseError error; - QJsonDocument doc=QJsonDocument::fromJson(file.readAll(),&error); + QJsonDocument doc=QJsonDocument::fromJson(content, &error); if (error.error!=QJsonParseError::NoError) return pHistory; bool ok; diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.cpp b/RedPandaIDE/widgets/ojproblemsetmodel.cpp index 8df08beb..14accbdc 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.cpp +++ b/RedPandaIDE/widgets/ojproblemsetmodel.cpp @@ -174,7 +174,9 @@ void OJProblemSetModel::loadFromFile(const QString &fileName, int& currentIndex) { QFile file(fileName); if (file.open(QFile::ReadOnly)) { - QByteArray content = file.readAll(); + QByteArray content = file.readAll().trimmed(); + if (content.isEmpty()) + return; QJsonParseError error; QJsonDocument doc(QJsonDocument::fromJson(content,&error)); if (error.error!=QJsonParseError::NoError) {