2025-08-20 23:06:28 +08:00
|
|
|
|
#include "ConfigFiles.h"
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
#include <QDebug>
|
2025-09-15 22:28:43 +08:00
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
#include <QSettings>
|
2025-08-20 23:06:28 +08:00
|
|
|
|
ConfigFiles::ConfigFiles(QObject *parent) : QObject(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-15 22:28:43 +08:00
|
|
|
|
tsServerAddr ConfigFiles::ReadServerAddr(){
|
|
|
|
|
|
tsServerAddr SAddr;
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
|
QFileInfo checkFile("./ProgramConfig/IPAddrConfig.ini");
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 创建QSettings对象,指定INI文件路径
|
|
|
|
|
|
QSettings File("./ProgramConfig/IPAddrConfig.ini", 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();
|
|
|
|
|
|
qInfo()<<"读取到的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);
|
|
|
|
|
|
qInfo()<<"写配置文件 地址配置:"<<SAddr.Host<<" 用户名:"<<SAddr.UserName<<" 密码:";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SAddr;
|
|
|
|
|
|
}
|
2025-08-20 23:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @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);
|
2025-09-28 17:14:34 +08:00
|
|
|
|
QFileInfo testFile(filePath);
|
2025-08-20 23:06:28 +08:00
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
|
{
|
2025-09-28 17:14:34 +08:00
|
|
|
|
qWarning() << "CSV 打开失败:" << file.errorString()<<testFile.absoluteFilePath();
|
2025-08-20 23:06:28 +08:00
|
|
|
|
return rows;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTextStream in(&file);
|
2025-09-15 22:28:43 +08:00
|
|
|
|
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
|
|
|
|
|
|
in.setCodec("GBK"); // 如有需要可改成 "GBK"
|
|
|
|
|
|
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
|
|
|
|
|
|
|
|
|
|
|
|
#else // 如果不是Linux也不是Windows
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
return rows;
|
|
|
|
|
|
}
|