1 {
2  ***************************************************************************
3  *                                                                         *
4  *   This source is free software; you can redistribute it and/or modify   *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This code is distributed in the hope that it will be useful, but      *
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
12  *   General Public License for more details.                              *
13  *                                                                         *
14  *   A copy of the GNU General Public License is available on the World    *
15  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
16  *   obtain it by writing to the Free Software Foundation,                 *
17  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
18  *                                                                         *
19  ***************************************************************************
20 
21   Author: Mattias Gaertner
22 
23   Abstract:
24     Demo for automatic fixing the filenames of include directives and uses
25     section.
26 }
27 program FixFilenames;
28 
29 {$mode objfpc}{$H+}
30 
31 uses
32   Classes, SysUtils,
33   // LazUtils
34   LazFileUtils, LazUTF8, AvgLvlTree,
35   // CodeTools
36   CodeToolManager, CodeCache;
37 
38 const
39   ConfigFilename = 'codetools.config';
40 var
41   Code: TCodeBuffer;
42   Filename: String;
43   MissingUnits: TStrings;
44   ReplaceUnits: TStringToStringTree;
45   MissingIncludeFilesCodeXYPos: TFPList;
46 begin
47   // init the codetools
48   CodeToolBoss.SimpleInit(ConfigFilename);
49 
50   // load the example unit
51   Filename:='scanexamples/brokenfilenames.pas';
52   if ParamCount>0 then
53     Filename:=ParamStrUTF8(1);
54   Filename:=ExpandFileNameUTF8(Filename);
55   writeln('Filename="',Filename,'"');
56   Code:=CodeToolBoss.LoadFile(Filename,false,false);
57   if Code=nil then
58     raise Exception.Create('unable to read '+Filename);
59 
60   // fix the filenames in the include directives
61   MissingIncludeFilesCodeXYPos:=nil;
62   if not CodeToolBoss.FixIncludeFilenames(Code,true,
63                                           MissingIncludeFilesCodeXYPos)
64   then
65     raise Exception.Create('unable to fix include filesnames in '+Filename+' '+CodeToolBoss.ErrorMessage);
66   CodeToolBoss.FreeListOfPCodeXYPosition(MissingIncludeFilesCodeXYPos);
67 
68   // replace some unit names
69   ReplaceUnits:=TStringToStringTree.Create(false);
70   ReplaceUnits['classes']:='Classes, SysUtils';
71   ReplaceUnits['CustApp']:='';
72   if not CodeToolBoss.ReplaceUsedUnits(Code,ReplaceUnits) then
73     raise Exception.Create('unable to fix unit names in '+Filename+' '+CodeToolBoss.ErrorMessage);
74   ReplaceUnits.Free;
75 
76   // fix the unitnames in the uses section
77   MissingUnits:=nil;
78   if not CodeToolBoss.FindMissingUnits(Code,MissingUnits,true) then
79     raise Exception.Create('unable to fix unit names in '+Filename+' '+CodeToolBoss.ErrorMessage);
80 
81   if MissingUnits<>nil then begin
82     writeln('MissingUnits=',MissingUnits.Text);
83     if not CodeToolBoss.CommentUnitsInUsesSections(Code,MissingUnits) then
84       raise Exception.Create('unable to comment units in uses section in '+Filename+' '+CodeToolBoss.ErrorMessage);
85     MissingUnits.Free;
86   end;
87 
88   writeln('==================================================================');
89   writeln(Code.Source);
90 end.
91 
92