summaryrefslogtreecommitdiff
path: root/docview/src/nvUtilities.pas
blob: df8f711b67c9430fd3ab69e5c7d4e20c1793b1c7 (plain)
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
unit nvUtilities;

{$mode objfpc}{$H+}

// disable to remove debugging output
{.$Define DEBUG}

interface

uses
  Classes, SysUtils, fpg_base;

const
 { TODO -oGraeme : Should this change to LineEnding (platfrom dependent) }
  EndLine= chr(13)+chr(10);
  TwoEndLines= chr(13)+chr(10)+chr(13)+chr(10);
  Quote = '''';
  DoubleQuote = '"';

  // -- Logging --
type
  LogAspect = (       LogStartup,
                      LogShutdown,
                      LogSettings,
                      LogI18n,
                      LogParse,
                      LogDisplay,
                      LogSearch,
                      LogNHM,
                      LogViewStub,
                      LogObjConstDest,
                      LogDebug
  );
  LogAspects = SET OF LogAspect;

procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String);

// Removes and returns the first value in a separated
// value list (removes quotes if found)
Function ExtractNextValue(var S: string; const Separator: string ): string;

Function ExtractNextValueNoTrim(var S: string; const Separator: string ): string;

function AllocateMemory( const Size: ValUInt ): pointer;
procedure DeallocateMemory( Var P: pointer );

// Alias method which is the same as Move() but with less confusing name
procedure MemCopy(const src; var dest; size: SizeInt);
procedure FillMem( Dest: pointer; Size: longint; Data: Byte );
// Allows for debug output and quite disable of output
procedure ProfileEvent(const AString: string);
// Return AFilename's size in bytes
function GetFileSize(const AFilename: string): integer;

function IsDigit(const AChar: TfpgChar): boolean;
function IsAlpha(const AChar: TfpgChar): boolean;
function Between( const Value: longint; const Limit1: longint; const Limit2: longint ): boolean;


operator = (ARect: TRect; BRect: TRect): boolean;

// Destroy the objects stored in List and clear the list.
procedure ClearListAndObjects( List: TList );
// Destroy the objects stored in the list and then destroy the list itself
// And set the reference to nil
procedure DestroyListAndObjects( Var List: TList );
// Destroy the objects stored in the list.
// You probably want to use one of the two functions above.
procedure DestroyListObjects( List: TList );

procedure AddList( Source, Dest: TList );
procedure AssignList( Source, Dest: TList );

procedure ListFilesInDirectory(const aDirectory: String; const aFilter: String; const aWithDirectoryFlag: boolean; var aList: TStrings);

// add all file name parts of aFileNameString to the aResult
// check for containing environment vars
// and include all help files if the environment var points
// to a directory
procedure ParseAndExpandFileNames(const aFileNameString: String; aResult: TStrings);



var
  startTime : Cardinal;
  lastTime : Cardinal;
  activeLogAspects : LogAspects;
  infoMessage1 : String;
  infoMessage2 : String;


implementation

uses
  fpg_utils
  ,fpg_main
  ,ACLStringUtility
  ,dvconstants
  ;

  Function GetAspectPrefix(const aLogAspect: LogAspect): String;
  Begin
    Case aLogAspect of
      LogStartup      : result := 'Startup';
      LogShutdown     : result := 'Start';
      LogSettings     : result := 'Settings';
      LogI18n         : result := 'I18n';
      LogParse        : result := 'Parse';
      LogDisplay      : result := 'Display';
      LogSearch       : result := 'Search';
      LogNHM          : result := 'NewHelpManager';
      LogViewStub     : result := 'ViewStub';
      LogObjConstDest : result := 'ObjConstDest';
      LogDebug        : result := 'Debug';
      else              result := 'Unknown';
      end;
  End;


  Procedure SetLogAspects(const aCommaSeparatedListOfAspectNames : String);
  Var
    tmpAspects : TStrings;
    i : Integer;
  Begin
    tmpAspects := TStringList.Create;
    StrExtractStrings(tmpAspects, aCommaSeparatedListOfAspectNames, [','], #0);

    for i:=0 to tmpAspects.count-1 do
    begin
      if tmpAspects[i] = 'LogStartup'      then activeLogAspects := activeLogAspects + [ LogStartup ];
      if tmpAspects[i] = 'LogShutdown'     then activeLogAspects := activeLogAspects + [ LogShutdown ];
      if tmpAspects[i] = 'LogSettings'     then activeLogAspects := activeLogAspects + [ LogSettings ];
      if tmpAspects[i] = 'LogI18n'         then activeLogAspects := activeLogAspects + [ LogI18n ];
      if tmpAspects[i] = 'LogParse'        then activeLogAspects := activeLogAspects + [ LogParse ];
      if tmpAspects[i] = 'LogDisplay'      then activeLogAspects := activeLogAspects + [ LogDisplay ];
      if tmpAspects[i] = 'LogSearch'       then activeLogAspects := activeLogAspects + [ LogSearch ];
      if tmpAspects[i] = 'LogNHM'          then activeLogAspects := activeLogAspects + [ LogNHM ];
      if tmpAspects[i] = 'LogViewStub'     then activeLogAspects := activeLogAspects + [ LogViewStub ];
      if tmpAspects[i] = 'LogObjConstDest' then activeLogAspects := activeLogAspects + [ LogObjConstDest ];
      if tmpAspects[i] = 'LogDebug'        then activeLogAspects := activeLogAspects + [ LogDebug ];
    end;

    tmpAspects.Destroy;
  End;

procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String);
var
  tmpMessage: String;
begin
  if (aLogAspect IN activeLogAspects) then
  begin
    tmpMessage := 'Log[' + GetAspectPrefix(aLogAspect) + ']  ' + anEventDescription;
    debugln(tmpMessage);
  end;
end;

Function ExtractNextValue( var S: string;
                           const Separator: string ): string;
begin
  Result := ExtractNextValueNoTrim( S, Separator );
  Result := trim( Result );

  // Remove quotes if present
  if Result <> '' then
    if Result[ 1 ] = DoubleQuote then
      Delete( Result, 1, 1 );

  if Result <> '' then
    if Result[ length( Result ) ] = DoubleQuote then
      Delete( Result, length( Result ), 1 );
end;

Function ExtractNextValueNoTrim( var S: string;
                                 const Separator: string ): string;
Var
  SeparatorPos: integer;
Begin
  SeparatorPos := Pos( Separator, S );
  if SeparatorPos > 0 then
  begin
    Result := Copy( S, 1, SeparatorPos-1 );
    Delete( S, 1, SeparatorPos + length( Separator ) - 1 );
  end
  else
  begin
    Result := S;
    S := '';
  end;
end;

function AllocateMemory( const Size: ValUInt ): pointer;
begin
  GetMem( Result, size + sizeof( Size ) );
  PtrUInt(Result^) := Size;
  inc( Result, sizeof( Size ) );
end;

procedure DeallocateMemory( Var P: pointer );
var
  Size: ValUInt;
begin
  if P = nil then
    exit;

  dec( P, sizeof( size ) );
  Size := ValUInt(P^);
  FreeMem( P, Size + sizeof( Size ) );
  P := nil;
end;

procedure MemCopy(const src; var dest; size: SizeInt);
begin
  Move(src, dest, size);
end;

procedure FillMem( Dest: pointer; Size: longint; Data: Byte );
begin
  FillChar( Dest^, Size, Data );
end;

procedure ProfileEvent(const AString: string);
begin
  {$IFDEF DEBUG}
  writeln('DEBUG:  ', AString);
  {$ENDIF}
end;

function GetFileSize(const AFilename: string): integer;
begin
  Result := fpgFileSize(AFilename);
end;

function IsDigit(const AChar: TfpgChar): boolean;
begin
  { TODO -oGraeme -cunicode : Not utf-8 compliant. }
  Result := ( AChar>='0' ) and ( AChar<='9' );
  //Result := TCharacter.IsDigit(AChar);
end;

function IsAlpha(const AChar: TfpgChar): boolean;
var
  UppercaseC: TfpgChar;
Begin
  { TODO -oGraeme -cunicode : Not utf-8 compliant. }
  UppercaseC := UpperCase( AChar );
  Result := ( UppercaseC >= 'A' ) and ( UppercaseC <= 'Z' );
  //Result := TCharacter.IsLetter(AChar);
end;

function Between( const Value: longint; const Limit1: longint; const Limit2: longint ): boolean;
begin
  if Limit1 < Limit2 then
    Result:= ( Value >= Limit1 ) and ( Value <= Limit2 )
  else
    Result:= ( Value >= Limit2 ) and ( Value <= Limit1 );
end;

operator = (ARect: TRect; BRect: TRect): boolean;
begin
  result :=  (ARect.Top   = BRect.Top) and
             (ARect.Left  = BRect.Left) and
             (ARect.Bottom = BRect.Bottom) and
             (ARect.Right = BRect.Right);
end;

// Destroy the objects stored in List
// and clear the list.
Procedure ClearListAndObjects( List: TList );
begin
  DestroyListObjects( List );
  List.Clear;
end;

// Destroy the objects stored in the list
// and then destroy the list itself.
Procedure DestroyListAndObjects( Var List: TList );
begin
  if not Assigned( List ) then
    exit;

  DestroyListObjects( List );
  List.Free;
  List := nil;
end;

Procedure DestroyListObjects( List: TList );
var
  Index: longint;
begin
  for Index := 0 to List.Count - 1 do
  begin
    if List[ Index ] <> nil then
    begin
      TObject( List[ Index ] ).Free;
      List[ Index ] := nil;
    end;
  end;
end;

Procedure AddList( Source, Dest: TList );
var
  i: longint;
begin
  // expand the destination list to what's required
  Dest.Capacity := Dest.Capacity + Source.Capacity;
  for i:= 0 to Source.Count - 1 do
    Dest.Add( Source[ i ] );
end;

Procedure AssignList( Source, Dest: TList );
begin
  Dest.Clear;
  AddList( Source, Dest );
end;

procedure ListFilesInDirectory(const aDirectory: String; const aFilter: String;
    const aWithDirectoryFlag: boolean; var aList: TStrings);
var
  tmpRC: longint;
  tmpSearchResults: TSearchRec;
  tmpMask: String;
  tmpFilterParts : TStrings;
  tmpDirectory: String;
  i: integer;
begin
  tmpFilterParts := TStringList.Create;

  StrExtractStrings(tmpFilterParts, aFilter, [PathSeparator], #0);

  for i:=0 to tmpFilterParts.count-1 do
  begin
    tmpMask := tmpFilterParts[i];
    tmpDirectory := IncludeTrailingPathDelimiter(aDirectory);
    tmpRC := fpgFindFirst(tmpDirectory + tmpMask, faAnyFile, tmpSearchResults);

    while tmpRC = 0 do
    begin
      if (tmpSearchResults.Attr and faDirectory) = 0 then
      begin
        if (aWithDirectoryFlag) then
          aList.Add(tmpDirectory + tmpSearchResults.Name)
        else
          aList.Add(tmpSearchResults.Name);
      end;

      tmpRC := fpgFindNext(tmpSearchResults);
    end;

    FindClose(tmpSearchResults);
  end;
  tmpFilterParts.Destroy;
end;

procedure ParseAndExpandFileNames(const aFileNameString: String; aResult: TStrings);
var
  i: longint;
  tmpFileNamesList: TStrings;
  tmpItem: String;
  tmpEnvironmentVarValue: string;
begin
  LogEvent(LogDebug, 'ParseAndExpandFileNames "' + aFileNameString + '"');
  tmpFileNamesList := TStringList.Create;

  StrExtractStrings(tmpFileNamesList, aFileNameString, [HELP_FILE_DELIMITER, PathSeparator], #0);
  for i := 0 to tmpFileNamesList.Count - 1 do
  begin
    tmpItem := tmpFileNamesList[i];

    // is this a environment var
    tmpEnvironmentVarValue := GetEnvironmentVariable(tmpItem);
    if tmpEnvironmentVarValue <> '' then      // environment var exists
    begin
      LogEvent(LogStartup, '    Environment var found; translated to: "' + tmpEnvironmentVarValue + '"');
      ParseAndExpandFileNames(tmpEnvironmentVarValue, aResult);
    end
    else if fpgDirectoryExists(tmpItem) then
    begin
      ListFilesInDirectory(tmpItem, '*' + INF_FILE_EXTENSION, true, aResult);
    end
    else
    begin
      aResult.Add(tmpItem);
    end;
  end;

  tmpFileNamesList.Free;
end;

{$IFDEF DEBUG}
initialization
  SetLogAspects('LogDebug,LogStartup');
{$ENDIF}

end.