PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/dependencycalculator/tsort2.pas

https://bitbucket.org/reiniero/smalltools
Pascal | 217 lines | 143 code | 21 blank | 53 comment | 2 complexity | a788745391ddd71d498f88ac7002f9bd MD5 | raw file
  1. Program tsort2;
  2. { Unix tsort clone for multiple platforms (Windows, OS X, Linux)
  3. Topological sorter to parse e.g. dependencies.
  4. Copyright (C) 2010-2012 Reinier Olislagers
  5. This library is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Library General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at your
  8. option) any later version with the following modification:
  9. As a special exception, the copyright holders of this library give you
  10. permission to link this library with independent modules to produce an
  11. executable, regardless of the license terms of these independent modules,and
  12. to copy and distribute the resulting executable under terms of your choice,
  13. provided that you also meet, for each linked independent module, the terms
  14. and conditions of the license of that module. An independent module is a
  15. module which is not derived from or based on this library. If you modify
  16. this library, you may extend this exception to your version of the library,
  17. but you are not obligated to do so. If you do not wish to do so, delete this
  18. exception statement from your version.
  19. This program is distributed in the hope that it will be useful, but WITHOUT
  20. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  22. for more details.
  23. You should have received a copy of the GNU Library General Public License
  24. along with this library; if not, write to the Free Software Foundation,
  25. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. }
  27. {$IFDEF FPC}{$mode objfpc}{$ENDIF}
  28. Uses
  29. {$IFDEF UNIX}
  30. cwstring, {* widestring support for unix *}
  31. {$IFDEF UseCThreads}
  32. cthreads,
  33. {$ENDIF UseCThreads}
  34. {$ENDIF UNIX}
  35. Classes,
  36. SysUtils,
  37. CustApp,
  38. topologicalsort {*for actual sorting *};
  39. Type
  40. { Tsort2 }
  41. TTsort2 = Class(TCustomApplication)
  42. Protected
  43. Procedure DoRun;
  44. override;
  45. Public
  46. constructor Create(TheOwner: TComponent);
  47. override;
  48. destructor Destroy;
  49. override;
  50. Procedure WriteHelp;
  51. Procedure WriteVersion;
  52. End;
  53. { Tsort2 }
  54. Procedure TTsort2.DoRun;
  55. Var
  56. ErrorMsg: string;
  57. InputFile: string;
  58. InputList: TStringList;
  59. TopSort: TTopologicalSort;
  60. OutputList: TStringList;
  61. Counter: Integer;
  62. LongOptions: TStringList;
  63. //Long format possible options
  64. OptionValues: TStringList;
  65. //Command line options extracted by CheckOptions
  66. OtherArguments: TStringList;
  67. //Other command line arguments (that are no options) extracted by CheckOptions
  68. Begin
  69. ErrorMsg := '';
  70. InputFile := '';
  71. LongOptions := TStringList.Create;
  72. LongOptions.Add('help');
  73. LongOptions.Add('version');
  74. OptionValues := TStringList.Create;
  75. OtherArguments := TStringList.Create;
  76. // Check parameters; parse into results
  77. ErrorMsg := CheckOptions('hv', LongOptions, OptionValues, OtherArguments);
  78. If ErrorMsg <> '' Then
  79. Begin
  80. //ShowException(Exception.Create(ErrorMsg)); // a bit too harsh
  81. writeln('Wrong command or option specified: ');
  82. writeln(ErrorMsg);
  83. writeln();
  84. WriteHelp;
  85. LongOptions.Free;
  86. OptionValues.Free;
  87. OtherArguments.Free;
  88. Terminate;
  89. Exit;
  90. End;
  91. // parse parameters
  92. If HasOption('h', 'help') Then
  93. Begin
  94. WriteHelp;
  95. LongOptions.Free;
  96. OptionValues.Free;
  97. OtherArguments.Free;
  98. Terminate;
  99. Exit;
  100. End;
  101. If HasOption('v', 'version') Then
  102. Begin
  103. WriteVersion;
  104. LongOptions.Free;
  105. OptionValues.Free;
  106. OtherArguments.Free;
  107. Terminate;
  108. Exit;
  109. End;
  110. // Get input file
  111. Begin
  112. // User could have just specified a InputFile
  113. If OtherArguments.Count > 0 Then
  114. Begin
  115. InputFile := OtherArguments[0];
  116. End
  117. Else // input from terminal
  118. Begin
  119. InputFile := '';
  120. //default value we rely on later
  121. End;
  122. End;
  123. Begin
  124. // Free up command line option-related varliabes
  125. LongOptions.Free;
  126. OptionValues.Free;
  127. OtherArguments.Free;
  128. //Actual sort
  129. InputList := TStringList.Create;
  130. TopSort := TTopologicalSort.Create;
  131. OutputList := TStringList.Create;
  132. try
  133. if InputFile='' then
  134. begin
  135. writeln(' **** standard input not implemented yet *** todo***');
  136. halt(1);
  137. end;
  138. InputList.LoadFromFile(InputFile);
  139. TopSort.Sort(InputList, OutputList);
  140. for Counter := 0 to OutputList.Count -1 do
  141. begin
  142. writeln(OutputList[Counter]);
  143. end;
  144. finally
  145. OutputList.Free;
  146. TopSort.Free;
  147. InputList.Free;
  148. end;
  149. // stop program loop
  150. End;
  151. Terminate;
  152. End;
  153. constructor TTsort2.Create(TheOwner: TComponent);
  154. Begin
  155. inherited Create(TheOwner);
  156. StopOnException := True;
  157. End;
  158. destructor TTsort2.Destroy;
  159. Begin
  160. inherited Destroy;
  161. End;
  162. Procedure TTsort2.WriteHelp;
  163. Begin
  164. writeln(Title, ' usage: tsort2 [OPTION] [FILE]');
  165. writeln(' Write totally ordered list consistent with the partial ordering in FILE.');
  166. writeln(' With no FILE, or when FILE is -, read standard input.');
  167. writeln(' **** standard input not implemented yet *** todo***');
  168. writeln(' ');
  169. writeln(' --help display this help and exit');
  170. writeln(' --version: output version information and exit');
  171. writeln('Freeware but no warranties, express or implied.');
  172. writeln('For full copyright and license, please see the source code.');
  173. End;
  174. Procedure TTsort2.WriteVersion;
  175. Begin
  176. writeln('Version: we don''t really have a version number.');
  177. End;
  178. Var
  179. Application: TTsort2;
  180. Begin
  181. Application := TTsort2.Create(Nil);
  182. Application.Title := 'sort2';
  183. {$IFDEF CRAZYDEBUG}
  184. writeln(stderr, 'Debug: ', DateTimeToStr(Now), ': Application started.');
  185. {$ENDIF}
  186. Application.Run;
  187. {$IFDEF CRAZYDEBUG}
  188. writeln(stderr, 'Debug: ', DateTimeToStr(Now), ': Application finished.');
  189. {$ENDIF}
  190. Application.Free;
  191. End.