home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / comline / clparse / clpdemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-12-18  |  8.2 KB  |  206 lines

  1. (*--------------------------------------------------------------------------*)
  2. (*                                                                          *)
  3. (* Program        CLPDEMO.PAS                                               *)
  4. (*                                                                          *)
  5. (* Description    Demonstrates the use of CLPARSER.TPU.                     *)
  6. (*                                                                          *)
  7. (*                The program will also look for the command line option    *)
  8. (*                _F=<filename mask>.  If the special switch _F= is found,  *)
  9. (*                the resulting matching filenames is loaded into the WILD  *)
  10. (*                list and displayed.                                       *)
  11. (*                                                                          *)
  12. (* Modifications  Released 07/26/88 v1.0                                    *)
  13. (*                                                                          *)
  14. (* Author         Greg Truesdell                                            *)
  15. (*                                                                          *)
  16. (* Copyright      Unrestricted.                                             *)
  17. (*                                                                          *)
  18. (*--------------------------------------------------------------------------*)
  19.  
  20. program CLPDEMO;
  21.  
  22. uses
  23.     dos, CLParser;
  24.  
  25. const
  26.     NormalChars : set of char = [#32..#127];    { ARGUMENT trigger       }
  27.     SwitchChars : set of char = ['/','-','+'];  { SWITCH trigger         }
  28.     SpecialChars: set of char = ['_','&'];      { Special SWITCH trigger }
  29.  
  30. var
  31.     i : integer;    { the infamous interator    }
  32.     ta : string;    { temporary argument string }
  33.  
  34.     { object pointers }
  35.     ArgumentPtr : ^ Argument;   { pointer to argument object }
  36.     SwitchPtr   : ^ Switch;     { pointer to switch   object }
  37.     WildPtr     : ^ Wild;       { pointer to wild     object }
  38.  
  39. (*-----------------------------------------------------------*)
  40. (*  DisplayList( ListObject, Message )                       *)
  41. (*                                                           *)
  42. (*     Displays the list of items in ListObject.  Prints     *)
  43. (*     the message at the end of the list.                   *)
  44. (*                                                           *)
  45. (*-----------------------------------------------------------*)
  46. procedure DisplayList ( ListObject : Argument; Message: string );
  47. var
  48.     ta : string;        { temp string }
  49.     Add_OK : boolean;   { was-add-successful? flag }
  50.  
  51. begin
  52.     { exit if no items in the list }
  53.     if ListObject.Count = 0 then exit;
  54.  
  55.     { write a header for the list }
  56.     writeln;
  57.     writeln(Message+':');
  58.     writeln('Item# Position# Item');
  59.     writeln('----- --------- ---------------------------------------');
  60.     Add_OK := ListObject.Add( 'End of '+Message,0);
  61.  
  62.     { display all of the list entries }
  63.     for i := 1 to ListObject.Count do begin
  64.         ta := ListObject.Next;
  65.         writeln( i:5,ListObject.Position:10,' ',ta );
  66.     end;
  67.  
  68.     { report if the list was overflowed }
  69.     if ListObject.Overflow then Writeln('Overflow!');
  70.  
  71.     { let us know if the add was not successful }
  72.     if not Add_OK then WriteLn('Couldn''t add comment!');
  73. end;
  74.  
  75. (*-----------------------------------------------------------*)
  76. (*  Do_Demo                                                  *)
  77. (*                                                           *)
  78. (*    This procedure demonstrates how the CLPARSER unit      *)
  79. (*    works.  Every object is demo-ed, but not all methods   *)
  80. (*    are used.                                              *)
  81. (*                                                           *)
  82. (*-----------------------------------------------------------*)
  83. procedure Do_Demo;
  84. begin
  85.     { display the primary lists }
  86.     DisplayList( Parse_Argument, 'Argument List');
  87.     DisplayList( Parse_Switch, 'Switch List');
  88.     DisplayList( Parse_Environ, 'Environment List');
  89.  
  90.     { search for a specific item in a list }
  91.     if Parse_Environ.Find('path=') <> '' then begin
  92.         writeln;
  93.         writeln('Found PATH Environment Variable ---');
  94.         writeln('      ',Parse_Environ.Find('path=') );
  95.     end;
  96.  
  97.     { Release the current switch list and parse for special switches }
  98.     Parse_Switch.Erase;
  99.     Parse_Switch.Init( SpecialChars );
  100.     DisplayList( Parse_Switch, 'Special List');
  101.  
  102.     { get the volume id }
  103.     Parse_Wild.Init('\*.*',VolumeID);
  104.  
  105.     { if found, display it }
  106.     if Parse_Wild.Count > 0 then begin
  107.         ta := Parse_Wild.Next;
  108.         delete(ta,9,1);
  109.         writeln;
  110.         writeln('Volume ID: ', ta);
  111.     end;
  112.  
  113.     { check for _f= switch for file mask ( ie. CLPDEMO _F=*.PAS )}
  114.     { if it is there, use it }
  115.     ta := Parse_Switch.Find('_f');      { returns '' if not found }
  116.     if ta <> ''
  117.     then ta := copy(ta,4,length(ta)-3)  { get mask from command line }
  118.     else ta := '*.*';                   { otherwise assume all files }
  119.  
  120.     { clear the current wild list and parse for new list }
  121.     Parse_Wild.Erase;
  122.  
  123.     { fill the list with matching files (not directories or the volume id }
  124.     Parse_Wild.Init(ta,AnyFile-Directory-VolumeID);
  125.  
  126.     { display the new list }
  127.     DisplayList( Parse_Wild , 'Wildcard List ('+ ta + ')');
  128.  
  129.     { erase the current wild list }
  130.     Parse_Wild.Erase;
  131.  
  132.     { now parse the directory for directories }
  133.     Parse_Wild.Init( ta, Directory );
  134.  
  135.     { display the resulting list, if any }
  136.     DisplayList( Parse_Wild, 'Directory List ('+ ta + ')');
  137.  
  138.     { now display the list of parameters from the sample parameter file }
  139.     DisplayList( Parse_File, 'Parameter File List (CLPDEMO.DAT)');
  140. end;
  141.  
  142. (*-----------------------------------------------------------*)
  143. (*  Initialize_List                                          *)
  144. (*                                                           *)
  145. (*    This procedure re-initializes the argument list to     *)
  146. (*    exclude arguments starting with special characters.    *)
  147. (*    These special characters will be used later as switch  *)
  148. (*    indicators.                                            *)
  149. (*                                                           *)
  150. (*    The Environ list is also initialized since it is not   *)
  151. (*    automatic on start-up.                                 *)
  152. (*                                                           *)
  153. (*    The demo parameter file is loaded into the file list.  *)
  154. (*    (CLPDEMO.DAT)                                          *)
  155. (*                                                           *)
  156. (*-----------------------------------------------------------*)
  157. procedure Initialize_Lists;
  158. begin
  159.     { release auto-loaded arguments }
  160.     Parse_Argument.Erase;
  161.  
  162.     { parse for arguments - switches - specials }
  163.     { this ensures that the special switch char }
  164.     { is not parsed as an argument              }
  165.     Parse_Argument.Init( NormalChars-SwitchChars-SpecialChars );
  166.  
  167.     { fill the environ list }
  168.     Parse_Environ.Init;
  169.  
  170.     { fill the parameter file list using ';' and '*' as comment tokens }
  171.     Parse_File.Init( 'CLPDEMO.DAT', [';','*'] );
  172. end;
  173.  
  174. (*-----------------------------------------------------------*)
  175. (*  Erase_Lists                                              *)
  176. (*                                                           *)
  177. (*    Erases lists and reclaims memory used.                 *)
  178. (*                                                           *)
  179. (*-----------------------------------------------------------*)
  180. procedure Erase_Lists;
  181. begin
  182.     Parse_Argument.Erase;
  183.     Parse_Switch.Erase;
  184.     Parse_Wild.Erase;
  185.     Parse_Environ.Erase;
  186.     Parse_File.Erase;
  187. end;
  188.  
  189.  
  190. (*-------------------------------------------------------*)
  191. (*                                                       *)
  192. (*                M      A      I      N                 *)
  193. (*                                                       *)
  194. (*-------------------------------------------------------*)
  195.  
  196. BEGIN
  197.     { turn redirection on, which means you can redirect the output }
  198.     { to a file, if you want to, with CLPDEMO > filename.ext       }
  199.     Assign(Output, '');  Rewrite(Output);
  200.     Assign(Input, '');   Reset(Input);
  201.  
  202.     Initialize_Lists;
  203.     Do_Demo;
  204.     Erase_Lists;
  205. END.
  206.