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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* File: filelib.cpp
* -----------------
* This file implements the filelib.h interface. All platform dependencies
* are managed through the platform interface.
*/
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "filelib.h"
#include "foreach.h"
#include "platform.h"
#include "strlib.h"
#include "vector.h"
using namespace std;
static Platform *pp = getPlatform();
/* Prototypes */
static void splitPath(string path, Vector<string> list);
static bool recursiveMatch(string str, int sx, string pattern, int px);
/* Implementations */
bool openFile(ifstream & stream, string filename) {
stream.clear();
stream.open(expandPathname(filename).c_str());
return !stream.fail();
}
bool openFile(ofstream & stream, string filename) {
stream.clear();
stream.open(expandPathname(filename).c_str());
return !stream.fail();
}
string promptUserForFile(ifstream & stream, string prompt) {
while (true) {
cout << prompt;
string filename;
getline(cin, filename);
openFile(stream, filename);
if (!stream.fail()) return filename;
stream.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt = "Input file: ";
}
}
string promptUserForFile(ofstream & stream, string prompt) {
while (true) {
cout << prompt;
string filename;
getline(cin, filename);
openFile(stream, filename);
if (!stream.fail()) return filename;
stream.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt = "Output file: ";
}
}
string openFileDialog(ifstream & stream) {
return openFileDialog(stream, "Open File", "");
}
string openFileDialog(ifstream & stream, string title) {
return openFileDialog(stream, title, "");
}
string openFileDialog(ifstream & stream, string title, string path) {
string filename = pp->openFileDialog(title, "load", path);
if (filename == "") return "";
stream.open(filename.c_str());
return (stream.fail()) ? "" : filename;
}
string openFileDialog(ofstream & stream) {
return openFileDialog(stream, "Open File", "");
}
string openFileDialog(ofstream & stream, string title) {
return openFileDialog(stream, title, "");
}
string openFileDialog(ofstream & stream, string title, string path) {
string filename = pp->openFileDialog(title, "save", path);
if (filename == "") return "";
stream.open(filename.c_str());
return (stream.fail()) ? "" : filename;
}
void readEntireFile(istream & is, Vector<string> & lines) {
lines.clear();
while (true) {
string line;
getline(is, line);
if (is.fail()) break;
lines.add(line);
}
}
void readEntireFile(istream & is, vector<string> & lines) {
lines.clear();
while (true) {
string line;
getline(is, line);
if (is.fail()) break;
lines.push_back(line);
}
}
string getRoot(string filename) {
int dot = -1;
int len = filename.length();
for (int i = 0; i < len; i++) {
char ch = filename[i];
if (ch == '.') dot = i;
if (ch == '/' || ch == '\\') dot = -1;
}
if (dot == -1) {
return filename;
} else {
return filename.substr(0, dot);
}
}
string getExtension(string filename) {
int dot = -1;
int len = filename.length();
for (int i = 0; i < len; i++) {
char ch = filename[i];
if (ch == '.') dot = i;
if (ch == '/' || ch == '\\') dot = -1;
}
if (dot == -1) {
return "";
} else {
return filename.substr(dot);
}
}
string getHead(string filename) {
int slash = -1;
int len = filename.length();
for (int i = 0; i < len; i++) {
char ch = filename[i];
if (ch == '/' || ch == '\\') slash = i;
}
if (slash < 0) {
return "";
} else if (slash == 0) {
return "/";
} else {
return filename.substr(0, slash);
}
}
string getTail(string filename) {
int slash = -1;
int len = filename.length();
for (int i = 0; i < len; i++) {
char ch = filename[i];
if (ch == '/' || ch == '\\') slash = i;
}
if (slash < 0) {
return filename;
} else {
return filename.substr(slash + 1);
}
}
string defaultExtension(string filename, string ext) {
bool force = (ext[0] == '*');
if (force) ext = ext.substr(1);
int dot = -1;
int len = filename.length();
for (int i = 0; i < len; i++) {
char ch = filename[i];
if (ch == '.') dot = i;
if (ch == '/' || ch == '\\') dot = -1;
}
if (dot == -1) {
force = true;
dot = len;
}
if (force) {
return filename.substr(0, dot) + ext;
} else {
return filename;
}
}
string openOnPath(ifstream & stream, string path, string filename) {
Vector<string> paths;
splitPath(path, paths);
foreach (string dir in paths) {
string pathname = dir + "/" + filename;
if (openFile(stream, pathname)) return pathname;
}
return "";
}
string openOnPath(ofstream & stream, string path, string filename) {
Vector<string> paths;
splitPath(path, paths);
foreach (string dir in paths) {
string pathname = dir + "/" + filename;
if (openFile(stream, pathname)) return pathname;
}
return "";
}
string findOnPath(string path, string filename) {
ifstream stream;
string result = openOnPath(stream, path, filename);
if (result != "") stream.close();
return result;
}
void deleteFile(string filename) {
remove(expandPathname(filename).c_str());
}
void renameFile(string oldname, string newname) {
oldname = expandPathname(oldname);
newname = expandPathname(newname);
rename(oldname.c_str(), newname.c_str());
}
void createDirectoryPath(string path) {
size_t cp = 1;
if (path == "") return;
while ((cp = path.find('/', cp + 1)) != string::npos) {
createDirectory(path.substr(0, cp - 1));
}
createDirectory(path);
}
bool matchFilenamePattern(string filename, string pattern) {
return recursiveMatch(filename, 0, pattern, 0);
}
bool fileExists(string filename) {
return pp->fileExists(filename);
}
bool isFile(string filename) {
return pp->isFile(filename);
}
bool isSymbolicLink(string filename) {
return pp->isSymbolicLink(filename);
}
bool isDirectory(string filename) {
return pp->isDirectory(filename);
}
void setCurrentDirectory(string path) {
return pp->setCurrentDirectory(path);
}
string getCurrentDirectory() {
return pp->getCurrentDirectory();
}
void createDirectory(string path) {
return pp->createDirectory(path);
}
string getDirectoryPathSeparator() {
return pp->getDirectoryPathSeparator();
}
string getSearchPathSeparator() {
return pp->getSearchPathSeparator();
}
string expandPathname(string filename) {
return pp->expandPathname(filename);
}
void listDirectory(string path, Vector<string> & list) {
vector<string> vec;
listDirectory(path, vec);
list.clear();
foreach (string file in vec) {
list.add(file);
}
}
void listDirectory(string path, vector<string> & list) {
return pp->listDirectory(path, list);
}
/* Private functions */
static void splitPath(string path, Vector<string> list) {
char sep = (path.find(';') == string::npos) ? ':' : ';';
path += sep;
size_t start = 0;
while (true) {
size_t finish = path.find(sep, start);
if (finish == string::npos) break;
if (finish > start + 1) {
list.add(path.substr(start, finish - start - 1));
}
start = finish + 1;
}
}
static bool recursiveMatch(string str, int sx, string pattern, int px) {
int slen = str.length();
int plen = pattern.length();
if (px == plen) return (sx == slen);
char pch = pattern[px];
if (pch == '*') {
for (int i = sx; i <= slen; i++) {
if (recursiveMatch(str, i, pattern, px + 1)) return true;
}
return false;
}
if (sx == slen) return false;
char sch = str[sx];
if (pch == '[') {
bool match = false;
bool invert = false;
px++;
if (px == plen) {
error("matchFilenamePattern: missing ]");
}
if (pattern[px] == '^') {
px++;
invert = true;
}
while (px < plen && pattern[px] != ']') {
if (px + 2 < plen && pattern[px + 1] == '-') {
match |= (sch >= pattern[px] && sch <= pattern[px + 2]);
px += 3;
} else {
match |= (sch == pattern[px]);
px++;
}
}
if (px == plen) {
error("matchFilenamePattern: missing ]");
}
if (match == invert) return false;
} else if (pch != '?') {
if (pch != sch) return false;
}
return recursiveMatch(str, sx + 1, pattern, px + 1);
}
|