2025-06-25 15:06:42 +08:00
|
|
|
|
#include "PathUtil.h"
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const QString PathUtil::bin_dir()
|
|
|
|
|
{
|
|
|
|
|
static QString s_binDir;
|
|
|
|
|
if (s_binDir.isEmpty())
|
|
|
|
|
s_binDir = QCoreApplication::applicationDirPath() + "/";
|
|
|
|
|
return s_binDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString& PathUtil::data_dir()
|
|
|
|
|
{
|
|
|
|
|
static QString s_dataDir;
|
|
|
|
|
if (s_dataDir.isEmpty())
|
|
|
|
|
s_dataDir = bin_dir() + "data/";
|
|
|
|
|
return s_dataDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathUtil::getEndMark()
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return "\r\n";
|
|
|
|
|
#else
|
|
|
|
|
return "\n";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathUtil::getLogEndLine()
|
|
|
|
|
{
|
|
|
|
|
QString str = getEndMark();
|
|
|
|
|
str += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
|
|
|
|
|
//str +=getEndMark();
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString& PathUtil::config_dir()
|
|
|
|
|
{
|
|
|
|
|
static QString s_configDir;
|
|
|
|
|
if (s_configDir.isEmpty())
|
|
|
|
|
s_configDir = bin_dir() + "config/";
|
|
|
|
|
return s_configDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString& PathUtil::trans_dir()
|
|
|
|
|
{
|
|
|
|
|
static QString s_trDir;
|
|
|
|
|
if (s_trDir.isEmpty())
|
|
|
|
|
s_trDir = bin_dir() + "trans/";
|
|
|
|
|
return s_trDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString PathUtil::get_default_path()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QSettings config_ini(PathUtil::bin_dir() + "config.ini", QSettings::IniFormat);
|
|
|
|
|
QString _defaultpath = config_ini.value("common/path", "").toString();
|
|
|
|
|
QDir dir(_defaultpath);
|
|
|
|
|
if (!dir.exists())
|
|
|
|
|
{
|
|
|
|
|
_defaultpath = PathUtil::bin_dir();
|
|
|
|
|
}
|
|
|
|
|
return _defaultpath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string PathUtil::jsonContainsKeyToString(const json &j ,string strkey)
|
|
|
|
|
{
|
|
|
|
|
if(j.contains(strkey))
|
|
|
|
|
{
|
|
|
|
|
if(j[strkey].is_string())
|
|
|
|
|
{
|
|
|
|
|
return j[strkey].get<std::string>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString PathUtil::jsonContainsKeyToQString(const json &j ,std::string strkey)
|
|
|
|
|
{
|
|
|
|
|
std::string str = PathUtil::jsonContainsKeyToString(j,strkey);
|
|
|
|
|
return QString::fromStdString(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PathUtil::jsonContainsKeyToInt(const json &j ,string strkey)
|
|
|
|
|
{
|
|
|
|
|
if(j.contains(strkey))
|
|
|
|
|
{
|
|
|
|
|
if(j[strkey].is_string())
|
|
|
|
|
{
|
|
|
|
|
string str = j[strkey].get<std::string>();
|
|
|
|
|
|
|
|
|
|
std::stringstream ss(str);
|
|
|
|
|
int iRet;
|
|
|
|
|
ss >> iRet;
|
|
|
|
|
// 检查流状态,判断是否转换成功
|
|
|
|
|
if (ss.fail() || !ss.eof())
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return iRet;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(j[strkey].is_number())
|
|
|
|
|
{
|
|
|
|
|
int iRet = j[strkey].get<int>();
|
|
|
|
|
return iRet;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double PathUtil::jsonContainsKeyToDouble(const json &j ,string strkey)
|
|
|
|
|
{
|
|
|
|
|
if(j.contains(strkey))
|
|
|
|
|
{
|
|
|
|
|
if(j[strkey].is_string())
|
|
|
|
|
{
|
|
|
|
|
string str = j[strkey].get<std::string>();
|
|
|
|
|
double dRet;
|
|
|
|
|
std::stringstream ss(str);
|
|
|
|
|
ss >> dRet;
|
|
|
|
|
// 检查流状态,判断是否转换成功
|
|
|
|
|
if (ss.fail() || !ss.eof())
|
|
|
|
|
{
|
|
|
|
|
return -1.0;
|
|
|
|
|
}
|
|
|
|
|
return dRet;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(j[strkey].is_number())
|
|
|
|
|
{
|
|
|
|
|
double str = j[strkey].get<double>();
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-26 15:07:17 +08:00
|
|
|
|
|
|
|
|
|
void PathUtil::deleteFile(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
QFile file(filePath); // 创建一个 QFile 对象
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
if (file.remove()) {
|
|
|
|
|
//qDebug() << "文件删除成功";
|
|
|
|
|
} else {
|
|
|
|
|
/// qDebug() << "文件删除失败";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//qDebug() << "文件不存在";
|
|
|
|
|
}
|
|
|
|
|
}
|