home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / comline / clparse / clparser.doc next >
Encoding:
Text File  |  1989-12-18  |  20.4 KB  |  534 lines

  1.  
  2.   ___________________________________________________________________________
  3.  
  4.   CLPARSER.TPU                                         (c)1989 Greg Truesdell
  5.   Version 2.1 (First Release)                  a Turbo Pascal 5.5 Object Unit
  6.   ___________________________________________________________________________
  7.  
  8.  
  9.  
  10.  
  11.  
  12.   INTRODUCTION_______________________________________________________________
  13.  
  14.   CLParser.TPU is a Command Line Parser.  It provides the programmer with
  15.   several pre-defined objects for dealing with command line options and
  16.   switches.  The unit also provides parsing for wildcard filenames, all
  17.   environment variables and simple text parameter files.
  18.  
  19.   The unit provides methods which manipulate lists of items.  The primary
  20.   object is LIST.  The following is the type definition for LIST:
  21.  
  22.     const
  23.          { maximum number of switches or arguments }
  24.          CLPARSE_MAXITEMS = 1024;
  25.  
  26.     List = object
  27.          { data structure }
  28.          item : array [1..CLPARSE_MAXITEMS] of ^String;
  29.          posn : array [1..CLPARSE_MAXITEMS] of word;
  30.          items, index : word;
  31.  
  32.          { methods }
  33.          constructor Init;
  34.          destructor  Erase;                                        virtual;
  35.          function    Add( Add_Item:string; locn:word ): boolean;   virtual;
  36.          function    Next: String;                                 virtual;
  37.          function    Prev: String;                                 virtual;
  38.          function    Count: word;                                  virtual;
  39.          function    Position: word;                               virtual;
  40.          procedure   Reset;                                        virtual;
  41.      end;
  42.  
  43.   The LIST object can be used to maintain any kind of sequential list of
  44.   strings.  Each item in the list is a pointer to a variable length string
  45.   and a location word.  The location word was primarily provided to register
  46.   where, on the command line, the item was found.  It may also be used to
  47.   mark entries in the list for other reasons.
  48.  
  49.   The minimum amount of memory used in the list is 1024 words + 1024 longs
  50.   (address pointers) + 2 words; or 6148 bytes.  The maximum number of item
  51.   entries is 1024 or as many as free memory will allow.  Each entry requires
  52.   only as many bytes are required by the string, plus two or three, depend-
  53.   ing on where the word boundries are.
  54.  
  55.   Since all other objects in this unit are based on this object, and because
  56.   none of these objects add any data instances of their own, the above
  57.   paragraph is true for them also.
  58.  
  59.  
  60.  
  61.   The following is a list of all objects defined in CLPARSER.TPU:
  62.  
  63.       name(type)  unique methods
  64.       ----------- --------------------------------------------------------
  65.       LIST        Init     :  Clear list to initial values (nil).
  66.       (object)    Erase    :  Erase and reclaim memory used by the list.
  67.                   Add      :  Add an item onto the list.
  68.                   Next     :  Step forward and get next item.
  69.                   Prev     :  Step backward and get previous item.
  70.                   Count    :  Return the number of items in the list.
  71.                   Position :  Return line position of this item.
  72.                   Reset    :  Reset the item pointer to the beginning.
  73.       ----------- --------------------------------------------------------
  74.       ARGUMENT    Init     :  Fill array with valid arguments.
  75.       (list)      Find     :  Locate a specific argument.
  76.                   Overflow :  Returns TRUE if there were more arguments
  77.                               than could fit in memory.
  78.       ----------- --------------------------------------------------------
  79.       WILD        Init     :  Fill the array with filenames that match the
  80.       (argument)              filename mask.
  81.       ----------- --------------------------------------------------------
  82.       ENVIRON     Init     :  Fill the array with all environment
  83.       (argument)              variables.
  84.       ----------- --------------------------------------------------------
  85.       PFILE       Init     :  Fill the array with all parameters from a
  86.       (argument)              text parameter file.
  87.       ----------- --------------------------------------------------------
  88.  
  89.   The LIST object is the primary object.  The ARGUMENT object inherits all of
  90.   LIST's methods except "Init", and adds two new methods; "Find" and
  91.   "Overflow".  The remaining objects (WILD, ENVIRON and PFILE) all inherit
  92.   the methods from LIST and ARGUMENT except "Init".  Once you understand how
  93.   to use LIST and ARGUMENT, then you need only examine the difference in the
  94.   "Init" methods for the remaining objects.
  95.  
  96.  
  97.  
  98.   HOW TO USE CLPARSER.TPU____________________________________________________
  99.  
  100.   To use CLPARSER, simply include the USES CLPARSER statement in your program
  101.   header.  You must also ensure that CLPARSER.TPU is placed in the directory
  102.   where you keep your units; usually \TP\UNITS.  CLPARSER will automatically
  103.   parse the commandline for you when the program executes.  The following is
  104.   a list of pre-defined data structures in CLPARSER:
  105.  
  106.       Automatically initialized when program starts:
  107.  
  108.           Parse_Argument  :  List of arguments from commandline
  109.           Parse_Switch    :  List of switches from commandline
  110.  
  111.       Available but must be initialized by the programmer:
  112.  
  113.           Parse_Wild      :  un-initialized list of filenames
  114.           Parse_Environ   :  un-initialized list of environment variables
  115.           Parse_File      :  un-initialized list of parameter file items
  116.  
  117.  
  118.   Data Structure Access Methods
  119.  
  120.   In the introduction section you will find a list of objects and unique
  121.   methods associated with them.  From that list you can see the data struct-
  122.   ure access methods available for the ARGUMENT and SWITCH objects.  Let's
  123.   first examine the automatically initialized data structures.
  124.  
  125.   Examine this program:
  126.  
  127.         program CLParser_Sample;
  128.  
  129.         uses CLParser;
  130.  
  131.         var I : Integer;
  132.  
  133.         begin
  134.             if Parse_Argument.Count > 0 then begin
  135.                 Write('There are ',Parse_Argument.Count );
  136.                 WriteLn(' arguments on the command line.');
  137.  
  138.                 for I := 1 to Parse_Argument.Count
  139.                 do WriteLn( Parse_Argument.Next );
  140.  
  141.                 WriteLn('End of Arguments');
  142.                 end
  143.             else WriteLn('There were NO arguments on the command line.');
  144.  
  145.             WriteLn;
  146.  
  147.             if Parse_Switch.Count > 0 then begin
  148.                 Write('There are ',Parse_Switch.Count );
  149.                 WriteLn(' arguments on the command line.');
  150.  
  151.                 for I := 1 to Parse_Switch.Count
  152.                 do WriteLn( Parse_Switch.Next );
  153.  
  154.                 WriteLn('End of Switches');
  155.                 end
  156.             else WriteLn('There were NO switches on the command line.');
  157.         end.
  158.  
  159.   When this program is compiled and executed, the two data structures are
  160.   automatically filled with items.  The unit determines which items on the
  161.   command line are switches or arguments by checking the first character
  162.   of the item.
  163.  
  164.        o    Switches are items that begin with '/', '-' or '+'.
  165.  
  166.        o    Arguments are items that begin with any normal character.
  167.             Normal characters are [#32..#127].
  168.  
  169.   The unit executes:
  170.  
  171.        Parse_Argument.Init( NormalChars-Switches );
  172.  
  173.                 and
  174.  
  175.        Parse_Switch.Init( Switches );
  176.  
  177.   when the program starts.  The initialization method is passed the set of
  178.   valid first characters for each data structure.  In the example program
  179.   above, the arguments listed would be any that do not start with a switch
  180.   character but DO start with a normal character.  All other items on the
  181.   commandline are IGNORED!
  182.  
  183.   Assume you have compiled the example program as SAMPLE.EXE and typed:
  184.  
  185.         SAMPLE ONE TWO /A THREE -B +C FOUR
  186.  
  187.   The program would display:
  188.  
  189.         There are 4 arguments on the command line.
  190.         ONE
  191.         TWO
  192.         THREE
  193.         FOUR
  194.  
  195.         There are 3 switches on the command line.
  196.         /A
  197.         -B
  198.         +C
  199.  
  200.   The methods used in the example are COUNT and NEXT.  COUNT returns the
  201.   number of items in the list. Parse_Argument.COUNT returns the number of
  202.   arguments parsed from the command line.  NEXT returns the next item in the
  203.   list; Parse_Argument.NEXT returns the next argument from the list.
  204.  
  205.   Here is a complete list of available COMMON methods:
  206.  
  207.         destructor  ERASE
  208.         function    ADD( add_item:string; locn: byte ): BOOLEAN
  209.         function    NEXT: STRING
  210.         function    PREV: STRING
  211.         function    POSITION: BYTE
  212.         function    COUNT: BYTE
  213.         procedure   RESET
  214.         function    FIND( target: string ): STRING
  215.         function    OVERFLOW: BOOLEAN
  216.  
  217.         examples:
  218.  
  219.                 Current_Position := Parse_Switch.Position;
  220.                 Next_Argument := Parse_Argument.Next;
  221.                 Parse_Switch.Reset;
  222.                 If Parse_Argument.Add('SETUP.DAT',0) then .....
  223.  
  224.   Here is a list of available UNIQUE methods:
  225.  
  226.     Automatic:
  227.  
  228.         Parse_Argument.Init( LegalChars: CharSet )
  229.         Parse_Switch.Init( LegalChars: CharSet )
  230.  
  231.     Programmer:
  232.  
  233.         Parse_Wild.Init( FileMask: string; Attributes: word )
  234.         Parse_Environ.Init
  235.         Parse_File.Init( Filename: string; Comment: CharSet )
  236.  
  237.  
  238.  
  239.   REFERENCE__________________________________________________________________
  240.  
  241.  
  242.   Method:       ADD( add_item: string; locn: byte ): BOOLEAN
  243.  
  244.   Type:         Function
  245.  
  246.   Definition:   The ADD method is used to add an item to the end of the
  247.                 list. If the ADD is successful, the function returns TRUE.
  248.                 Failure to ADD occurs if the list is full (1024 items max)
  249.                 or there is not enough memory on the heap for a new item.
  250.  
  251.                 The LOCN parameter is normally used with Parse_Argument and
  252.                 Parse_Switch to indicate where on the command line an item
  253.                 was found. It can be used by the programmer to supply a
  254.                 unique or special code to an item.
  255.  
  256.  
  257.   Usage:        If not Parse_Wild.ADD('C:\COMMAND.COM',0)
  258.                 Then WriteLn('Not enough room!');
  259.  
  260.   ___________________________________________________________________________
  261.  
  262.   Method:       COUNT: WORD
  263.  
  264.   Type:         Function
  265.  
  266.   Definition:   The COUNT method returns the number of items in the list.
  267.                 If the list is empty then COUNT returns 0.
  268.  
  269.   Usage:        If Parse_Switch.COUNT = 0
  270.                 Then WriteLn('No Switches on the command line.');
  271.  
  272.   ___________________________________________________________________________
  273.  
  274.   Method:       ERASE
  275.  
  276.   Type:         Destructor
  277.  
  278.   Definition:   ERASE will reclaim memory used by the list and reset all
  279.                 entries to nil.
  280.  
  281.   Usage:        Parse_Environ.ERASE;       ... or ...
  282.  
  283.                 Dispose(Parse_Environ, ERASE);
  284.  
  285.   ___________________________________________________________________________
  286.  
  287.   Method:       FIND( target: string ): STRING
  288.  
  289.   Type:         Function
  290.  
  291.   Definition:   FIND is used to locate a given item in the list.  If a match
  292.                 is found then FIND will return the matching item. If the
  293.                 target is shorter that the item compared, FIND will match
  294.                 partial string.  ie. to find "/P=<filename>" use:
  295.  
  296.   Usage:        Parameter_File := Parse_Switch.FIND('/P=');
  297.  
  298.                 If length(Parameter_File) > 0
  299.                 Then Filename := copy(Parameter_File,4,255)
  300.                 Else Filename := '';
  301.   ___________________________________________________________________________
  302.  
  303.   Method:       Parse_Argument.INIT( LegalChars: CharSet )
  304.  
  305.   Type:         Constructor
  306.  
  307.   Definition:   This instance of INIT is used to fill the ARGUMENT list
  308.                 with items from the command line.  LegalChars is a set
  309.                 of characters to be used as indicators of legal arguments.
  310.  
  311.                 CLPARSER defaults to [#32..#127] - ['/','-','+'].
  312.  
  313.                 WARNING!  Do not use this command in your program until
  314.                           you have used Parse_Argument.ERASE first!  Since
  315.                           this command is automatically executed when your
  316.                           program starts, allocated memory would become
  317.                           trapped and unusable!
  318.  
  319.   Usage:        Parse_Argument.ERASE; { REMEMBER TO DO THIS! }
  320.                 Parse_Argument.INIT( ['0'..'9'] ); { scan for numbers only }
  321.  
  322.   ___________________________________________________________________________
  323.  
  324.   Method:       Parse_Environ.INIT
  325.  
  326.   Type:         Constructor
  327.  
  328.   Definition:   This instance of INIT fills the ENVIRON list with all
  329.                 environment variables that will fit.  This is not an
  330.                 automatic-execution object.  You MUST use this first
  331.                 before you can access the data structure.
  332.  
  333.   Usage:        Parse_Environ.INIT
  334.  
  335.   ___________________________________________________________________________
  336.  
  337.   Method:       Parse_File.INIT( filename: string, Comment: CharSet )
  338.  
  339.   Type:         Constructor
  340.  
  341.   Definition:   This is one of the more interesting objects in this unit.
  342.                 If the file specified by filename is found, then it is
  343.                 parsed for items to be added to the list.  White space
  344.                 before and after the item is stipped.  Blank lines and
  345.                 comment lines are skipped.  Comment lines are defined
  346.                 by the set of characters specified by Comment.  Any line
  347.                 whose first non-white space (tab or space) character is
  348.                 in the Comment set of characters is skipped.
  349.  
  350.   Usage:        Example Parameter File:  PARM.DAT
  351.  
  352.                         ; Sample Parameter file
  353.  
  354.                         *************  User Data *************
  355.  
  356.                         John Smith                               {1}
  357.                         Level=10                                 {2}
  358.                         Password=Not Now, Dear!                  {3}
  359.  
  360.                         *************  System Data ***********
  361.  
  362.                         Size=100                                 {4}
  363.                         Time=1:00                                {5}
  364.                         Delay=1000                               {6}
  365.  
  366.                         ; end of parameter file
  367.  
  368.                 Program example:
  369.  
  370.                         Parse_File.Init('PARM.DAT',[';','*']);
  371.  
  372.                 The {} numbers represent the position numbers in the list
  373.                 after execution.  The list will contain only 6 items.  All
  374.                 other lines have been skipped.
  375.  
  376.   ___________________________________________________________________________
  377.  
  378.   Method:       Parse_Switch.INIT( LegalChars: CharSet )
  379.  
  380.   Type:         Constructor
  381.  
  382.   Definition:   This instance of INIT is used to fill the SWITCH list
  383.                 with items from the command line.  LegalChars is a set
  384.                 of characters to be used as indicators of legal switches.
  385.  
  386.                 CLPARSER defaults to ['/','-','+'].
  387.  
  388.                 WARNING!  Do not use this command in your program until
  389.                           you have used Parse_Switch.ERASE first!  Since
  390.                           this command is automatically executed when your
  391.                           program starts, allocated memory would become
  392.                           trapped and unusable!
  393.  
  394.   Usage:        Parse_Switch.ERASE; { REMEMBER TO DO THIS! }
  395.                 Parse_Switch.INIT( ['%','@','/','-','+'] );
  396.  
  397.   ___________________________________________________________________________
  398.  
  399.   Method:       Parse_Wild.INIT( filemask:string; attributes:word );
  400.  
  401.   Type:         Constructor
  402.  
  403.   Definition:   This method fills the WILD list with items (filenames) that
  404.                 that match the filename mask.  This can build a potentially
  405.                 gigantic list.  CLPARSER is limited to 1024 items up to
  406.                 the maximum memory can handle.
  407.  
  408.                 The attributes parameter is identical to the attributes
  409.                 provided by Turbo's DOS unit.
  410.  
  411.   Usage:        Parse_Wild.INIT('*.DOC',ANYFILE-HIDDEN-VOLUMEID);
  412.  
  413.   ___________________________________________________________________________
  414.  
  415.   Method:       NEXT: STRING
  416.  
  417.   Type:         Function
  418.  
  419.   Definition:   NEXT returns the next item in the list.  If the last item
  420.                 has already been accessed, then NEXT returns an empty
  421.                 string.  Use RESET to set the internal index back to the
  422.                 beginning of the list.  Use PREV to return the previous
  423.                 item.
  424.  
  425.   Usage:        Next_Item := Parse_Wild.NEXT;
  426.  
  427.   ___________________________________________________________________________
  428.  
  429.   Method:       OVERFLOW: BOOLEAN
  430.  
  431.   Type:         Function
  432.  
  433.   Definition:   Returns TRUE if INIT found more items than would fit in
  434.                 memory or in the list.
  435.  
  436.   Usage:        If Parse_Wild.OVERFLOW
  437.                 Then WriteLn('Sorry, not enough space for all files!');
  438.  
  439.  
  440.   ___________________________________________________________________________
  441.  
  442.   Method:       POSITION: WORD
  443.  
  444.   Type:         Function
  445.  
  446.   Definition:   When used with Parse_Argument or Parse_Switch POSITION
  447.                 returns the position on the command line where the current
  448.                 item was found.
  449.  
  450.                 Otherwise POSITION returns whatever value the programmer
  451.                 placed there with the ADD method.
  452.  
  453.   Usage:        Current_Switch := Parse_Switch.NEXT;
  454.                 If Parse_Switch.POSITION < Parse_Argument.POSITION
  455.                 Then WriteLn('Ambiguous use of ',Current_Switch);
  456.  
  457.   ___________________________________________________________________________
  458.  
  459.   Method:       PREV: STRING
  460.  
  461.   Type:         Function
  462.  
  463.   Definition:   PREV backs up one entry and returns that item in the list.
  464.                 If already at the beginning, then PREV returns an empty
  465.                 string.
  466.  
  467.   Usage:        Previous_Item := Parse_Environ.PREV;
  468.  
  469.   ___________________________________________________________________________
  470.  
  471.   Method:       RESET
  472.  
  473.   Type:         Procedure
  474.  
  475.   Definition:   RESET simply resets the data structure's current entry
  476.                 index pointer to the beginning of the list.
  477.  
  478.   Usage:        If Parse_Argument.NEXT := '' then Parse_Argument.RESET;
  479.  
  480.   ___________________________________________________________________________
  481.  
  482.  
  483.  
  484.   REGISTRATION_______________________________________________________________
  485.  
  486.  
  487.   There is no charge for using this unit unless you are profitting from
  488.   it's use or you want the source code.  Commercial users MUST register
  489.   irregardless of whether you sell software or not.
  490.  
  491.  
  492.   This version of CLPARSER.TPU is available only for Turbo Pascal 5.5.  This
  493.   is the only version, currently, that supports objects.  Since the unit is
  494.   in object format, you can make use of the primary objects in your own
  495.   programs.  You are free to use this unit as-is or as a basis for your own
  496.   objects under the following restrictions:
  497.  
  498.       o  Using CLParser.TPU for any commercial purpose, you must register
  499.          for a single-site license fee of $12.00.  No royalties are required
  500.          beyond the site-license.
  501.  
  502.       o  To use CLParser.TPU in a Public Domain release you must register.
  503.          Registration is free, but, please mention that CLPARSER was used
  504.          somewhere in your documentation.
  505.  
  506.       o  To use CLParser.TPU with your own personal programs, no registrat-
  507.          ion is required.
  508.  
  509.   To obtain the source code you must register for a source-code license of
  510.   $20.00.  This license covers all above mentioned licenses and source for
  511.   the TP5.5 TPU version only.
  512.  
  513.   When registering, please remember to include your:
  514.  
  515.                         Name
  516.                         Company name
  517.                         Address
  518.                         City, State/Province
  519.                         Postal Code.
  520.  
  521.   If you are paying for registration, send MONEY ORDERS ONLY, please.
  522.  
  523.   Also available are non-object versions of CLParser.TPU for TP4 and TP5.
  524.   To recieve a disk with all versions, on disk manual and demo programs,
  525.   send $6.00 to:
  526.  
  527.                         Greg L. Truesdell
  528.                         1740 Lilac Drive
  529.                         Surrey, BC  CANADA  V4A 5C9
  530.  
  531.                         CIS: 75360,1303
  532.  
  533.  
  534.