RasterProcessTool/BaseCommonLibrary/BaseTool/FileOperator.cpp

262 lines
6.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "stdafx.h"
#include "FileOperator.h"
#include <boost/filesystem.hpp>
#include <string>
#include <memory.h>
#include <memory>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <QDebug>
#include <QString>
#include <QMessageBox>
QString addMaskToFileName(const QString& filePath,QString _zzname) {
// 获取文件路径和文件名
QFileInfo fileInfo(filePath);
// 获取文件名和扩展名
QString baseName = fileInfo.baseName(); // 不包含扩展名的文件名
QString extension = fileInfo.suffix(); // 文件扩展名(例如 ".txt", ".jpg"
// 拼接新的文件名
QString newFileName = baseName + _zzname; // 在文件名中增加 "_mask"
if (!extension.isEmpty()) {
newFileName += "." + extension; // 如果有扩展名,添加扩展名
}
// 返回新的文件路径
QString newFilePath = fileInfo.path() + "/" + newFileName;
return newFilePath;
}
std::vector<QString> getFilelist(const QString& folderpath, const QString& filenameExtension, int (*logfun)(QString logtext, int value))
{
QString filenameExtensionStr = filenameExtension;
filenameExtensionStr = filenameExtensionStr.remove(0, 1);
std::vector<QString> filenames(0);
if (isExists(folderpath) && isDirectory(folderpath)) {
QDir directory(folderpath);
if (directory.exists() && directory.isReadable()) {
QFileInfoList fileList = directory.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
for (const QFileInfo& fileInfo : fileList) {
// qDebug() << fileInfo.filePath() + "\nExtension: (" + filenameExtensionStr + ", " + fileInfo.suffix() + ")";
if (filenameExtensionStr == u8"*" || filenameExtensionStr == fileInfo.suffix()) {
filenames.push_back(fileInfo.filePath());
}
if (logfun) {
logfun(fileInfo.filePath() + "\nExtension: (" + filenameExtensionStr + ", " + fileInfo.suffix() + ")", 4);
}
}
}
else {
qWarning() << "Folder does not exist or is not readable: " << folderpath;
}
return filenames;
}
else {
return std::vector<QString>(0);
}
}
QString getParantFolderNameFromPath(const QString& path)
{
QDir directory(path);
directory.cdUp();
QString parentPath = directory.absolutePath();
return directory.dirName();
}
QString getParantFromPath(const QString& path)
{
//qDebug() << path;
QDir directory(path);
directory.cdUp();
QString parentPath = directory.absolutePath();
//qDebug() << parentPath;
return parentPath;
}
QString getFileNameFromPath(const QString& path)
{
QFileInfo fileInfo(path);
return fileInfo.fileName();
}
QString getFileNameWidthoutExtend(QString path)
{
QFileInfo fileInfo(path);
QString fileNameWithoutExtension = fileInfo.completeBaseName(); // 获取无后缀文件名
qDebug() << u8"File name without extension: " << fileNameWithoutExtension;
return fileNameWithoutExtension;
}
QString BASECONSTVARIABLEAPI getFileExtension(QString path)
{
QFileInfo fileInfo(path);
QString fileExtension = fileInfo.suffix(); // 获取无后缀文件名
return fileExtension;
}
bool isDirectory(const QString& path)
{
QFileInfo fileinfo(path);
return fileinfo.isDir();
}
bool isExists(const QString& path)
{
QFileInfo fileinfo(path);
return fileinfo.exists();
}
bool isFile(const QString& path)
{
QFileInfo fileinfo(path);
return fileinfo.isFile();
}
int write_binfile(char* filepath, char* data, size_t data_len)
{
FILE* pd = fopen(filepath, "w");
if (NULL == pd) {
return 2;
}
//数据块首地址: "&a",元素大小: "sizeof(unsigned __int8)" 元素个数: "10" 文件指针:"pd"
fwrite(data, sizeof(char), data_len, pd);
fclose(pd);
return -1;
}
char* read_textfile(char* text_path, int* length)
{
char* data = NULL;
FILE* fp1 = fopen(text_path, "r");
if (fp1 == NULL) {
return NULL;
}
else {}
// 读取文件
fseek(fp1, 0, SEEK_END);
int data_length = ftell(fp1);
data = (char*)malloc((data_length + 1) * sizeof(char));
rewind(fp1);
if (data_length == fread(data, sizeof(char), data_length, fp1)) {
data[data_length] = '\0'; // 文件尾
}
else {
free(data);
fclose(fp1);
return NULL;
}
fclose(fp1);
*length = data_length + 1;
return data;
}
bool exists_test(const QString& name)
{
return isExists(name);
}
size_t fsize(FILE* fp)
{
size_t n;
fpos_t fpos; // 当前位置
fgetpos(fp, &fpos); // 获取当前位置
fseek(fp, 0, SEEK_END);
n = ftell(fp);
fsetpos(fp, &fpos); // 恢复之前的位置
return n;
}
void removeFile(const QString& filePath)
{
QFile file(filePath);
if (file.exists()) {
if (file.remove()) {
qDebug() << "File removed successfully: " << filePath;
}
else {
qWarning() << "Failed to remove file: " << filePath;
}
}
else {
qDebug() << "File does not exist: " << filePath;
}
}
unsigned long convertToULong(const QString& input) {
bool ok; // Used to check if the conversion was successful
unsigned long result = input.toULong(&ok);
if (!ok) {
qWarning() << "Conversion to unsigned long failed for input: " << input;
}
return result;
}
void copyFile(const QString& sourcePath, const QString& destinationPath) {
QFile sourceFile(sourcePath);
QFile destinationFile(destinationPath);
qDebug() << QString("copy file ready !! from ") + sourcePath+" to "+destinationPath ;
if (sourceFile.exists()) {
if (sourceFile.copy(destinationPath)) {
qDebug() << QString("copy file sucessfully !! from ") + sourcePath+" to "+destinationPath ;
// 复制成功
//QMessageBox::information(nullptr, u8"成功", u8"文件复制成功");
}
else {
// 复制失败
if(sourceFile.exists()){
QMessageBox::critical(nullptr, QObject::tr("error"), QObject::tr("The file already exists !!"));
}
else{
QMessageBox::critical(nullptr, QObject::tr("error"), QObject::tr("file copy error"));
}
}
}
else {
// 源文件不存在
QMessageBox::warning(nullptr, QObject::tr("warning"), QObject::tr("Source file not found"));
}
}
bool copyAndReplaceFile(const QString& sourceFilePath, const QString& destinationFilePath) {
// 检查源文件是否存在
if (!QFile::exists(sourceFilePath)) {
qDebug() << "Source file does not exist:" << sourceFilePath;
return false;
}
// 如果目标文件存在,则删除它
if (QFile::exists(destinationFilePath)) {
if (!QFile::remove(destinationFilePath)) {
qDebug() << "Failed to remove existing destination file:" << destinationFilePath;
return false;
}
}
// 复制文件
if (!QFile::copy(sourceFilePath, destinationFilePath)) {
qDebug() << "Failed to copy file from" << sourceFilePath << "to" << destinationFilePath;
return false;
}
qDebug() << "File copied successfully from" << sourceFilePath << "to" << destinationFilePath;
return true;
}