-
Notifications
You must be signed in to change notification settings - Fork 0
/
uGlobalFileIoFunc.pas
725 lines (671 loc) · 21.4 KB
/
uGlobalFileIoFunc.pas
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
unit uGlobalFileIoFunc;
interface
uses
Windows, SysUtils, Classes, RTLConsts
, AcedContainers, AcedStrings, ZLibExGZ, ZLibEx, uAnsiStrings, uGlobalTypes,
uGlobalConstants
{$IFDEF UNICODE}
, AnsiStrings, Masks
{$ENDIF}
;
type
TFindInDirOpt = (foFindDirs, foFindFiles, foIncFullPath, foRecurs, foIncFilePath);
TFindInDirOpts = set of TFindInDirOpt;
/// <summary>
/// Открывает файл для чтения
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="ARaiseException">в случае ошибки вызвать Exception</param>
/// <returns>Handle открытого файла или INVALID_HANDLE_VALUE в случае ошибки</returns>
function OpenFileRead(const AFileName: TFileName; ARaiseException: Boolean): THandle;
/// <summary>
/// Открывает файл на запись
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="AAppend">добавлять новые данные в конец файла</param>
/// <param name="ARaiseException">в случае ошибки вызвать Exception</param>
/// <returns>Handle открытого файла или INVALID_HANDLE_VALUE в случае ошибки</returns>
function OpenFileWrite(const AFileName: TFileName; AAppend, ARaiseException: Boolean): THandle;
function FileWriteText(AHandle: THandle; const AText: AnsiString; ARaiseException: Boolean): Boolean;
function StringLoadFromFileHandle(const AHandle: THandle; out AOut: AnsiString;
ARaiseException: Boolean; const ADefault: AnsiString): DWORD;
function StringLoadFromFile(
const AFileName: string;
out AOut: AnsiString;
ATestFileExist: Boolean = False;
ARaiseException: Boolean = True;
const ADefault: AnsiString = ''
): DWORD; overload;
/// <summary>
/// Загрузка файла в строку Ansi
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="ATestFileExist">проверять наличие файла</param>
/// <param name="ARaiseException">вызывать исключение в случае ошибки открытия/чтение файла</param>
/// <param name="ADefault">возвращать строку в случае неудачи</param>
/// <returns>содержимое файла в AnsiString</returns>
function StringLoadFromFile(
const AFileName: string;
ATestFileExist: Boolean = False;
ARaiseException: Boolean = True;
const ADefault: AnsiString = ''
): AnsiString; overload;
/// <summary>
/// Сохранение строки в файл
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="ABuffer">буфер данных</param>
/// <param name="AAppend">добавлять данные в конец файла</param>
/// <param name="ARaiseException">при ошибке вызывать исключение</param>
/// <returns>True - если файнные записыны успешно</returns>
//TODO: добавть параметр для отклбчения ForceDirectory()
//TODO: сдеелать перегрузку с параметрами set of ()
function StringSaveToFile(
const AFileName: string;
const ABuffer: AnsiString;
AAppend: Boolean = False;
ARaiseException: Boolean = True
): Boolean;
/// <summary>
/// Загрузка файла в TAnsiStrings
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="AStrings">куда загружать</param>
/// <param name="ATestFileExist">проверять наличие файла</param>
/// <param name="AAlwaysClear">очищать AStrings даже если файла нет</param>
/// <param name="AReadByLine">читать файл по строкам TextReader'ом</param>
/// <returns>возвращает AStrings</returns>
function StringsLoadFromFile(
const AFileName: string;
AStrings: TAnsiStrings;
ATestFileExist: Boolean = False;
AAlwaysClear: Boolean = True;
AReadByLine: Boolean = False
): TAnsiStrings;
/// <summary>
/// Запись в файла из TAnsiStrings построчно
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="AStrings">что сохранять</param>
/// <param name="AAppend">добалять в конец файта, False - перезависать файл</param>
/// <param name="AUseTextWriter">использовать TextWriter (т.е. буфер)</param>
/// <param name="ARaiseException">в случае ошибки записи вызвать exception</param>
/// <returns>возвращает AStrings</returns>
function StringsSaveToFile(
const AFileName: string;
AStrings: TAnsiStrings;
AAppend: Boolean = False;
AUseTextWriter: Boolean = False;
ARaiseException: Boolean = True
): TAnsiStrings; {$IFDEF UNICODE} overload;{$ENDIF}
{$IFDEF UNICODE}
function StringsSaveToFile(
const AFileName: TFileName;
AStrings: TUnicodeStrings;
AAppend: Boolean = False;
AUseTextWriter: Boolean = False;
ARaiseException: Boolean = True
): TUnicodeStrings; overload;
{$ENDIF}
/// <summary>
/// полный размер файла (>2ГБ)
/// </summary>
/// <param name="AFileName">Имя файла</param>
/// <returns>размер файла</returns>
function GetFileSize2(const AFileName: string): Int64;
function SafeForceDirectories(const ADirName: TFileName): Boolean;
function FindInDir(const ADirName: string; AStrings: TStrings;
const AOpts: TFindInDirOpts = [foFindFiles];
const AFindMask: string = '*'): Integer; overload;
/// <summary>
/// поиск файлов\директорий
/// </summary>
/// <param name="ADirName">где искать</param>
/// <param name="AStrings">куда записывать найденное</param>
/// <param name="AFindFile">искать файлы или директории</param>
/// <param name="AFindMask">маска поиска</param>
/// <param name="AIncFullPath">записывать в AStrings полный путь к файлу</param>
/// <param name="ARecurs">рекурсивный поиск, т.е. включая поддиректории</param>
/// <param name="AIncFilePath">при AIncFullPath=False и ARecurs=True записывать найденное с поддиректориями</param>
/// <returns>кол-во найденный файлов\директорий</returns>
function FindInDir(const ADirName: string; AStrings: TStrings;
AFindFile: Boolean = True; const AFindMask: string = '*';
AIncFullPath: Boolean = False; ARecurs: Boolean = False;
AIncFilePath: Boolean = False): Integer; overload;
{$IFDEF UNICODE}
function FindInDir(const ADirName: string; AStrings: TAnsiStrings;
AFindFile: Boolean = True; const AFindMask: string = '*';
AIncFullPath: Boolean = False; ARecurs: Boolean = False;
AIncFilePath: Boolean = False): Integer; overload;
/// <summary>
/// Загрузка UTF8 файла в UncodeString
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="ATestFileExist">проверять наличие файла</param>
/// <param name="ARaiseException">вызывать исключение в случае ошибки открытия/чтение файла</param>
/// <param name="ADefault">возвращать строку в случае неудачи</param>
/// <returns>содержимое файла в UncodeString</returns>
function UnicodeStringLoadFromFile(
const AFileName: TFileName;
ATestFileExist: Boolean = False;
ARaiseException: Boolean = True;
const ADefault: UnicodeString = ''
): UnicodeString; overload;
/// <summary>
/// Сохранение UnicodeString строки в файл UTF8
/// </summary>
/// <param name="AFileName">имя файла</param>
/// <param name="ABuffer">буфер данных</param>
/// <param name="AAppend">добавлять данные в конец файла</param>
/// <param name="ARaiseException">при ошибке вызывать исключение</param>
/// <returns>True - если файнные записыны успешно</returns>
function UnicodeStringSaveToFile(
const AFileName: TFileName;
const ABuffer: UnicodeString;
AAppend: Boolean = False;
ARaiseException: Boolean = True
): Boolean;
{$ENDIF}
implementation
uses
uGlobalVars, uGlobalFunctions, uTextReader, uTextWriter;
function OpenFileReadOrWrite(const AFileName: TFileName;
AOpenRead, AAppend, ARaiseException: Boolean): THandle;
var
dwDesiredAccess: DWORD;
dwCreationDisposition: DWORD;
begin
// Result := INVALID_HANDLE_VALUE;
if AOpenRead then
begin
dwDesiredAccess := GENERIC_READ;
dwCreationDisposition := OPEN_EXISTING;
end
else
begin
dwDesiredAccess := GENERIC_WRITE;
if AAppend then
dwCreationDisposition := OPEN_ALWAYS
else
dwCreationDisposition := CREATE_ALWAYS
end;
//---
SetLastError(0);
//---
Result := CreateFile(
PChar(AFileName),
dwDesiredAccess,
FILE_SHARE_READ,
nil,
dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL,
0
);
if Result<>INVALID_HANDLE_VALUE then
begin
if (not AOpenRead) and AAppend then
SetFilePointer(Result, 0, nil, FILE_END);
end
else if ARaiseException then
begin
raise EFOpenError.CreateFmt(
SFOpenErrorEx,
[
SysUtils.ExpandFileName(AFileName),
SysErrorMessage(GetLastError())
]
);
end;
end;
function OpenFileRead(const AFileName: TFileName; ARaiseException: Boolean): THandle;
begin
Result := OpenFileReadOrWrite(AFileName, True, False, ARaiseException)
end;
function OpenFileWrite(const AFileName: TFileName; AAppend, ARaiseException: Boolean): THandle;
begin
Result := OpenFileReadOrWrite(AFileName, False, AAppend, ARaiseException)
end;
function FileWriteText(AHandle: THandle; const AText: AnsiString; ARaiseException: Boolean): Boolean;
begin
Result := SysUtils.FileWrite(AHandle, Pointer(AText)^, Length(AText)) > -1;
if (not Result) and (ARaiseException) then
begin
raise EWriteError.CreateRes(@SWriteError);
end
end;
function StringLoadFromFileHandle(const AHandle: THandle; out AOut: AnsiString;
ARaiseException: Boolean; const ADefault: AnsiString): DWORD;
var
sizeFile: Cardinal;
res: Int64;
begin
AOut := '';
Result := ERROR_SUCCESS;
SetLastError(Result);
sizeFile := GetFileSize(AHandle, nil);
if sizeFile=0 then
Exit; //***
SetLength(AOut, sizeFile);
res := FileRead(Integer(AHandle), Pointer(AOut)^, sizeFile);
if (res>-1) and (res=sizeFile) then
Exit //***
else
if ARaiseException then
raise EReadError.CreateRes(@SReadError);
//---
AOut := ADefault;
Result := GetLastError()
end;
function StringLoadFromFile(const AFileName: string; out AOut: AnsiString;
ATestFileExist, ARaiseException: Boolean;
const ADefault: AnsiString): DWORD;
var
hFile: THandle;
lasterr: DWORD;
begin
AOut := '';
Result := ERROR_SUCCESS;
SetLastError(Result);
if not ATestFileExist or FileExists(AFileName) then
begin
hFile := OpenFileRead(AFileName, ARaiseException);
if hFile<>INVALID_HANDLE_VALUE then
begin
try
StringLoadFromFileHandle(hFile, AOut, ARaiseException, ADefault);
Exit;
finally
lasterr := GetLastError();
CloseHandle(hFile);
SetLastError(lasterr);
end
end
end
else
begin
SetLastError(ERROR_FILE_NOT_FOUND);
end;
//---
AOut := ADefault;
Result := GetLastError()
end;
function StringLoadFromFile(const AFileName: string;
ATestFileExist, ARaiseException: Boolean;
const ADefault: AnsiString): AnsiString;
begin
StringLoadFromFile(AFileName, Result, ATestFileExist, ARaiseException, ADefault)
end;
function StringSaveToFile(const AFileName: string; const ABuffer: AnsiString;
AAppend, ARaiseException: Boolean): Boolean;
var hFile: THandle;
begin
Result := False;
if SafeForceDirectories(ExtractFileDir(AFileName)) then
begin
hFile := OpenFileWrite(AFileName, AAppend, ARaiseException);
if hFile<>INVALID_HANDLE_VALUE then
begin
try
Result := FileWriteText(hFile, ABuffer, ARaiseException);
finally
CloseHandle(hFile)
end
end
end;
end;
function StringsLoadFromFile(const AFileName: string; AStrings: TAnsiStrings;
ATestFileExist, AAlwaysClear, AReadByLine: Boolean): TAnsiStrings;
var reader: TAnsiStreamReader;
begin
Result := AStrings;
if AAlwaysClear then
AStrings.Clear;
//---
if not ATestFileExist or FileExists(AFileName) then
begin
if AReadByLine then
begin
reader := TAnsiStreamReader.Create(AFileName);
try
if not AAlwaysClear then
AStrings.Clear;
reader.ReadStrings(AStrings);
finally
reader.Free
end
end
else
begin
if not AAlwaysClear then
AStrings.Clear;
AStrings.LoadFromFile(AFileName);
end;
end
end;
function StringsSaveToFile(const AFileName: string; AStrings: TAnsiStrings;
AAppend, AUseTextWriter, ARaiseException: Boolean): TAnsiStrings;
var
hFile: THandle;
writer: TAnsiStreamWriter;
j: Integer;
begin
Result := AStrings;
if AUseTextWriter then
begin
try
writer := TAnsiStreamWriter.Create(AFileName, not AAppend);
try
writer.WriteStrings(AStrings);
finally
writer.Free;
end;
except
on E: Exception do
begin
if ARaiseException or (not (E is EStreamError)) then
raise
end;
end;
end
else
begin
hFile := OpenFileWrite(AFileName, AAppend, ARaiseException);
if hFile<>INVALID_HANDLE_VALUE then
begin
try
for j:=0 to AStrings.Count-1 do
begin
if not FileWriteText(hFile, AStrings[j]+CRLF, ARaiseException) then
Exit;
end;
finally
CloseHandle(hFile)
end
end
end;
end;
{$IFDEF UNICODE}
function StringsSaveToFile(const AFileName: TFileName; AStrings: TUnicodeStrings;
AAppend, AUseTextWriter, ARaiseException: Boolean): TUnicodeStrings;
var
hFile: THandle;
writer: TStreamWriter;
j: Integer;
dat: RawByteString;
begin
Result := AStrings;
if AUseTextWriter then
begin
try
writer := TStreamWriter.Create(AFileName, not AAppend, TEncoding.UTF8);
try
for j:=0 to AStrings.Count-1 do
writer.WriteLine(AStrings[j]);
finally
writer.Free;
end;
except
on E: Exception do
begin
if ARaiseException or (not (E is EStreamError)) then
raise
end;
end;
end
else
begin
hFile := OpenFileWrite(AFileName, AAppend, ARaiseException);
if hFile<>INVALID_HANDLE_VALUE then
begin
try
for j:=0 to AStrings.Count-1 do
begin
dat := UTF8Encode(AStrings[j]+CRLF);
if not FileWrite(hFile, dat, ARaiseException) then
Exit;
end;
finally
CloseHandle(hFile)
end
end
end;
end;
{$ENDIF}
function GetFileSize2(const AFileName: string): Int64;
var
d : TWin32FindData;
h : hwnd;
begin
Result := 0;
h := FindFirstFile(PChar(AFileName), d);
if (h<>INVALID_HANDLE_VALUE) then
begin
Result := d.nFileSizeLow or Int64(d.nFileSizeHigh) shl 32;
Windows.FindClose(h);
end;
end;
function _FindInDir(const ADirName, ASubDir: string; AStrings: TObject;
const AOpts: TFindInDirOpts; const AFindMask: string): Integer;
var
ssr : TSearchRec;
{$IFDEF UNICODE}
MaskCheckObj: Masks.TMask;
ansi_out: Boolean;
lAnsiStrings: TAnsiStrings;
{$ENDIF}
lStrings: TStrings;
procedure _Add(const AStr: string);
begin
{$IFDEF UNICODE}
if ansi_out then
lAnsiStrings.Add(AnsiString(AStr))
else
lStrings.Add(AStr)
{$ELSE}
lStrings.Add(AStr)
{$ENDIF}
end;
function _MatchesMask(const AFileName: string): Boolean;
begin
{$IFDEF UNICODE}
Result := MaskCheckObj.Matches(AFileName);
{$ELSE}
Result := G_ValidateWildText(AFileName, AFindMask);
{$ENDIF}
end;
begin
Result := 0;
//---
{$IFDEF UNICODE}
MaskCheckObj := nil;
if AStrings is TAnsiStrings then
begin
ansi_out := True;
lStrings := nil;
lAnsiStrings := AStrings as TAnsiStrings;
end
else if AStrings is TStrings then
begin
ansi_out := False;
lStrings := AStrings as TStrings;
lAnsiStrings := nil;
end
else
begin
raise Exception.Create('FindInDir() AStrings param isn''t TStrings object.');
end;
{$ELSE}
lStrings := AStrings as TStrings;
{$ENDIF}
//---
if FindFirst(ADirName+ASubDir+'*', faAnyFile, ssr)=0 then
try
{$IFDEF UNICODE}
MaskCheckObj := Masks.TMask.Create(AFindMask);
{$ENDIF}
//---
repeat
if (ssr.Name<>'..') and (ssr.Name<>'.') then
begin
if ((foFindFiles in AOpts) and ((ssr.Attr and faDirectory)=0)) or
((foFindDirs in AOpts) and (((ssr.Attr and faDirectory)<>0))) then
begin
if _MatchesMask(ssr.Name) then
begin
Inc(Result);
if Assigned(AStrings) then
begin
if foIncFullPath in AOpts then
_Add(ADirName+ASubDir+ssr.Name)
else
if foIncFilePath in AOpts then
_Add(ASubDir+ssr.Name)
else
_Add(ssr.Name)
end;
end;
end;
if (foRecurs in AOpts) and ((ssr.Attr and faDirectory)<>0) then
Result := Result +
_FindInDir(ADirName, ASubDir+ssr.Name+'\', AStrings, AOpts, AFindMask)
end;
until FindNext(ssr)<>0;
finally
SysUtils.FindClose(ssr);
{$IFDEF UNICODE}
FreeAndNil(MaskCheckObj);
{$ENDIF}
end;
end;
{$IFDEF UNICODE}
function FindInDir(const ADirName: string; AStrings: TAnsiStrings;
AFindFile: Boolean; const AFindMask: string;
AIncFullPath, ARecurs, AIncFilePath: Boolean): Integer;
var lOpts: TFindInDirOpts;
begin
lOpts := [];
if AFindFile then lOpts := lOpts + [foFindFiles]
else lOpts := lOpts + [foFindDirs];
if AIncFullPath then lOpts := lOpts + [foIncFullPath];
if ARecurs then lOpts := lOpts + [foRecurs];
if AIncFilePath then lOpts := lOpts + [foIncFilePath];
Result := _FindInDir(
SysUtils.IncludeTrailingPathDelimiter(ADirName),
'',
AStrings,
lOpts,
AFindMask
);
end;
function UnicodeStringLoadFromFile(const AFileName: TFileName;
ATestFileExist, ARaiseException: Boolean;
const ADefault: UnicodeString): UnicodeString;
var F: TStreamReader;
begin
Result := '';
if not ATestFileExist or FileExists(AFileName) then
begin
try
F := TStreamReader.Create(AFileName, TEncoding.UTF8);
try
Result := F.ReadToEnd();
Exit;
finally
F.Free;
end;
except
on E: Exception do
begin
if ARaiseException or (not (E is EStreamError)) then
raise
end;
end;
end;
//---
Result := ADefault;
end;
function UnicodeStringSaveToFile(const AFileName: TFileName;
const ABuffer: UnicodeString; AAppend, ARaiseException: Boolean): Boolean;
var F: TStreamWriter;
begin
Result := False;
if SafeForceDirectories(ExtractFileDir(AFileName)) then
begin
try
F := TStreamWriter.Create(AFileName, False, TEncoding.UTF8);
try
F.Write(ABuffer);
Result := True;
finally
F.Free;
end;
except
on E: Exception do
begin
if ARaiseException or (not (E is EStreamError)) then
raise
end;
end;
end;
end;
{$ENDIF}
function FindInDir(const ADirName: string; AStrings: TStrings;
AFindFile: Boolean; const AFindMask: string;
AIncFullPath, ARecurs, AIncFilePath: Boolean): Integer;
var lOpts: TFindInDirOpts;
begin
lOpts := [];
if AFindFile then lOpts := lOpts + [foFindFiles]
else lOpts := lOpts + [foFindDirs];
if AIncFullPath then lOpts := lOpts + [foIncFullPath];
if ARecurs then lOpts := lOpts + [foRecurs];
if AIncFilePath then lOpts := lOpts + [foIncFilePath];
Result := _FindInDir(
SysUtils.IncludeTrailingPathDelimiter(ADirName),
'',
AStrings,
lOpts,
IfElse(AFindMask='*.*','*',AFindMask)
);
end;
function FindInDir(const ADirName: string; AStrings: TStrings;
const AOpts: TFindInDirOpts; const AFindMask: string): Integer;
begin
Result := _FindInDir(
SysUtils.IncludeTrailingPathDelimiter(ADirName),
'',
AStrings,
AOpts,
IfElse(AFindMask='*.*','*',AFindMask)
);
end;
function SafeForceDirectories(const ADirName: TFileName): Boolean;
var
Dir: TFileName;
err: DWORD;
res: Boolean;
begin
if ADirName='' then
begin
Result := True;
Exit;
end;
Result := False;
Dir := ExcludeTrailingPathDelimiter(ADirName);
if Length(Dir)>=2 then
begin
if DirectoryExists(Dir) then
begin
Result := True;
Exit;
end;
if SafeForceDirectories(ExtractFileDir(Dir)) then
begin
SetLastError(0);
res := CreateDir(dir);
err := GetLastError();
Result := res or (err=ERROR_ALREADY_EXISTS)
end;
end;
end;
end.