-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathFileUtils.cpp
151 lines (136 loc) · 3.34 KB
/
FileUtils.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
#include <utils/FileUtils.h>
// qt incl
#include <QDir>
#include <QFileInfo>
// hyperion include
#include <hyperion/Hyperion.h>
namespace FileUtils {
QString getBaseName(const QString& sourceFile)
{
QFileInfo fi(sourceFile);
return fi.fileName();
}
QString getDirName(const QString& sourceFile)
{
QFileInfo fi(sourceFile);
return fi.path();
}
bool removeDir(const QString& path, Logger* log)
{
if(!QDir(path).removeRecursively())
{
Error(log, "Failed to remove directory: %s", QSTRING_CSTR(path));
return false;
}
return true;
}
bool fileExists(const QString& path, Logger* log, bool ignError)
{
if(!QFile::exists(path))
{
ErrorIf((!ignError), log,"File does not exist: %s",QSTRING_CSTR(path));
return false;
}
return true;
}
bool readFile(const QString& path, QString& data, Logger* log, bool ignError)
{
QFile file(path);
if(!fileExists(path, log, ignError))
{
return false;
}
if(!file.open(QFile::ReadOnly | QFile::Text))
{
if(!ignError)
resolveFileError(file,log);
return false;
}
data = QString(file.readAll());
file.close();
return true;
}
bool writeFile(const QString& path, const QByteArray& data, Logger* log)
{
QFile file(path);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
{
resolveFileError(file,log);
return false;
}
if(file.write(data) == -1)
{
resolveFileError(file,log);
return false;
}
file.close();
return true;
}
bool removeFile(const QString& path, Logger* log, bool ignError)
{
QFile file(path);
if(!file.remove())
{
if(!ignError)
resolveFileError(file,log);
return false;
}
return true;
}
void resolveFileError(const QFile& file, Logger* log)
{
QFile::FileError error = file.error();
QByteArray fileNameUtf8 = file.fileName().toUtf8();
const char* fn = fileNameUtf8.constData();
switch(error)
{
case QFileDevice::NoError:
Debug(log,"No error occurred while procesing file: %s",fn);
break;
case QFileDevice::ReadError:
Error(log,"Can't read file: %s",fn);
break;
case QFileDevice::WriteError:
Error(log,"Can't write file: %s",fn);
break;
case QFileDevice::FatalError:
Error(log,"Fatal error while processing file: %s",fn);
break;
case QFileDevice::ResourceError:
Error(log,"Resource Error while processing file: %s",fn);
break;
case QFileDevice::OpenError:
Error(log,"Can't open file: %s",fn);
break;
case QFileDevice::AbortError:
Error(log,"Abort Error while processing file: %s",fn);
break;
case QFileDevice::TimeOutError:
Error(log,"Timeout Error while processing file: %s",fn);
break;
case QFileDevice::UnspecifiedError:
Error(log,"Unspecified Error while processing file: %s",fn);
break;
case QFileDevice::RemoveError:
Error(log,"Failed to remove file: %s",fn);
break;
case QFileDevice::RenameError:
Error(log,"Failed to rename file: %s",fn);
break;
case QFileDevice::PositionError:
Error(log,"Position Error while processing file: %s",fn);
break;
case QFileDevice::ResizeError:
Error(log,"Resize Error while processing file: %s",fn);
break;
case QFileDevice::PermissionsError:
Error(log,"Permission Error at file: %s",fn);
break;
case QFileDevice::CopyError:
Error(log,"Error during file copy of file: %s",fn);
break;
default:
break;
}
}
};