mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Utils: Add file access utils for json and text
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
#include "Json.h"
|
#include "Json.h"
|
||||||
|
#include "Platform.h"
|
||||||
#include "../plugin-macros.generated.h"
|
#include "../plugin-macros.generated.h"
|
||||||
|
|
||||||
bool Utils::Json::JsonArrayIsValidObsArray(json j)
|
bool Utils::Json::JsonArrayIsValidObsArray(json j)
|
||||||
@ -152,3 +153,24 @@ json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault)
|
|||||||
|
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Utils::Json::GetJsonFileContent(std::string fileName, json &content)
|
||||||
|
{
|
||||||
|
std::string textContent;
|
||||||
|
if (!Utils::Platform::GetTextFileContent(fileName, textContent))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
content = json::parse(textContent);
|
||||||
|
} catch (json::parse_error& e) {
|
||||||
|
blog(LOG_WARNING, "Failed to decode content of JSON file `%s`. Error: %s", fileName.c_str(), e.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Utils::Json::SetJsonFileContent(std::string fileName, json content, bool createNew)
|
||||||
|
{
|
||||||
|
std::string textContent = content.dump(2);
|
||||||
|
return Utils::Platform::SetTextFileContent(fileName, textContent, createNew);
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <obs.hpp>
|
#include <obs.hpp>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
@ -10,5 +11,7 @@ namespace Utils {
|
|||||||
bool JsonArrayIsValidObsArray(json j);
|
bool JsonArrayIsValidObsArray(json j);
|
||||||
obs_data_t *JsonToObsData(json j);
|
obs_data_t *JsonToObsData(json j);
|
||||||
json ObsDataToJson(obs_data_t *d, bool includeDefault = false);
|
json ObsDataToJson(obs_data_t *d, bool includeDefault = false);
|
||||||
|
bool GetJsonFileContent(std::string fileName, json &content);
|
||||||
|
bool SetJsonFileContent(std::string fileName, json content, bool createNew = true);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <QNetworkInterface>
|
#include <QNetworkInterface>
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
|
#include <QFile>
|
||||||
#include <obs-frontend-api.h>
|
#include <obs-frontend-api.h>
|
||||||
|
|
||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
@ -98,3 +99,27 @@ void Utils::Platform::SendTrayNotification(QSystemTrayIcon::MessageIcon icon, QS
|
|||||||
delete notification;
|
delete notification;
|
||||||
}, (void*)notification, false);
|
}, (void*)notification, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Utils::Platform::GetTextFileContent(std::string fileName, std::string &content)
|
||||||
|
{
|
||||||
|
QFile f(QString::fromStdString(fileName));
|
||||||
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
content = f.readAll().toStdString();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Utils::Platform::SetTextFileContent(std::string fileName, std::string content, bool createNew)
|
||||||
|
{
|
||||||
|
if (!createNew && !QFile::exists(QString::fromStdString(fileName)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QFile f(QString::fromStdString(fileName));
|
||||||
|
if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QTextStream out(&f);
|
||||||
|
out << content.c_str();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -10,5 +10,7 @@ namespace Utils {
|
|||||||
QString GetCommandLineArgument(QString arg);
|
QString GetCommandLineArgument(QString arg);
|
||||||
bool GetCommandLineFlagSet(QString arg);
|
bool GetCommandLineFlagSet(QString arg);
|
||||||
void SendTrayNotification(QSystemTrayIcon::MessageIcon icon, QString title, QString body);
|
void SendTrayNotification(QSystemTrayIcon::MessageIcon icon, QString title, QString body);
|
||||||
|
bool GetTextFileContent(std::string fileName, std::string &content);
|
||||||
|
bool SetTextFileContent(std::string filePath, std::string content, bool createNew = true);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user