File: resourcecodetool.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (264 lines) | stat: -rw-r--r-- 8,996 bytes parent folder | download | duplicates (6)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    The TResourceCodeTool provides functions to find, add and delete resources
    in resource files.
}
unit ResourceCodeTool;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, KeywordFuncLists, MultiKeyWordListTool, CodeCache,
  CodeAtom, BasicCodeTools;

type
  TResourceCodeTool = class(TMultiKeyWordListCodeTool)
  protected
    procedure SetSource(ACode: TCodeBuffer);
  public
    // lazarus resources
    function FindLazarusResourceHeaderComment(ResourceCode: TCodeBuffer
          ): TAtomPosition;
    function AddLazarusResourceHeaderComment(ResourceCode: TCodeBuffer;
          const Comment: string): boolean;
    function FindLazarusResource(ResourceCode: TCodeBuffer;
          const ResourceName: string; StartPos: integer): TAtomPosition;
    function FindAllLazarusResource(ResourceCode: TCodeBuffer;
          const ResourceName: string; StartPos: integer): TAtomList;
    function AddLazarusResource(ResourceCode: TCodeBuffer;
          const ResourceName, ResourceData: string): boolean;
    function RemoveLazarusResource(ResourceCode: TCodeBuffer;
          const ResourceName: string): boolean;
    function RemoveLazarusResourceEx(ResourceCode: TCodeBuffer;
          const ResourceName: string; AllExceptFirst: boolean;
          out First: TAtomPosition): boolean;
  end;
  
  TResourceCodeToolError = class(Exception)
  end;

implementation

{ TResourceCodeTool }

procedure TResourceCodeTool.SetSource(ACode: TCodeBuffer);
begin
  ClearLastError;
  Src:=ACode.Source;
  SrcLen:=length(Src);
  CurPos:=StartAtomPosition;
  LastAtoms.Clear;
  CurNode:=nil;
  DoDeleteNodes(Tree.Root);
end;

function TResourceCodeTool.FindLazarusResourceHeaderComment(
  ResourceCode: TCodeBuffer): TAtomPosition;
begin
  Result.StartPos:=-1;
  Result.EndPos:=-1;
  Result.Flag:=cafNone;
  SetSource(ResourceCode);

  Result.StartPos:=FindNextNonSpace(Src,1);
  if (Result.StartPos<=SrcLen) and (Src[Result.StartPos]='{') then
    Result.EndPos:=FindCommentEnd(Src,Result.StartPos,false)
  else
    Result.StartPos:=-1;
end;

function TResourceCodeTool.AddLazarusResourceHeaderComment(
  ResourceCode: TCodeBuffer; const Comment: string): boolean;
var
  InsertPos: TAtomPosition;
begin
  Result:=true;
  
  // find existing one
  InsertPos:=FindLazarusResourceHeaderComment(ResourceCode);
  if InsertPos.StartPos>0 then begin
    // there is already a comment
    // -> don't touch it
  end else
    ResourceCode.Insert(1,Comment);
end;

function TResourceCodeTool.FindLazarusResource(
  ResourceCode: TCodeBuffer; const ResourceName: string;
  StartPos: integer): TAtomPosition;
var
  ResourceNameInPascal: string;
  ResStartPos: integer;
begin
  Result.StartPos:=-1;
  Result.EndPos:=-1;
  SetSource(ResourceCode);
  if StartPos>=1 then
    MoveCursorToCleanPos(StartPos);

  // search "LAZARUSRESOURCES.ADD('ResourceName',"
  ResourceNameInPascal:=''''+UpperCaseStr(ResourceName)+'''';
  repeat
    ReadNextAtom;
    if UpAtomIs('LAZARUSRESOURCES') then begin
      ResStartPos:=CurPos.StartPos;
      ReadNextAtom;
      if CurPos.Flag<>cafPoint then continue;
      ReadNextAtom;
      if not UpAtomIs('ADD') then continue;
      ReadNextAtom;
      if CurPos.Flag<>cafRoundBracketOpen then continue;
      ReadNextAtom;
      if UpAtomIs(ResourceNameInPascal) then begin
        // resource start found
        Result.StartPos:=ResStartPos;
      end;
      UndoReadNextAtom;
      ReadTilBracketClose(false);
      if CurPos.Flag<>cafRoundBracketClose then begin
        // syntax error
        Result.StartPos:=-1;
        exit;
      end;
      if (Result.StartPos>0) then begin
        // resource end found
        Result.EndPos:=CurPos.EndPos;
        ReadNextAtom;
        if CurPos.Flag=cafSemicolon then
          Result.EndPos:=CurPos.EndPos;
        exit;
      end;
    end;
  until CurPos.StartPos>SrcLen;
end;

function TResourceCodeTool.FindAllLazarusResource(ResourceCode: TCodeBuffer;
  const ResourceName: string; StartPos: integer): TAtomList;
var
  ResourcePos: TAtomPosition;
begin
  Result:=TAtomList.Create;
  repeat
    ResourcePos:=FindLazarusResource(ResourceCode,ResourceName,StartPos);
    if ResourcePos.StartPos<1 then break;
    Result.Add(ResourcePos);
    StartPos:=ResourcePos.EndPos;
  until false;
end;

function TResourceCodeTool.AddLazarusResource(ResourceCode: TCodeBuffer;
  const ResourceName, ResourceData: string): boolean;
var
  InsertAtom: TAtomPosition;
  NeededLineEnds, i: integer;
  NewResData: string;
begin
  Result:=false;
  // try to find an old resource and delete all doubles
  Result:=RemoveLazarusResourceEx(ResourceCode,ResourceName,true,InsertAtom);
  if InsertAtom.StartPos<1 then begin
    // not found -> add at end of file
    InsertAtom.StartPos:=ResourceCode.SourceLength+1;
    InsertAtom.EndPos:=ResourceCode.SourceLength+1;
  end else begin
    InsertAtom.StartPos:=BasicCodeTools.FindLineEndOrCodeInFrontOfPosition(Src,
                                     InsertAtom.StartPos,1,false,true);
    InsertAtom.EndPos:=BasicCodeTools.FindLineEndOrCodeAfterPosition(Src,
                                     InsertAtom.EndPos,SrcLen,false);
  end;
  if CodeIsOnlySpace(Src,1,InsertAtom.StartPos-1) then
    InsertAtom.StartPos:=1;
  if CodeIsOnlySpace(Src,InsertAtom.EndPos+1,SrcLen) then
    InsertAtom.EndPos:=SrcLen+1;

  NewResData:=ResourceData;
  i:=length(NewResData);
  while (i>1) and (NewResData[i] in [' ',#10,#13]) do
    dec(i);
  SetLength(NewResData,i);
  // add front gap
  NeededLineEnds:=CountNeededLineEndsToAddForward(ResourceData,1,2);
  NeededLineEnds:=CountNeededLineEndsToAddBackward(Src,InsertAtom.StartPos-1,
                                                   NeededLineEnds);
  for i:=1 to NeededLineEnds do
    NewResData:=LineEnding+NewResData;
  // add start gap
  NeededLineEnds:=CountNeededLineEndsToAddBackward(ResourceData,
                                                   length(ResourceData),2);
  NeededLineEnds:=CountNeededLineEndsToAddForward(Src,InsertAtom.EndPos,
                                                  NeededLineEnds);
  for i:=1 to NeededLineEnds do
    NewResData:=NewResData+LineEnding;
  // replace
  ResourceCode.Replace(InsertAtom.StartPos,
                       InsertAtom.EndPos-InsertAtom.StartPos,
                       NewResData);
  Result:=true;
end;

function TResourceCodeTool.RemoveLazarusResource(ResourceCode: TCodeBuffer;
  const ResourceName: string): boolean;
var
  FirstResPos: TAtomPosition;
begin
  Result:=RemoveLazarusResourceEx(ResourceCode,ResourceName,false,FirstResPos);
end;

function TResourceCodeTool.RemoveLazarusResourceEx(ResourceCode: TCodeBuffer;
  const ResourceName: string; AllExceptFirst: boolean; out First: TAtomPosition
  ): boolean;
var
  ResourcePositions: TAtomList;
  CurResPos: TAtomPosition;
  i, FirstIndex: integer;
begin
  Result:=true;
  ResourcePositions:=FindAllLazarusResource(ResourceCode,ResourceName,-1);
  try
    if AllExceptFirst then
      FirstIndex:=1
    else
      FirstIndex:=0;
    for i:=ResourcePositions.Count-1 downto FirstIndex do begin
      CurResPos:=ResourcePositions[i];
      CurResPos.EndPos:=BasicCodeTools.FindLineEndOrCodeAfterPosition(Src,
                             CurResPos.EndPos,SrcLen,false);
      ResourceCode.Delete(CurResPos.StartPos,
                          CurResPos.EndPos-CurResPos.StartPos);
    end;
    if ResourcePositions.Count>0 then begin
      First:=ResourcePositions[0];
    end else begin
      First.StartPos:=-1;
      First.EndPos:=-1;
    end;
  finally
    ResourcePositions.Free;
  end;
end;

end.