summaryrefslogtreecommitdiff
path: root/src/nvUtilities.pas
blob: 1af9b6cf94a23a1741ecb90837302ce0155b21ea (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
unit nvUtilities;

{$mode objfpc}{$H+}

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

interface

uses
  Classes, SysUtils;

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


// 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;

// Alias method which is the same as Move() but with less confusing name
procedure MemCopy(const src; var dest; size: SizeInt);
// 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;


implementation


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;

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

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

function GetFileSize(const AFilename: string): integer;
var
  f: File;
begin
  Result := 0;
  Assign(f, AFileName);
  {$i-}
  Reset(f);
  {$i+}
  Result := FileSize(f);
  CloseFile(f);
end;

end.