-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_op.cpp
More file actions
68 lines (58 loc) · 1.72 KB
/
file_op.cpp
File metadata and controls
68 lines (58 loc) · 1.72 KB
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
#include "file_op.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
namespace cf::desktop::settings::path {
static constexpr const char* DESKTOP_ROOT = "cfdesktop";
static constexpr const char* DESKTOP = "desktop";
static constexpr const char* RUNTIME = "runtime";
} // namespace cf::desktop::settings::path
namespace cf::desktop::base::filesystem {
// create file or dirent anyway
bool create_anyway(const QString& path) {
if (path.isEmpty()) {
return false;
}
QFileInfo info(path);
if (info.exists()) {
return true;
}
// Create parent directories if they don't exist
QDir dir;
if (!dir.mkpath(info.absolutePath())) {
return false;
}
// Determine if it's a file or directory based on extension
// If path ends with '/', it's a directory
if (path.endsWith('/') || path.endsWith(QDir::separator())) {
return dir.mkdir(path);
}
// If the path has an extension (contains '.' after the last separator), treat as file
// Otherwise, treat as directory
QString fileName = info.fileName();
if (fileName.contains('.')) {
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
file.close();
return true;
}
return false;
} else {
return dir.mkdir(path);
}
}
QString app_runtime_dir() {
return QCoreApplication::applicationDirPath();
}
QString concat_filepath(const QString& dirent, const QString& rest_dir) {
return QDir(dirent).filePath(rest_dir);
}
bool exsited(const QString& path) {
if (path.isEmpty()) {
return false;
}
return QFileInfo::exists(path);
}
} // namespace cf::desktop::base::filesystem