228 lines
4.4 KiB
C++
228 lines
4.4 KiB
C++
#include "QtYaml.h"
|
|
|
|
|
|
#include "yaml-cpp/yaml.h"
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <iosfwd>
|
|
|
|
|
|
YAML::Node QtYaml::LoadFile2(QString fileName) {
|
|
YAML::Node node;
|
|
QFile file(fileName);
|
|
QFileInfo fileinfo(file);
|
|
|
|
if (!fileinfo.exists()) {
|
|
QString strLog = QString("YAML: this file not exist(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return node;
|
|
}
|
|
|
|
if (!fileinfo.isFile()) {
|
|
QString strLog = QString("YAML: this path not a file(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return node;
|
|
}
|
|
|
|
if (fileinfo.size() == 0) {
|
|
QString strLog = QString("YAML: file content is empty(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return node;
|
|
}
|
|
|
|
try {
|
|
bool isOpen = file.open(QIODevice::ReadOnly);
|
|
file.close();
|
|
if (!isOpen) throw file.error();
|
|
}
|
|
catch (const QFileDevice::FileError& err) {
|
|
QString strLog = QString("YAML: file could not be opened(%1):%2.").
|
|
arg(fileName).
|
|
arg(static_cast<int>(err));
|
|
l4qDebug(strLog);
|
|
return node;
|
|
}
|
|
|
|
m_fileName = fileName;
|
|
|
|
try {
|
|
node = YAML::LoadAllFromFile(fileName.toStdString());
|
|
}
|
|
catch (const YAML::Exception& err) {
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return node;
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
bool QtYaml::LoadFile(QString& fileName)
|
|
{
|
|
m_node = YAML::LoadFile(fileName.toStdString());
|
|
|
|
QFile file(fileName);
|
|
QFileInfo fileinfo(file);
|
|
|
|
if (!fileinfo.exists())
|
|
{
|
|
QString strLog = QString("YAML: this file not exist(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
if (!fileinfo.isFile())
|
|
{
|
|
QString strLog = QString("YAML: this path not a file(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
if (fileinfo.size() == 0)
|
|
{
|
|
QString strLog = QString("YAML: file content is empty(%1).").
|
|
arg(fileName);
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
bool isOpen = file.open(QIODevice::ReadOnly);
|
|
file.close();
|
|
if (!isOpen) throw file.error();
|
|
}
|
|
catch (const QFileDevice::FileError& err)
|
|
{
|
|
QString strLog = QString("YAML: file could not be opened(%1):%2.").
|
|
arg(fileName).
|
|
arg(static_cast<int>(err));
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
m_fileName = fileName;
|
|
|
|
try
|
|
{
|
|
m_node = YAML::LoadFile(fileName.toStdString());
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool QtYaml::LoadString(QString& data)
|
|
{
|
|
try
|
|
{
|
|
m_node = YAML::Load(data.toStdString());
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool QtYaml::GetString(std::string sec, std::string key, std::string& s)
|
|
{
|
|
if (!m_node[sec][key].IsScalar())
|
|
return false;
|
|
try
|
|
{
|
|
s = m_node[sec][key].as<std::string>();
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool QtYaml::GetBool(std::string sec, std::string key, bool& b)
|
|
{
|
|
if (!m_node[sec][key].IsScalar())
|
|
return false;
|
|
try
|
|
{
|
|
b = m_node[sec][key].as<bool>();
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool QtYaml::GetInt(std::string sec, std::string key, int& n)
|
|
{
|
|
if (!m_node[sec][key].IsScalar())
|
|
return false;
|
|
try
|
|
{
|
|
n = m_node[sec][key].as<int>();
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool QtYaml::GetDouble(std::string sec, std::string key, double& f)
|
|
{
|
|
if (!m_node[sec][key].IsScalar())
|
|
return false;
|
|
try
|
|
{
|
|
f = m_node[sec][key].as<double>();
|
|
}
|
|
catch (const YAML::Exception& err)
|
|
{
|
|
QString strLog = QString(err.what()).replace("yaml-cpp", "YAML");
|
|
l4qDebug(strLog);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void split(std::vector<std::string>& strings, std::string data) {
|
|
std::istringstream f(data);
|
|
std::string s;
|
|
char sep = ',';
|
|
while (getline(f, s, sep)) {
|
|
strings.emplace_back(s);
|
|
}
|
|
}
|
|
|
|
void split(std::vector<int>& ints, std::string data) {
|
|
std::istringstream f(data);
|
|
std::string s;
|
|
std::vector<std::string> strings;
|
|
char sep = ',';
|
|
while (getline(f, s, sep)) {
|
|
strings.emplace_back(s);
|
|
}
|
|
std::vector<std::string>::iterator it = strings.begin();
|
|
for (it; it != strings.end(); it++) {
|
|
QString str(it->c_str());
|
|
ints.push_back(str.toInt());
|
|
}
|
|
} |