home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / lxlt121s.zip / lxLite_src / common / CmdLine.pas next >
Pascal/Delphi Source File  |  1996-10-04  |  2KB  |  73 lines

  1. Unit CmdLine;
  2.  
  3. Interface
  4.  
  5. type
  6. { Parameter handling function definition }
  7.  ParmHandleFn = Function(var Parm : string) : Byte;
  8.  
  9. {Command line parsing helper. PH gets called on each /# and -# parameter;}
  10. {otherwise NH is called; they return number of chars starting from string}
  11. {start which have to be stripped (i.e. when they were already recognized)}
  12. {If ParmStr is #0 uses the real command line; otherwise ParmStr is used  }
  13.  Procedure ParseCommandLine(const ParmStr : string; PH,NH : ParmHandleFn);
  14. {ParseCommandLine additional helper to parse filenames and other stuff which}
  15. {can contain spaces. To specify an parameter (i.e. an filename) containing  }
  16. {spaces use " double quote " symbol. Retrieves the length of parsed string; }
  17. {however if parameter contains quotes they are removed from DestStr         }
  18.  Function  ParseName(var ParmStr : string; StartChar : Word; var DestStr : string) : Word;
  19.  
  20. Implementation uses Strings, strOp;
  21.  
  22. Procedure ParseCommandLine;
  23. var S : String;
  24. begin
  25.  if ParmStr = #0
  26.   then {$ifDef OS2}
  27.        S := StrPas(GetASCIIZptr(System.CmdLine^, 2))
  28.        {$else}
  29.        Move(mem[PrefixSeg:$80], S, succ(mem[PrefixSeg:$80]))
  30.        {$endIf}
  31.   else S := ParmStr;
  32.  While S <> '' do
  33.   begin
  34.    While (S <> '') and ((S[1] = ' ') or (S[1] = #9)) do Delete(S, 1, 1);
  35.    if S <> ''
  36.     then
  37.      if (S[1] in ['/','-'])
  38.       then begin
  39.             Delete(S, 1, 1);
  40.             if (@PH <> NIL) and (S <> '') then Delete(S, 1, PH(S));
  41.            end
  42.       else
  43.      if @NH <> NIL
  44.       then Delete(S, 1, NH(S))
  45.       else Delete(S, 1, 1);
  46.   end;
  47. end;
  48.  
  49. Function ParseName;
  50. var I,J : Word;
  51.     fCh : Char;
  52. begin
  53.  I := StartChar;
  54.  While (I < length(ParmStr)) and (ParmStr[I] in [#9, ' ']) do Inc(I);
  55.  if I <= length(ParmStr)
  56.   then begin
  57.         J := I;
  58.         if ParmStr[I] = '"'
  59.          then fCh := '"'
  60.          else fCh := ' ';
  61.         repeat
  62.          Inc(I);
  63.         until (I > length(ParmStr)) or (ParmStr[I] = fCh);
  64.         if fCh = '"'
  65.          then begin DestStr := Copy(ParmStr, succ(J), pred(I - J)); Inc(I); end
  66.          else DestStr := Copy(ParmStr, J, I - J);
  67.        end
  68.   else DestStr := '';
  69.  ParseName := I - StartChar;
  70. end;
  71.  
  72. end.
  73.