forked from iwxyi/EasyMeeting_Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileutil.cpp
229 lines (205 loc) · 5.67 KB
/
fileutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "fileutil.h"
QString readTextFile(QString path)
{
return readTextFile(path, QTextCodec::codecForName(QByteArray("utf-8")));
}
QString readTextFile(QString path, QString codec)
{
return readTextFile(path, QTextCodec::codecForName(QString(codec).toLatin1()));
}
QString readTextFile(QString path, QTextCodec *codec)
{
QString text;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "无法打开文件:" << path;
QMessageBox::critical(nullptr, QObject::tr("错误"), QObject::tr("无法打开文件\n路径:%1").arg(path));
return "";
}
if (!file.isReadable())
{
qDebug() << "无法打开文件:" << path;
QMessageBox::critical(nullptr, QObject::tr("错误"), QObject::tr("该文件不可读\n路径:%1").arg(path));
return "";
}
QTextStream text_stream(&file);
text_stream.setCodec(codec);
while(!text_stream.atEnd())
{
text = text_stream.readAll();
}
file.close();
return text;
}
bool writeTextFile(QString path, QString text)
{
return writeTextFile(path, text, QTextCodec::codecForName(QByteArray("utf-8")));
}
bool writeTextFile(QString path, QString text, QTextCodec *codec)
{
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::critical(nullptr, QObject::tr("保存失败"), QObject::tr("打开文件失败\n路径:%1").arg(path));
return false;
}
QTextStream text_stream(&file);
text_stream.setCodec(codec);
text_stream << text;
file.close();
return true;
}
bool writeTextFile(QString path, QString text, QString codec)
{
return writeTextFile(path, text, QTextCodec::codecForName(QString(codec).toLatin1()));
}
bool copyFile(QString old_path, QString new_path)
{
if (!isFileExist(old_path) || isFileExist(new_path))
return false;
return QFile::copy(old_path, new_path);
}
bool copyFile2(QString old_path, QString new_path)
{
if (!isFileExist(old_path))
return false;
if (isFileExist(new_path))
deleteFile(new_path);
return QFile::copy(old_path, new_path);
}
bool deleteFile(QString path)
{
if (!isFileExist(path)) return true;
return QFile::remove(path);
}
bool renameFile(QString path, QString new_path)
{
if (!isFileExist(path)) return false;
if (isFileExist(new_path)) return false;
return QFile::rename(path, new_path);
}
bool isFileExist(QString path)
{
QFileInfo file_info(path);
return file_info.exists();
}
bool isDir(QString path)
{
QFileInfo file_info(path);
if (!file_info.exists()) // 需要确保存在……
return false;
return file_info.isDir();
}
bool ensureFileExist(QString path)
{
QFileInfo file_info(path);
if (file_info.exists())
{
if (file_info.isDir())
{
QDir dir(path);
dir.rmdir(path); // 删除目录
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) // 创建文件
QMessageBox::critical(NULL, QObject::tr("创建失败"), QObject::tr("创建文件失败\n路径:%1").arg(path));
file.close();
return false;
}
return true;
}
else
{
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) // 创建文件
QMessageBox::critical(NULL, QObject::tr("创建失败"), QObject::tr("创建文件失败\n路径:%1").arg(path));
file.close();
return false;
}
}
bool ensureDirExist(QString path)
{
QFileInfo file_info(path);
if (file_info.exists() && file_info.isFile())
{
QFile file(path);
file.remove(); // 删除文件
QDir dir(path); // 创建目录
return dir.mkpath(path);
}
QDir dir(path);
if (dir.exists())
{
return true;
}
else
{
// dir.mkdir(path) ;// 创建文件夹
return dir.mkpath(path); // 创建多级目录
}
}
QString readExistedTextFile(QString path)
{
ensureFileExist(path);
return readTextFile(path, QTextCodec::codecForName(QByteArray("utf-8")));
}
bool ensureFileNotExist(QString path)
{
QFileInfo file_info(path);
if (file_info.exists())
{
if (file_info.isDir())
{
QDir dir(path);
dir.rmdir(path); // 删除目录
}
else
{
QFile file(path);
file.remove();
}
return false;
}
return true;
}
bool canBeFileName(QString name)
{
// 判断是否包含特殊字符
QChar cs[] = {'\\', '/', ':', '*', '?', '"', '<', '>', '|', '\''};
for (int i = 0; i < 10; i++)
if (name.contains(cs[i]))
return false;
return true;
}
QString getPathWithIndex(QString dir, QString name, QString suffix)
{
int index = 0;
if (!dir.endsWith("/"))
dir += "/";
if (isFileExist(dir+name+suffix))
{
index++;
while(isFileExist(dir+name+QString(" (%1)").arg(index)+suffix))
index++;
}
if (index)
return dir+name+QString("(%1)").arg(index)+suffix;
else
return dir+name+suffix;
}
QString getDirByFile(QString file_path)
{
QFileInfo info(file_path);
if (info.isDir())
return file_path;
int index = file_path.lastIndexOf("/");
if (index == -1)
index = file_path.lastIndexOf("\\");
if (index == -1)
return file_path;
return file_path.left(index); // 不包含末尾的 /
}
void addLinkToDeskTop(const QString& filename,const QString& name)
{
QFile::link(filename, QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("/").append(name+".lnk"));
}