You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.1 KiB
C++
162 lines
4.1 KiB
C++
#include "nmCalculationUtils.h"
|
|
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
#include <QDebug>
|
|
#include <fstream>
|
|
|
|
nmCalculationUtils::nmCalculationUtils() {
|
|
}
|
|
|
|
bool nmCalculationUtils::copyFileToDir(QString filePath, QString destDir) {
|
|
// 如果文件不存在,则退出
|
|
if(!QFile::exists(filePath)) {
|
|
return false;
|
|
}
|
|
|
|
// 如果目标目录不存在,则创建
|
|
if(!QDir(destDir).exists()) {
|
|
if(!QDir().mkdir(destDir)) {
|
|
qWarning() << "cannot mkdir:" << destDir;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 获取源文件的文件名
|
|
QString fileName = QFileInfo(filePath).fileName();
|
|
// 构造目标文件的完整路径
|
|
QString destinationFilePath = QDir(destDir).filePath(fileName);
|
|
|
|
// 如果目标文件已存在,则删除
|
|
if(QFile::exists(destinationFilePath)) {
|
|
QFile destinationFile(destinationFilePath);
|
|
|
|
if(!destinationFile.remove()) {
|
|
qWarning() << "cannot delete file:" << destinationFilePath;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 拷贝文件
|
|
if(QFile::copy(filePath, destinationFilePath)) {
|
|
qDebug() << "file copy success:" << destinationFilePath;
|
|
return true;
|
|
} else {
|
|
qWarning() << "file copy failed:" << destinationFilePath;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool nmCalculationUtils::createDirectoryIfNotExists(QString directoryPath) {
|
|
QDir dir(directoryPath);
|
|
|
|
// 检查目录是否存在
|
|
if(dir.exists()) {
|
|
// 目录已存在
|
|
return true;
|
|
}
|
|
|
|
// 尝试创建目录
|
|
if(dir.mkpath(directoryPath)) {
|
|
// 创建成功
|
|
return true;
|
|
}
|
|
|
|
// 创建失败
|
|
qDebug() << "创建目录失败,错误代码。";
|
|
return false;
|
|
}
|
|
|
|
bool nmCalculationUtils::copyFile(const std::string& sourcePath, const std::string& destinationPath) {
|
|
std::ifstream src(sourcePath, std::ios::binary); // 以二进制模式打开源文件
|
|
std::ofstream dst(destinationPath, std::ios::binary); // 以二进制模式打开目标文件
|
|
|
|
if(!src || !dst) {
|
|
std::cerr << "文件打开失败" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
dst << src.rdbuf(); // 拷贝文件内容
|
|
return true;
|
|
}
|
|
|
|
bool nmCalculationUtils::removeDirectory(const QString &dirPath) {
|
|
QDir dir(dirPath);
|
|
|
|
if(!dir.exists()) {
|
|
qDebug() << "Directory does not exist.";
|
|
return true;
|
|
}
|
|
|
|
// 获取目录中的所有文件和目录
|
|
QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden);
|
|
|
|
foreach(QFileInfo fileInfo, fileList) {
|
|
if(fileInfo.isDir()) {
|
|
// 递归删除子目录
|
|
if(!removeDirectory(fileInfo.absoluteFilePath())) {
|
|
return false;
|
|
}
|
|
} else {
|
|
// 删除文件
|
|
if(!QFile::remove(fileInfo.absoluteFilePath())) {
|
|
qDebug() << "Failed to remove file:" << fileInfo.absoluteFilePath();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 删除目录自身
|
|
if(!dir.rmdir(dirPath)) {
|
|
qDebug() << "Failed to remove directory:" << dirPath;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool nmCalculationUtils::writeFile(const QStringList& content, const QString &filePath) {
|
|
QFile file(filePath);
|
|
|
|
//检查
|
|
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
qWarning("Cannot open file for writing: %s", qPrintable(file.errorString()));
|
|
return false;
|
|
}
|
|
|
|
QTextStream out(&file);
|
|
out << content.join("\n");
|
|
file.close();
|
|
return true;
|
|
}
|
|
|
|
QStringList nmCalculationUtils::readFile(const QString &filePath) {
|
|
QStringList content;
|
|
// 打开文件
|
|
QFile file(filePath);
|
|
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
qDebug() << "Failed to open file:" << filePath;
|
|
return content;
|
|
}
|
|
|
|
// 使用QTextStream读取文件
|
|
QTextStream in(&file);
|
|
|
|
while(!in.atEnd()) {
|
|
// 读取一行
|
|
QString line = in.readLine();
|
|
// 将行内容放入QStringList
|
|
content.append(line);
|
|
}
|
|
|
|
// 关闭文件
|
|
file.close();
|
|
|
|
return content;
|
|
}
|
|
|