-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.cpp
More file actions
146 lines (130 loc) · 3.9 KB
/
FileUtil.cpp
File metadata and controls
146 lines (130 loc) · 3.9 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
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
#include "FileUtil.h"
#include <array>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
using namespace std;
namespace fs = std::filesystem;
bool FileUtil::MirrorDirectory(const fs::path &src, const fs::path &dst,
const vector<string> &excludes,
function<void(const string &)> logger) {
try {
if (!fs::exists(src)) {
if (logger)
logger("Sync Error: Source does not exist: " + src.string());
return false;
}
if (!fs::exists(dst))
fs::create_directories(dst);
for (const auto &entry : fs::recursive_directory_iterator(src)) {
const auto &path = entry.path();
string relPath = fs::relative(path, src).string();
// Robust exclusion check: Match prefix or substring
bool skip = false;
for (const auto &pattern : excludes) {
if (pattern.empty())
continue;
if (relPath == pattern ||
relPath.compare(0, pattern.length(), pattern) == 0 ||
relPath.find("/" + pattern) != string::npos) {
skip = true;
break;
}
}
if (skip)
continue;
auto destPath = dst / relPath;
if (entry.is_directory()) {
fs::create_directories(destPath);
} else if (entry.is_regular_file()) {
fs::copy_file(path, destPath, fs::copy_options::overwrite_existing);
}
}
} catch (const exception &e) {
if (logger)
logger("Sync Error: " + string(e.what()));
return false;
}
return true;
}
bool FileUtil::StripLines(const fs::path &filePath, int startLine,
int endLine) {
try {
ifstream ifs(filePath);
if (!ifs.is_open())
return false;
vector<string> lines;
string line;
int currentLine = 1;
while (getline(ifs, line)) {
if (currentLine < startLine || currentLine > endLine) {
lines.push_back(line);
}
currentLine++;
}
ifs.close();
ofstream ofs(filePath, ios::trunc);
for (const auto &l : lines) {
ofs << l << "\n";
}
return true;
} catch (...) {
return false;
}
}
bool FileUtil::ZipDirectory(const fs::path &dir, const fs::path &zipFile) {
// Single quotes for shell safety on Mac/Linux
string cmd =
"cd \"" + dir.string() + "\" && zip -rq \"" + zipFile.string() + "\" .";
#ifdef _WIN32
cmd = "powershell -Command \"Compress-Archive -Path '" + dir.string() +
"/*' -DestinationPath '" + zipFile.string() + "' -Force\"";
#endif
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe)
return false;
return pclose(pipe) == 0;
}
vector<string> FileUtil::ListDirectories(const fs::path &root) {
vector<string> dirs;
if (!fs::exists(root) || !fs::is_directory(root))
return dirs;
for (const auto &entry : fs::directory_iterator(root)) {
if (entry.is_directory() && entry.path().filename().string()[0] != '.') {
dirs.push_back(entry.path().filename().string());
}
}
return dirs;
}
bool FileUtil::MergeDirectory(const fs::path &src, const fs::path &dst,
function<void(const string &)> logger) {
try {
if (!fs::exists(src))
return false;
if (!fs::exists(dst))
fs::create_directories(dst);
for (const auto &entry : fs::recursive_directory_iterator(src)) {
const auto &path = entry.path();
auto rel = fs::relative(path, src);
auto destPath = dst / rel;
if (entry.is_directory()) {
fs::create_directories(destPath);
} else if (entry.is_regular_file()) {
fs::create_directories(destPath.parent_path());
fs::copy_file(path, destPath, fs::copy_options::overwrite_existing);
}
}
return true;
} catch (const exception &e) {
if (logger)
logger("Merge Error: " + string(e.what()));
return false;
}
}