-
Notifications
You must be signed in to change notification settings - Fork 0
/
FsUtils.cxx
409 lines (367 loc) · 9.73 KB
/
FsUtils.cxx
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#ifndef WIN32
#include <dirent.h>
#define PATH_SEP "/"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#else
#include "win_windirent.h"
#define opendir win_opendir
#define readdir win_readdir
#define closedir win_closedir
#define DIR win_DIR
#define dirent win_dirent
#define PATH_SEP "\\"
#endif
//*****************************************************************************
void ToLower(string &in)
{
size_t n=in.size();
for (size_t i=0; i<n; ++i)
{
in[i]=tolower(in[i]);
}
}
//******************************************************************************
int FileExists(const char *path)
{
#ifndef WIN32
struct stat s;
int iErr=stat(path,&s);
if (iErr==0)
{
return 1;
}
#endif
return 0;
}
//******************************************************************************
int Present(const char *path, const char *fileName)
{
ostringstream fn;
fn << path << PATH_SEP << fileName;
FILE *fp=fopen(fn.str().c_str(),"r");
if (fp==0)
{
// file is not present.
return 0;
}
// file is present.
fclose(fp);
return 1;
}
// Return 1 if a file is found with the prefix in the directory given by path.
//******************************************************************************
int Represented(const char *path, const char *prefix)
{
size_t prefixLen=strlen(prefix);
#ifndef NDEBUG
if (prefix[prefixLen-1]!='_')
{
cerr << __LINE__ << " Error: prefix is expected to end with '_' but it does not." << endl;
return 0;
}
#endif
DIR *ds=opendir(path);
if (ds)
{
struct dirent *de;
while ((de=readdir(ds)))
{
char *fname=de->d_name;
if (strncmp(fname,prefix,prefixLen)==0)
{
// Found at least one file beginning with the given prefix.
closedir(ds);
return 1;
}
}
closedir(ds);
}
else
{
cerr << __LINE__ << " Error: Failed to open the given directory. " << endl
<< path << endl;
}
//We failed to find any files starting with the given prefix
return 0;
}
//******************************************************************************
int ScalarRepresented(const char *path, const char *scalar)
{
string prefix(scalar);
prefix+="_";
return Represented(path,prefix.c_str());
}
//******************************************************************************
int VectorRepresented(const char *path, const char *vector)
{
string xprefix(vector);
xprefix+="x_";
string yprefix(vector);
yprefix+="y_";
string zprefix(vector);
zprefix+="z_";
return
Represented(path,xprefix.c_str())
&& Represented(path,yprefix.c_str())
&& Represented(path,zprefix.c_str());
}
//******************************************************************************
int SymetricTensorRepresented(const char *path, const char *tensor)
{
string xxprefix(tensor);
xxprefix+="-xx_";
string xyprefix(tensor);
xyprefix+="-xy_";
string xzprefix(tensor);
xzprefix+="-xz_";
string yyprefix(tensor);
yyprefix+="-yy_";
string yzprefix(tensor);
yzprefix+="-yz_";
string zzprefix(tensor);
zzprefix+="-zz_";
return
Represented(path,xxprefix.c_str())
&& Represented(path,xyprefix.c_str())
&& Represented(path,xzprefix.c_str())
&& Represented(path,yyprefix.c_str())
&& Represented(path,yzprefix.c_str())
&& Represented(path,zzprefix.c_str());
}
//******************************************************************************
int TensorRepresented(const char *path, const char *tensor)
{
string xxprefix(tensor);
xxprefix+="-xx_";
string xyprefix(tensor);
xyprefix+="-xy_";
string xzprefix(tensor);
xzprefix+="-xz_";
string yxprefix(tensor);
yxprefix+="-yx_";
string yyprefix(tensor);
yyprefix+="-yy_";
string yzprefix(tensor);
yzprefix+="-yz_";
string zxprefix(tensor);
zxprefix+="-zx_";
string zyprefix(tensor);
zyprefix+="-zy_";
string zzprefix(tensor);
zzprefix+="-zz_";
return
Represented(path,xxprefix.c_str())
&& Represented(path,xyprefix.c_str())
&& Represented(path,xzprefix.c_str())
&& Represented(path,yxprefix.c_str())
&& Represented(path,yyprefix.c_str())
&& Represented(path,yzprefix.c_str())
&& Represented(path,zxprefix.c_str())
&& Represented(path,zyprefix.c_str())
&& Represented(path,zzprefix.c_str());
}
// Collect the ids from a collection of files that start with
// the same prefix (eg. prefix_ID.ext). The prefix should include
// the underscore.
//*****************************************************************************
int GetSeriesIds(const char *path, const char *prefix, vector<int> &ids)
{
size_t prefixLen=strlen(prefix);
#ifndef NDEBUG
if (prefix[prefixLen-1]!='_')
{
cerr << __LINE__ << " Error: prefix is expected to end with '_' but it does not." << endl;
return 0;
}
#endif
DIR *ds=opendir(path);
if (ds)
{
struct dirent *de;
while ((de=readdir(ds)))
{
char *fname=de->d_name;
if (strncmp(fname,prefix,prefixLen)==0)
{
if (isdigit(fname[prefixLen]))
{
int id=atoi(fname+prefixLen);
ids.push_back(id);
}
}
}
closedir(ds);
sort(ids.begin(),ids.end());
return 1;
}
else
{
cerr << __LINE__ << " Error: Failed to open the given directory. " << endl
<< path << endl;
}
//We failed to find any files starting with the given prefix
return 0;
}
// Returns the path not including the file name and not
// including the final PATH_SEP. If PATH_SEP isn't found
// then ".PATH_SEP" is returned.
//*****************************************************************************
string StripFileNameFromPath(const string fileName)
{
size_t p;
p=fileName.find_last_of(PATH_SEP);
if (p==string::npos)
{
// current directory
return "." PATH_SEP;
}
return fileName.substr(0,p); // TODO Why does this leak?
}
// Returns the file name not including the extension (ie what ever is after
// the last ".". If there is no "." then the fileName is retnurned unmodified.
//*****************************************************************************
string StripExtensionFromFileName(const string fileName)
{
size_t p;
p=fileName.rfind(".");
if (p==string::npos)
{
// current directory
return fileName;
}
return fileName.substr(0,p); // TODO Why does this leak?
}
// Returns the file name from the given path. If PATH_SEP isn't found
// then the filename is returned unmodified.
//*****************************************************************************
string StripPathFromFileName(const string fileName)
{
size_t p;
p=fileName.find_last_of(PATH_SEP);
if (p==string::npos)
{
// current directory
return fileName;
}
return fileName.substr(p+1,string::npos); // TODO Why does this leak?
}
//*****************************************************************************
int LoadLines(const char *fileName, vector<string> &lines)
{
// Load each line in the given file into a the vector.
int nRead=0;
const int bufSize=1024;
char buf[bufSize]={'\0'};
ifstream file(fileName);
if (!file.is_open())
{
cerr << "ERROR: File " << fileName << " could not be opened." << endl;
return 0;
}
while(file.good())
{
file.getline(buf,bufSize);
if (file.gcount()>1)
{
lines.push_back(buf);
++nRead;
}
}
file.close();
return nRead;
}
//*****************************************************************************
int LoadText(const string &fileName, string &text)
{
ifstream file(fileName.c_str());
if (!file.is_open())
{
cerr << "ERROR: File " << fileName << " could not be opened." << endl;
return 0;
}
// Determine the length of the file ...
file.seekg (0,ios::end);
size_t nBytes=file.tellg();
file.seekg (0, ios::beg);
// and allocate a buffer to hold its contents.
char *buf=new char [nBytes];
memset(buf,0,nBytes);
// Read the file and convert to a string.
file.read (buf,nBytes);
file.close();
text=buf;
return nBytes;
}
//*****************************************************************************
int WriteText(string &fileName, string &text)
{
ofstream file(fileName.c_str());
if (!file.is_open())
{
cerr << "ERROR: File " << fileName << " could not be opened." << endl;
return 0;
}
file << text << endl;
file.close();
return 1;
}
//*****************************************************************************
int SearchAndReplace(
const string &searchFor,
const string &replaceWith,
string &inText)
{
int nFound=0;
const size_t n=searchFor.size();
size_t at=string::npos;
while ((at=inText.find(searchFor))!=string::npos)
{
inText.replace(at,n,replaceWith);
++nFound;
}
return nFound;
}
//*****************************************************************************
ostream &operator<<(ostream &os, vector<string> v)
{
size_t n=v.size();
if (n>0)
{
os << v[0];
for (size_t i=1; i<n; ++i)
{
os << " " << v[i];
}
}
return os;
}
//*****************************************************************************
bool operator&(vector<string> &v, const string &s)
{
// test for set inclusion.
const size_t n=v.size();
for (size_t i=0; i<n; ++i)
{
if (v[i]==s) return true;
}
return false;
}