summaryrefslogtreecommitdiff
path: root/extras/contributed/ats/ptrparsefunc.pas
blob: a1e514b7be567c51237a966a711a2c7f07922aeb (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
unit ptrparsefunc;

{$mode objfpc}{$H+}

interface

procedure ppSkipSpaces(var ReadPtr : PChar; bufend : PChar);

function ppReadLine(var ReadPtr : PChar; bufend : PChar; var LineLength : integer) : boolean;

function ppReadTo(var ReadPtr : PChar; bufend : PChar; const stopchars : shortstring; var CharCount : integer) : boolean;

function ppCheckSymbol(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring) : boolean;
function ppCheckSymbolCI(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring) : boolean; // case insensitive

function ppSearchPattern(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring;
  var distance : integer) : boolean;

function ppMakeString(buf : pointer; len : integer) : string;

implementation

function ppMakeString(buf : pointer; len : integer) : string;
begin
  SetLength(result, len);
  if len > 0 then move(buf^, result[1], len);
end;

procedure ppSkipSpaces(var ReadPtr : PChar; bufend : PChar);
begin
  while (ReadPtr < bufend) and (ReadPtr^ in [#13,#10,#9,#32]) do
  begin
    inc(ReadPtr);
  end;
end;

function ppReadLine(var ReadPtr : PChar; bufend : PChar; var LineLength : integer) : boolean;
begin
  LineLength := 0;
  result := true;
  while (ReadPtr < bufend) do
  begin
    if ReadPtr^ = #10 then
    begin
      // unix line end
      inc(ReadPtr);
      exit;
    end
    else if ReadPtr^ = #13 then
    begin
      // DOS or Mac line end
      inc(ReadPtr);
      if (ReadPtr < bufend) and (ReadPtr^ = #10) then inc(ReadPtr);  // DOS line ending
      Exit;
    end;
    inc(LineLength);
    inc(ReadPtr);
  end;
  result := false;
end;

function ppReadTo(var ReadPtr : PChar; bufend : PChar; const stopchars : shortstring; var CharCount : integer) : boolean;
begin
  CharCount := 0;
  while (ReadPtr < bufend) do
  begin
    if pos(ReadPtr^,stopchars) > 0 then
    begin
      result := true;
      exit;
    end;
    inc(CharCount);
    inc(ReadPtr);
  end;
  Result := false;
end;


function ppCheckSymbol(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring) : boolean;
var
  rp : PChar;
  cc : integer;
begin
  result := false;
  cc := 1;
  rp := ReadPtr;
  while rp < bufend do
  begin
    if checkstring[cc] <> rp^ then
    begin
      EXIT;
    end
    else if cc >= length(checkstring) then
    begin
      ReadPtr := rp;
      inc(ReadPtr);
      result := true;
      EXIT;
    end;
    inc(cc);
    inc(rp);
  end;
end;

function ppCheckSymbolCI(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring) : boolean;
var
  rp : PChar;
  cc : integer;
begin
  result := false;
  cc := 1;
  rp := ReadPtr;
  while rp < bufend do
  begin
    if UpCase(checkstring[cc]) <> UpCase(rp^) then
    begin
      EXIT;
    end
    else if cc >= length(checkstring) then
    begin
      ReadPtr := rp;
      inc(ReadPtr);
      result := true;
      EXIT;
    end;
    inc(cc);
    inc(rp);
  end;
end;

function ppSearchPattern(var ReadPtr : PChar; bufend : PChar; const checkstring : shortstring;
  var distance : integer) : boolean;
var
  rp, cstartp : PChar;
  cc : integer;
begin
  result := false;
  cc := 1;
  cstartp := ReadPtr;
  rp := ReadPtr;
  while rp < bufend do
  begin
    if checkstring[cc] <> rp^ then
    begin
      // try the next position
      inc(cstartp);
      rp := cstartp;
      cc := 1;
    end
    else if cc >= length(checkstring) then
    begin
      inc(rp);
      distance := rp - ReadPtr;
      ReadPtr := rp;
      result := true;
      EXIT;
    end
    else
    begin
      inc(cc);
      inc(rp);
    end;
  end;
end;

end.