-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileutil.cpp
627 lines (565 loc) · 18.4 KB
/
fileutil.cpp
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
Authors : Guru Kathiresan - [email protected] ,
FreePascal Team - http://www.freepascal.org
License :
Short Verion : wxVCL is distributed under Modified LGPL
(the same license used by FCL, LCL). In short,
this license allows you to use wxVCL in your application either
statically or dynamically linked (and keep your source code as
closed source) but you cannot sell wxVCL alone as a seperate
product or claim owner ship for it and when you make a change to
the wxVCL library (not your application code), you have to give
the changes (of the wxVCL library code) back to the community.
Long Version : The source code of wxVCL is distributed under the
Library GNU General Public License with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,
and to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a module
which is not derived from or based on this library. If you modify this
library, you may extend this exception to your version of the library, but you are
not obligated to do so. If you do not wish to do so, delete this exception
statement from your version.
*/
#include "fileutil.h"
#include "strutils.h"
#include "wx/wx.h"
#include "wx/dir.h"
#include "wx/file.h"
#include "wx/filefn.h"
#include "sysutils.h"
#include "sysset.h"
#include "classes.h"
#include <wx/filename.h>
#include <wx/wfstream.h>
bool DirPathExists(wxString const & Filename)
{
return wxDir::Exists(Filename);
}
int CompareFilenames(wxString const & Filename1, wxString const & Filename2)
{
#ifdef WXMSW
return AnsiCompareText(Filename1, Filename2);
#else
return AnsiCompareStr(Filename1, Filename2);
#endif
}
int CompareFilenames(wxString const & Filename1, wxString const & Filename2,
bool ResolveLinks)
{
int result;
wxString File1;
wxString File2;
File1 = Filename1;
File2 = Filename2;
if (ResolveLinks)
{
File1 = ReadAllLinks(File1, false);
if ((File1 == ""))
File1 = Filename1;
File2 = ReadAllLinks(File2, false);
if ((File2 == ""))
File2 = Filename2;
}
result = CompareFilenames(File1, File2);
return result;
}
int CompareFilenames(char* Filename1, int Len1, char* Filename2, int Len2,
bool ResolveLinks)
{
wxUnusedVar(Filename1);
wxUnusedVar(Len1);
wxUnusedVar(Filename2);
wxUnusedVar(Len2);
wxUnusedVar(ResolveLinks);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function FilenameIsAbsolute(const TheFilename: string):boolean;
------------------------------------------------------------------------------ */
bool FilenameIsAbsolute(wxString const & TheFilename)
{
#ifdef WXMSW
return FilenameIsWinAbsolute(TheFilename);
#else
return FilenameIsUnixAbsolute(TheFilename);
#endif
}
bool FilenameIsWinAbsolute(wxString const & TheFilename)
{
return ((Length(TheFilename) >= 2) && (AllEnglishCharSet.Contains
(TheFilename[0])) && (TheFilename[1] == ':')) ||
((Length(TheFilename) >= 2) && (TheFilename[0] == '\\') &&
(TheFilename[1] == '\\'));
}
bool FilenameIsUnixAbsolute(wxString const & TheFilename)
{
return (TheFilename.Length() != 0) && (TheFilename[0] == '/');
}
/* ------------------------------------------------------------------------------
function FilenameIsPascalUnit(const Filename: string): boolean;
------------------------------------------------------------------------------ */
bool FilenameIsPascalUnit(wxString const & Filename)
{
wxUnusedVar(Filename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function AppendPathDelim(const Path: string): string;
------------------------------------------------------------------------------ */
wxString AppendPathDelim(wxString const & Path)
{
wxString result;
if ((Path.Length() != 0) && (Path.Last() != PathDelim))
result = Path + PathDelim;
else
result = Path;
return result;
}
/* ------------------------------------------------------------------------------
function TrimFilename(const AFilename: string): string;
------------------------------------------------------------------------------ */
wxString TrimFilename(wxString const & AFilename)
{
wxUnusedVar(AFilename);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString ExtractFileNameWithoutExt(wxString const & AFilename)
{
wxFileName fnobj(AFilename);
return fnobj.GetName();
}
/* ------------------------------------------------------------------------------
function CompareFileExt(const Filename, Ext: string;
CaseSensitive: boolean): integer;
------------------------------------------------------------------------------ */
int CompareFileExt(wxString const & Filename, wxString const & Ext,
bool CaseSensitive)
{
wxString FileExt = ExtractFileExt(Filename);
if (CaseSensitive)
return CompareStr(FileExt, Ext);
else
return CompareText(FileExt, Ext);
}
int CompareFileExt(wxString const & Filename, wxString const & Ext)
{
return CompareFileExt(Filename, Ext, false);
}
/* ------------------------------------------------------------------------------
function ChompPathDelim(const Path: string): string;
------------------------------------------------------------------------------ */
wxString ChompPathDelim(wxString const & Path)
{
if ((Path.Length() != 0) && (Path.Last() == PathDelim))
return LeftStr(Path, Length(Path) - 1);
else
return Path;
}
/* ------------------------------------------------------------------------------
function FileIsText(const AFilename: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsText(wxString const & AFilename)
{
bool result;
bool FileReadable;
result = FileIsText(AFilename, FileReadable);
if (FileReadable)
return result;
else
return false;
}
bool FileIsText(wxString const & AFilename, bool & /* OUT-only */ FileReadable)
{
wxUnusedVar(FileReadable);
wxFileInputStream filestream(AFilename);
if (filestream.IsOk() == false)
return false;
bool hit = false;
do
{
wxChar a;
filestream.Read(&a, 1);
if (((int)a < 7) || (((int)a >= 14) && ((int)a <= 31)))
hit = true;
}
while ((filestream.Eof() == false) || hit);
return !hit;
}
/* ------------------------------------------------------------------------------
procedure CheckIfFileIsSymlink(const AFilename: string);
------------------------------------------------------------------------------ */
void CheckIfFileIsSymlink(wxString const & AFilename)
{
wxUnusedVar(AFilename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function FileIsSymlink(const AFilename: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsSymlink(wxString const & AFilename)
{
wxUnusedVar(AFilename);
#ifdef WXMSW
return false;
#else
THROW_NOT_IMPLEMENTED_ERROR;
#endif
}
/* ------------------------------------------------------------------------------
function FileIsReadable(const AFilename: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsReadable(wxString const & AFilename)
{
return (wxAccess(AFilename.c_str(), 04) == 0);
}
/* ------------------------------------------------------------------------------
FileIsWritable
------------------------------------------------------------------------------ */
bool FileIsWritable(wxString const & AFilename)
{
return (wxAccess(AFilename.c_str(), 02) == 0);
}
wxULongLong FileSize(wxString const & Filename)
{
return wxFileName::GetSize(Filename);
}
/* ------------------------------------------------------------------------------
GetFileDescription
------------------------------------------------------------------------------ */
wxString GetFileDescription(wxString const & AFilename)
{
wxUnusedVar(AFilename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
------------------------------------------------------------------------------ */
wxString ReadAllLinks(wxString const & Filename, bool ExceptionOnError)
{
wxUnusedVar(Filename);
wxUnusedVar(ExceptionOnError);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function ExtractFileNameOnly(const AFilename: string): string;
------------------------------------------------------------------------------ */
wxString ExtractFileNameOnly(wxString const & AFilename)
{
return ExtractFileNameWithoutExt(AFilename);
}
/* ------------------------------------------------------------------------------
function FileIsExecutable(const AFilename: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsExecutable(wxString const & AFilename)
{
wxUnusedVar(AFilename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
procedure CheckIfFileIsExecutable(const AFilename: string);
------------------------------------------------------------------------------ */
void CheckIfFileIsExecutable(wxString const & AFilename)
{
wxUnusedVar(AFilename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function ForceDirectory(DirectoryName: string): boolean;
------------------------------------------------------------------------------ */
bool ForceDirectory(wxString DirectoryName)
{
wxUnusedVar(DirectoryName);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function DeleteDirectory(const DirectoryName: string;
OnlyChilds: boolean): boolean;
------------------------------------------------------------------------------ */
bool DeleteDirectory(wxString const & DirectoryName, bool OnlyChilds)
{
wxUnusedVar(DirectoryName);
wxUnusedVar(OnlyChilds);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function ProgramDirectory: string;
------------------------------------------------------------------------------ */
wxString ProgramDirectory()
{
THROW_NOT_IMPLEMENTED_ERROR;
}
bool DirectoryIsWritable(wxString const & DirectoryName)
{
bool result = false;
wxString TempFilename = GetTempFilename(DirectoryName, "tstperm");
wxFileOutputStream fileStream(TempFilename);
if (fileStream.IsOk() == false)
return false;
if (fileStream.Write("Test", 4).IsOk() == false)
result = false;
else
result = true;
DeleteFile(TempFilename);
return result;
}
/* ------------------------------------------------------------------------------
function CleanAndExpandFilename(const Filename: string): string;
------------------------------------------------------------------------------ */
wxString CleanAndExpandFilename(wxString const & Filename)
{
wxUnusedVar(Filename);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function CleanAndExpandDirectory(const Filename: string): string;
------------------------------------------------------------------------------ */
wxString CleanAndExpandDirectory(wxString const & Filename)
{
wxUnusedVar(Filename);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString CreateAbsoluteSearchPath(wxString const & SearchPath,
wxString const & BaseDirectory)
{
wxUnusedVar(SearchPath);
wxUnusedVar(BaseDirectory);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString CreateRelativePath(wxString const & Filename,
wxString const & BaseDirectory)
{
wxUnusedVar(Filename);
wxUnusedVar(BaseDirectory);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function FileIsInPath(const Filename, Path: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsInPath(wxString const & Filename, wxString const & Path)
{
wxUnusedVar(Filename);
wxUnusedVar(Path);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function FileIsInPath(const Filename, Path: string): boolean;
------------------------------------------------------------------------------ */
bool FileIsInDirectory(wxString const & Filename, wxString const & Directory)
{
bool result;
wxString ExpFile;
wxString ExpDir;
int LenFile;
int LenDir;
long p;
ExpFile = CleanAndExpandFilename(Filename);
ExpDir = CleanAndExpandDirectory(Directory);
LenFile = Length(ExpFile);
LenDir = Length(ExpDir);
p = LenFile;
while ((p > 0) && (ExpFile[(unsigned int)p] != PathDelim))
--p;
result = (p == LenDir) && (p < LenFile) && (CompareFilenames(ExpDir,
LeftStr(ExpFile, p)) == 0);
return result;
}
/* ------------------------------------------------------------------------------
function FileInFilenameMasks(const Filename, Masks: string): boolean;
Checks if 'Filename' fits to one of the mask in 'Masks'.
Note: It checks the whole Filename. So, for example /somewhere/unit1.pas does
not fit the mask 'unit*.pas', but it will fit '*.pas'.
Masks is delimited by semicolon.
Masks allows asterisk (*) for arbitrary text and question mark (?) for one
arbitrary character.
Examples:
'*.pas;*.pp;*.inc'
'*.tar.*'
'lazarus*.xpm'
------------------------------------------------------------------------------ */
bool FileInFilenameMasks(wxString const & Filename, wxString const & Masks)
{
wxUnusedVar(Filename);
wxUnusedVar(Masks);
THROW_NOT_IMPLEMENTED_ERROR;
}
/* ------------------------------------------------------------------------------
function CopyFile(const SrcFilename, DestFilename: string): boolean;
------------------------------------------------------------------------------ */
bool CopyFile(wxString const & SrcFilename, wxString const & DestFilename)
{
bool result;
result = CopyFile(SrcFilename, DestFilename, false);
return result;
}
/* ------------------------------------------------------------------------------
function CopyFile(const SrcFilename, DestFilename: string PreserveTime:
boolean): boolean;
------------------------------------------------------------------------------ */
bool CopyFile(wxString const & SrcFilename, wxString const & DestFilename,
bool PreserveTime)
{
wxUnusedVar(PreserveTime);
if (wxCopyFile(SrcFilename, DestFilename, true))
{
// fixme: if (PreserveTime)
}
return false;
}
/* ------------------------------------------------------------------------------
function GetTempFilename(const Directory, Prefix: string): string;
------------------------------------------------------------------------------ */
wxString GetTempFilename(wxString const & Directory, wxString const & Prefix)
{
wxString result;
int i;
wxString CurPath;
CurPath = AppendPathDelim(Directory) + Prefix;
i = 1;
do
{
result = CurPath + IntToStr(i) + ".tmp";
if (!FileExists(result))
return result;
++i;
}
while (!(false));
return result;
}
/* ------------------------------------------------------------------------------
function SearchFileInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): string;
------------------------------------------------------------------------------ */
wxString SearchFileInPath(wxString const & Filename, wxString const & BasePath,
wxString const & SearchPath, wxString const & Delimiter,
TSearchFileInPathFlags Flags)
{
wxUnusedVar(Filename);
wxUnusedVar(BasePath);
wxUnusedVar(SearchPath);
wxUnusedVar(Delimiter);
wxUnusedVar(Flags);
THROW_NOT_IMPLEMENTED_ERROR;
}
TStrings SearchAllFilesInPath(wxString const & Filename,
wxString const & BasePath, wxString const & SearchPath,
wxString const & Delimiter, TSearchFileInPathFlags Flags)
{
wxUnusedVar(Filename);
wxUnusedVar(BasePath);
wxUnusedVar(SearchPath);
wxUnusedVar(Delimiter);
wxUnusedVar(Flags);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString FindDiskFilename(wxString const & Filename)
{
wxUnusedVar(Filename);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString FindDiskFileCaseInsensitive(wxString const & Filename)
{
wxString result = "";
wxString ShortFilename;
wxString CurDir;
wxDir dir;
CurDir = ExtractFilePath(Filename);
wxArrayString FileList;
dir.GetAllFiles(CurDir, &FileList, GetAllFilesMask(), wxDIR_DEFAULT);
for (size_t i = 0; i < FileList.GetCount(); i++)
{
ShortFilename = ExtractFileName(Filename);
if (CompareText(FileList[i], ShortFilename) == 0)
{
if (CompareStr(FileList[i], ShortFilename) == 0)
{
// fits exactly
result = Filename;
break;
}
// fits case insensitive
result = CurDir + FileList[i];
// search further
}
}
return result;
}
wxString FindDefaultExecutablePath(wxString const & Executable)
{
wxUnusedVar(Executable);
THROW_NOT_IMPLEMENTED_ERROR;
}
wxString GetAllFilesMask(void)
{
return AllDirectoryEntriesMask;
}
wxString GetExeExt(void)
{
wxString result;
#ifdef _WXMSW_
result = ".exe";
#else
result = "";
#endif
return result;
}
/* ------------------------------------------------------------------------------
function ReadFileToString(const Filename: string): string;
------------------------------------------------------------------------------ */
wxString ReadFileToString(wxString const & Filename)
{
wxArrayString str;
LoadFromFile(Filename, str);
return ArrayStringToString(str);
}
TStringList GetFolderList(wxString baseFolder, bool includeFullFolderPath)
{
TStringList result;
wxString FName;
wxDir dir(baseFolder);
if (dir.GetFirst(&FName,wxEmptyString,wxDIR_DIRS))
{
do
{
if (includeFullFolderPath)
{
wxString FullDirName = IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(baseFolder) + FName);
result.Add(FullDirName);
}
else
{
result.Add(FName);
}
} while(dir.GetNext(&FName));
}
return result;
}
TStringList GetFileList(wxString folder, const wxString& mask, bool includeFullFolderPath)
{
TStringList result;
wxString FName;
if (wxDir::Exists(folder) == false)
{
return result;
}
wxDir dir(folder);
if (dir.GetFirst(&FName,mask,wxDIR_FILES))
{
do
{
if (includeFullFolderPath)
{
wxString FullFileName = IncludeTrailingPathDelimiter(folder) + FName;
result.Add(FullFileName);
}
else
{
result.Add(FName);
}
} while(dir.GetNext(&FName));
}
return result;
}