Repository navigation
Expand file tree
/
Copy pathfile_utils.cpp
More file actions
225 lines (199 loc) · 6.35 KB
/
Copy pathfile_utils.cpp
File metadata and controls
225 lines (199 loc) · 6.35 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
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
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/file_utils.hpp>
#include <wx/filename.h>
#include <wx/dir.h>
#include <errno.h>
#include <sys/stat.h>
#include <boost/range/adaptor/reversed.hpp>
// ----------------------------------------------------------------------------- : File names
String normalize_filename(const String& name) {
wxFileName fn(name);
fn.Normalize();
return fn.GetFullPath();
}
String normalize_internal_filename(const String& name) {
String ret;
for(const auto& c : name) {
if (c==_('\\')) ret += _('/');
else ret += toLower(c);
}
return ret;
}
bool ignore_file(const String& name) {
// Files that are never part of a package,
// i.e. random stuff the OS file manager dumps without being asked
return name == _("Thumbs.db"); // winXP explorer thumbnails
}
String add_extension(const String& filename, String const& extension) {
if (extension.size() <= filename.size() && is_substr(filename, filename.size() - extension.size(), extension)) {
return filename;
} else {
return filename + extension;
}
}
bool is_filename_char(Char c) {
return isAlnum(c) || c == _(' ') || c == _('_') || c == _('-') || c == _('.');
}
String clean_filename(const String& name) {
String clean;
// allow only valid characters, and remove leading whitespace
bool start = true;
for(const auto& c : name) {
if (is_filename_char(c) && !(start && c == _(' '))) {
start = false;
clean += c;
}
}
// remove trailing whitespace
while (!clean.empty() && clean[clean.size()-1] == _(' ')) {
clean.resize(clean.size()-1);
}
if (clean.empty() || starts_with(clean, _("."))) {
clean = _("no-name") + clean;
}
return clean;
}
bool resolve_filename_conflicts(wxFileName& fn, FilenameConflicts conflicts, set<String>& used) {
switch (conflicts) {
case CONFLICT_KEEP_OLD:
return !fn.FileExists();
case CONFLICT_OVERWRITE:
return true;
case CONFLICT_NUMBER: {
int i = 0;
String ext = fn.GetExt();
while(fn.FileExists()) {
fn.SetExt(String() << ++i << _(".") << ext);
}
return true;
}
case CONFLICT_NUMBER_OVERWRITE: {
int i = 0;
String ext = fn.GetExt();
while(used.find(fn.GetFullPath()) != used.end()) {
fn.SetExt(String() << ++i << _(".") << ext);
}
return true;
}
default: {
throw InternalError(_("resolve_filename_conflicts: default case"));
}
}
}
// ----------------------------------------------------------------------------- : File info
time_t file_modified_time(const String& path) {
// Note: wxFileName also provides a function for this, but that is very slow.
struct stat statbuf;
if (stat(path.mb_str(), &statbuf) != 0) {
if (errno == ENOENT) {
return 0;
} else {
throw InternalError(_("could not stat ") + path);
}
}
return statbuf.st_mtime;
}
// ----------------------------------------------------------------------------- : Directories
bool create_parent_dirs(const String& file) {
for (size_t pos = file.find_first_of(_("\\/"), 1) ;
pos != String::npos ;
pos = file.find_first_of(_("\\/"),pos+1)) {
String part = file.substr(0,pos);
if (!wxDirExists(part)) {
if (!wxMkdir(part)) return false;
}
}
return true;
}
// ----------------------------------------------------------------------------- : Removing
class RecursiveDeleter : public wxDirTraverser {
public:
RecursiveDeleter(const String& start) {
to_delete.push_back(start);
ok = true;
}
bool ok;
void remove() {
for(auto& dir : boost::adaptors::reverse(to_delete)) {
if (!wxRmdir(dir)) {
ok = false;
handle_error(_("Cannot delete ") + dir + _("\n")
_("The remainder of the package has still been removed, if possible.\n")
_("Other packages may have been removed, including packages that this on is dependent on. Please remove manually."));
}
}
}
wxDirTraverseResult OnFile(const String& filename) {
if (!wxRemoveFile(filename)) {
ok = false;
handle_error(_("Cannot delete ") + filename + _("\n")
_("The remainder of the package has still been removed, if possible.\n")
_("Other packages may have been removed, including packages that this on is dependent on. Please remove manually."));
}
return wxDIR_CONTINUE;
}
wxDirTraverseResult OnDir(const String& dirname) {
to_delete.push_back(dirname);
return wxDIR_CONTINUE;
}
private:
vector<String> to_delete;
};
bool remove_file_or_dir(const String& name) {
if (wxFileExists(name)) {
return wxRemoveFile(name);
} else if (wxDirExists(name)) {
RecursiveDeleter rd(name);
{
wxDir dir(name);
dir.Traverse(rd);
}
rd.remove();
return rd.ok;
} else {
return true;
}
}
// ----------------------------------------------------------------------------- : Renaming
bool rename_file_or_dir(const String& from, const String& to) {
create_parent_dirs(to);
return wxRenameFile(from, to);
}
// ----------------------------------------------------------------------------- : Moving
class IgnoredMover : public wxDirTraverser {
public:
IgnoredMover(const String& from, const String& to)
: from(from), to(to)
{}
wxDirTraverseResult OnFile(const String& filename) {
tryMove(filename);
return wxDIR_CONTINUE;
}
wxDirTraverseResult OnDir(const String& dirname) {
return tryMove(dirname) ? wxDIR_IGNORE : wxDIR_CONTINUE;
}
private:
String from, to;
bool tryMove(const String& from_path) {
if (is_substr(from_path,0,from)) {
String to_path = to + from_path.substr(from.size());
return rename_file_or_dir(from_path, to_path);
} else {
// This shouldn't happen
return false;
}
}
};
void move_ignored_files(const String& from_dir, const String& to_dir) {
if (wxDirExists(from_dir) && wxDirExists(to_dir)) {
wxDir dir(from_dir);
IgnoredMover im(from_dir, to_dir);
dir.Traverse(im);
}
}