Repository navigation
Expand file tree
/
Copy pathFilesEx.pas
More file actions
208 lines (171 loc) · 5.52 KB
/
Copy pathFilesEx.pas
File metadata and controls
208 lines (171 loc) · 5.52 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
unit FilesEx;
(*
DESCRIPTION: High-level finctions for working with files
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
*)
(***) interface (***)
uses SysUtils, Utils, Files, DataLib, StrLib, Classes, DataFlows;
type
(* Import *)
TStrList = DataLib.TStrList;
TDict = DataLib.TDict;
TSearchSubj = Files.TSearchSubj;
(*
Provides means for formatted output to external storage.
It's recommended to internally use StrBuilder for file output.
Default indentation must be ' ', line end marker - #13#10.
*)
IFormattedOutput = interface
procedure SetIndentation (const Identation: string);
procedure SetLineEndMarker (const aLineEndMarker: string);
// Increase or decrease indentation
procedure Indent;
procedure Unindent;
procedure SetIndentLevel (Level: integer);
procedure Write (const Str: string);
// Same as Write + Write([line end marker])
procedure WriteIndentation;
procedure RawLine (const Str: string);
// Same as Write([indentation]) * [indent level] + RawLine(Str)
procedure Line (const Str: string);
// Same as Write([line end marker])
procedure EmptyLine;
property Indentation: string write SetIndentation;
property LineEndMarker: string write SetLineEndMarker;
end;
TFileFormattedOutput = class (TInterfacedObject, IFormattedOutput)
protected
{O} fOutputBuf: StrLib.TStrBuilder;
fFilePath: string;
fIndentation: string;
fIndentLevel: integer;
fLineEndMarker: string;
public
constructor Create (const aFilePath: string);
destructor Destroy; override;
procedure SetIndentation (const aIndentation: string);
procedure SetLineEndMarker (const aLineEndMarker: string);
procedure Indent;
procedure Unindent;
procedure SetIndentLevel (Level: integer);
procedure WriteIndentation;
procedure Write (const Str: string);
procedure RawLine (const Str: string);
procedure Line (const Str: string);
procedure EmptyLine;
end;
// List does not own its objects. It simply contains case insensitive file names
function GetFileList (const MaskedPath: string; SearchSubj: TSearchSubj): {O} TStrList;
procedure MergeFileLists (MainList, DependantList: TStrList);
function WriteFormattedOutput (const FilePath: string): IFormattedOutput;
(***) implementation (***)
destructor TFileFormattedOutput.Destroy;
begin
Files.WriteFileContents(fOutputBuf.BuildStr, fFilePath);
FreeAndNil(fOutputBuf);
inherited;
end;
procedure TFileFormattedOutput.SetIndentation (const aIndentation: string);
begin
fIndentation := aIndentation;
end;
procedure TFileFormattedOutput.SetLineEndMarker (const aLineEndMarker: string);
begin
fLineEndMarker := aLineEndMarker;
end;
procedure TFileFormattedOutput.Indent;
begin
Inc(fIndentLevel);
end;
procedure TFileFormattedOutput.Unindent;
begin
if fIndentLevel > 0 then begin
Dec(fIndentLevel);
end;
end;
procedure TFileFormattedOutput.SetIndentLevel (Level: integer);
begin
if Level <= 0 then begin
fIndentLevel := 0;
end else begin
fIndentLevel := Level;
end;
end;
procedure TFileFormattedOutput.WriteIndentation;
var
i: integer;
begin
for i := 1 to fIndentLevel do begin
fOutputBuf.Append(fIndentation);
end;
end;
procedure TFileFormattedOutput.Write (const Str: string);
begin
fOutputBuf.Append(Str);
end;
procedure TFileFormattedOutput.RawLine (const Str: string);
begin
fOutputBuf.Append(Str);
fOutputBuf.Append(fLineEndMarker);
end;
procedure TFileFormattedOutput.Line (const Str: string);
var
i: integer;
begin
for i := 1 to fIndentLevel do begin
fOutputBuf.Append(fIndentation);
end;
fOutputBuf.Append(Str);
fOutputBuf.Append(fLineEndMarker);
end; // .procedure TFileFormattedOutput.Line
procedure TFileFormattedOutput.EmptyLine;
begin
fOutputBuf.Append(fLineEndMarker);
end;
constructor TFileFormattedOutput.Create (const aFilePath: string);
begin
fFilePath := aFilePath;
fOutputBuf := StrLib.TStrBuilder.Create;
fIndentation := ' ';
fIndentLevel := 0;
fLineEndMarker := #13#10;
end;
function GetFileList (const MaskedPath: string; SearchSubj: TSearchSubj): {O} TStrList;
begin
result := DataLib.NewStrList(not Utils.OWNS_ITEMS, DataLib.CASE_INSENSITIVE);
with Files.Locate(MaskedPath, SearchSubj) do begin
while FindNext do begin
result.Add(FoundName);
end;
end;
end;
procedure MergeFileLists (MainList, DependantList: TStrList);
var
{O} NamesDict: TDict {OF FileName: STRING => Ptr(1)};
i: integer;
begin
{!} Assert(MainList <> nil);
{!} Assert(DependantList <> nil);
NamesDict := DataLib.NewDict(not Utils.OWNS_ITEMS, DataLib.CASE_INSENSITIVE);
// * * * * * //
for i := 0 to MainList.Count - 1 do begin
NamesDict[MainList[i]] := Ptr(1);
end;
for i := 0 to DependantList.Count - 1 do begin
if NamesDict[DependantList[i]] = nil then begin
NamesDict[DependantList[i]] := Ptr(1);
MainList.Add(DependantList[i]);
end;
end;
// * * * * * //
SysUtils.FreeAndNil(NamesDict);
end; // .procedure MergeFileLists
function WriteFormattedOutput (const FilePath: string): IFormattedOutput;
var
{O} FormattedOutputObj: TFileFormattedOutput;
begin
FormattedOutputObj := TFileFormattedOutput.Create(FilePath);
// * * * * * //
result := FormattedOutputObj; FormattedOutputObj := nil;
end;
end.