Files
EJM_Display/FileOperation/ConfigFiles.cpp.bak
2025-10-20 22:28:37 +08:00

153 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ConfigFiles.h"
#include <QFile>
#include <QDebug>
#include <QFileInfo>
#include <QSettings>
ConfigFiles::ConfigFiles(QObject *parent) : QObject(parent)
{
}
void ConfigFiles::SystemConfig(){
QFileInfo checkFile("./ProgramConfig.ini");
QSettings File("./ProgramConfig.ini", QSettings::IniFormat);
if (checkFile.exists() && checkFile.isFile()) {
ConfigurationPath = File.value("System/ConfigurationPath").toString();
if(isDebug) qDebug()<<"配置文件路径:"<<ConfigurationPath;
}else{
qCritical()<<"未找到系统配置文件路径!";
File.setValue("System/ConfigurationPath","./");
}
QString Path = ConfigurationPath + "系统配置/SystemConfig.ini";
QFileInfo SystemConfigCheck(Path);
QSettings SystemConfig(Path, QSettings::IniFormat);
if (SystemConfigCheck.exists() && SystemConfigCheck.isFile()) {
TrailRefreshTime = SystemConfig.value("System/TrailRefreshTime").toUInt();
RTSP_Url = SystemConfig.value("System/RTSP_Url").toString();
if(isDebug) qDebug()<<"曲线刷新时间(秒):"<<TrailRefreshTime;
}else{
qCritical()<<"未找到曲线刷新时间配置!";
SystemConfig.setValue("System/TrailRefreshTime",10);
}
}
tsServerAddr ConfigFiles::ReadServerAddr(){
tsServerAddr SAddr;
QString Path = ConfigurationPath + "OpcUa配置/OpcUa_ServerConfig.ini";
qDebug()<<"OpcUa服务配置文件路径:"<<Path;
// 检查文件是否存在
QFileInfo checkFile(Path);
// 创建QSettings对象指定INI文件路径
QSettings File(Path, QSettings::IniFormat);
if (checkFile.exists() && checkFile.isFile()) {
// 如果文件已经存在
SAddr.Host = File.value("PLC_Server/Host").toString();
SAddr.UserName = File.value("PLC_Server/UserName").toString();
SAddr.Passwd = File.value("PLC_Server/Password").toString();
qDebug()<<"读取到的PLC 地址配置:"<<SAddr.Host<<" 用户名:"<<SAddr.UserName<<" 密码:"<<SAddr.Passwd;
} else {
// 如果文件不存在
File.setValue("PLC_Server/Host", SAddr.Host);
File.setValue("PLC_Server/UserName", SAddr.UserName);
File.setValue("PLC_Server/Password", SAddr.Passwd);
qDebug()<<"写配置文件 地址配置:"<<SAddr.Host<<" 用户名:"<<SAddr.UserName<<" 密码:";
}
return SAddr;
}
/**
* @brief ReadFile_Csv 读取 CSV 文件,返回行列表
* @param &filePath CSV 文件路径
* @return QList<QStringList> 每个 QStringList 为一行的字段
*/
QList<QStringList> ConfigFiles::ReadFile_Csv(const QString &filePath){
QList<QStringList> rows;
QFile file(filePath);
QFileInfo testFile(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "CSV 打开失败:" << file.errorString()<<testFile.absoluteFilePath();
return rows;
}
QTextStream in(&file);
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
in.setCodec("GBK"); // 如有需要可改成 "GBK"
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
#else // 如果不是Linux也不是Windows
#endif
QStringList currentRow;
QString currentField;
bool inQuote = false;
while (!in.atEnd())
{
QString line = in.readLine();
// 处理跨行引号:如果上一行未闭合,继续拼
if (inQuote)
currentField += "\n";
else
currentField.clear();
for (int i = 0; i < line.size(); ++i)
{
QChar ch = line.at(i);
if (ch == '\"')
{
// 连续两个引号表示转义
if (i + 1 < line.size() && line.at(i + 1) == '\"')
{
currentField += '\"';
++i; // 跳过下一个引号
}
else
{
inQuote = !inQuote;
}
}
else if (ch == ',' && !inQuote)
{
// 字段结束
currentRow << currentField.trimmed();
currentField.clear();
}
else
{
currentField += ch;
}
}
// 如果引号已闭合,把最后一列也加入并结束本行
if (!inQuote)
{
currentRow << currentField.trimmed();
rows << currentRow;
currentRow.clear();
}
}
// 如果文件结尾时引号未闭合,把剩余部分作为最后一列
if (!currentField.isEmpty() || !currentRow.isEmpty())
{
currentRow << currentField.trimmed();
rows << currentRow;
}
file.close();
//qDebug() << "CSV 打开成功:" << file.errorString()<<testFile.absoluteFilePath()<<rows;
return rows;
}