makenamespaceslist.lpr 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. program makenamespaceslist;
  2. {$mode objfpc}
  3. {$h+}
  4. uses sysutils, classes;
  5. Function ExtractInclude(const aFileName : string) : string;
  6. Const
  7. SInclude = '{$include ';
  8. SI = '{$i ';
  9. var
  10. aFile: TStringList;
  11. FN,S : String;
  12. P : Integer;
  13. begin
  14. Result:='';
  15. aFile:=TStringList.Create;
  16. try
  17. aFile.LoadFromFile(aFileName);
  18. For S in aFile do
  19. begin
  20. FN:=S;
  21. P:=Pos(SInclude,LowerCase(FN));
  22. if P<>0 then
  23. Delete(FN,1,P+Length(SInclude)-1)
  24. else
  25. begin
  26. P:=Pos(SI,LowerCase(FN));
  27. if P<>0 then
  28. Delete(FN,1,P+Length(SI)-1)
  29. end;
  30. if P<>0 then
  31. begin
  32. P:=Pos('}',FN);
  33. if P>0 then
  34. begin
  35. FN:=Trim(Copy(FN,1,P-1));
  36. FN:=ExtractFilePath(aFileName)+FN;
  37. Result:=ExpandFileName(FN);
  38. end;
  39. end;
  40. end;
  41. finally
  42. aFile.Free;
  43. end;
  44. end;
  45. Procedure AddNameSpaces(const aBaseDir,aSubDir : String; aList : TStrings; IsKnownList : Boolean);
  46. var
  47. Info : TSearchRec;
  48. Ext : string;
  49. NS,NonNS: String;
  50. begin
  51. Writeln('Examining dir: ',aSubDir+AllFilesMask);
  52. if FindFirst(aSubDir+AllFilesMask,0,Info)=0 then
  53. try
  54. Repeat
  55. if ((Info.Attr and faDirectory)=0) then
  56. begin
  57. Ext:=ExtractFileExt(Info.Name);
  58. Writeln('Examining ',Info.Name);
  59. if SameText(Ext,'.pp') or SameText(Ext,'.pas') then
  60. begin
  61. NS:=aSubDir+Info.Name;
  62. NonNS:=ExtractInclude(NS);
  63. Writeln(NS,' -> ',NonNS);
  64. if NonNS<>'' then
  65. begin
  66. if IsKnownList then
  67. begin
  68. NS:='*'+ChangeFileExt(ExtractFileName(NS),'');
  69. NonNS:=ChangeFileExt(ExtractFileName(NonNS),'');
  70. end
  71. else
  72. begin
  73. NS:=ExtractRelativePath(aBaseDir,NS);
  74. NonNS:=ExtractRelativePath(aBaseDir,NonNS);
  75. end;
  76. aList.Add(NonNS+'='+NS);
  77. end;
  78. end;
  79. end;
  80. Until (FindNext(Info)<>0);
  81. finally
  82. FindClose(Info);
  83. end;
  84. end;
  85. Procedure CreateNameSpaces(const aBaseDir : string; const aListFile : String; MakeKnownList : Boolean);
  86. var
  87. L : TStringList;
  88. Info : TSearchRec;
  89. Subdir : string;
  90. begin
  91. L:=TStringList.Create;
  92. try
  93. if FindFirst(aBaseDir+AllFilesMask,faDirectory,Info)=0 then
  94. try
  95. Repeat
  96. if ((Info.Attr and faDirectory)=faDirectory) and
  97. Not ((Info.Name='.') or (Info.Name='..')) then
  98. begin
  99. SubDir:=aBaseDir+Info.Name+PathDelim+'namespaced'+PathDelim;
  100. if DirectoryExists(SubDir) then
  101. AddNameSpaces(aBaseDir,SubDir,L,MakeKnownList);
  102. end;
  103. Until FindNext(Info)<>0;
  104. finally
  105. FindClose(Info);
  106. end;
  107. if L.Count>0 then
  108. begin
  109. Writeln('Writing ',L.Count,' namespaces to ',aListFile);
  110. L.SaveToFile(aListFile)
  111. end
  112. else
  113. Writeln('Error : no namespaced files found');
  114. finally
  115. L.Free;
  116. end;
  117. end;
  118. var
  119. ListFile,BaseDir : String;
  120. MakeKnownList : Boolean;
  121. begin
  122. BaseDir:=ParamStr(1);
  123. if BaseDir='-k' then
  124. begin
  125. MakeKnownList:=True;
  126. BaseDir:=ParamStr(2);
  127. end;
  128. if (BaseDir='') then
  129. begin
  130. Writeln('Usage : ',ExtractFileName(Paramstr(0)),' [-k] DIR [LISTFILE]');
  131. Writeln('If Listfile is not specified then it defaults to : DIR/namespaces.lst');
  132. Halt(1);
  133. end;
  134. BaseDir:=IncludeTrailingPathDelimiter(BaseDir);
  135. ListFile:=ParamStr(2+Ord(MakeKNownList));
  136. if ListFile='' then
  137. if MakeKnownList then
  138. ListFile:=BaseDir+'knownaliases.lst'
  139. else
  140. ListFile:=BaseDir+'namespaces.lst';
  141. CreateNameSpaces(BaseDir,ListFile,MakeKnownList);
  142. end.