Program CpDemo3;


Uses
  CmdParse;


Function Clip(s : String) : String;

Var
  z : Byte absolute s;

Begin
   If (s <> '') then
     While (Copy(s,z,1) = ' ') do Delete(s,z,1);
   Clip := s;
End;


Function Left(s : String) : String;

Begin
   If (s <> '') then Begin
     While (Copy(s,1,1) = ' ') do Begin
       Delete(s,1,1);
       s := s + ' ';
     End;
   End;
   Left := s;
End;


{$F+}

Function FullClip(s : String) : String;

Begin
  FullClip := Clip(Left(s));
End;


Function Upper(s : String) : String;

Var
   q : Byte;
   z : Byte absolute s;

Begin
   If (s <> '') then
     For q := 1 to z do s[q] := UpCase(s[q]);
   Upper := s;
End;


Function GetWord(n : Byte; s : String; d : Chars) : String;

  Function wPos(n : Byte; s : String; d : Chars) : Byte;

  Var
    c : Byte;
    q : Word;
    z : Byte absolute s;

  Begin
    c := 0;
    q := 1;
    wPos := 0;
    While (q <= z) and (c <> n) do Begin
      While (q <= z) and (s[q] in d) do
        Inc(q);
      If (q <= z) then
        Inc(c);
      If (c <> n) then
        While (q <= z) and Not (s[q] in d) do
          Inc(q)
      Else
        wPos := q;
    End;
  End;

Var
  q,
  p : Byte;
  t : String;
  z : Byte absolute s;

Begin
  p := 0;
  t := '';
  q := wPos(n,s,d);
  If (q <> 0) then
    While (q <= z) and Not (s[q] in d) do Begin
      Inc(p);
      t[p] := s[q];
      Inc(q);
    End;
  t[0] := Char(p);
  GetWord := t;
End;

{$F-}


Begin
  SetStrFuncs(Upper, FullClip, GetWord);
  WriteLn('CpDemo #3');
  WriteLn('Using ',CmdParseVer);
  WriteLn;
  ComPath.Init('ev2','aedtv5',True,True);
  If ComPath.IsHelp then
    WriteLn('Usage: CPDEMO3 [[!]-aedtv#] source [destination]')
  Else If ComPath.IsBad then
    WriteLn('Invalid option(s): -',ComPath.GetBad)
  Else If (ComPath.ParmCount > 2) then
    WriteLn('Invalid parameter(s)')
  Else If (ComPath.ParmCount < 1) then
    WriteLn('Source must be specified')
  Else Begin
    WriteLn('Source is ',Upper(ComPath.GetParm(1)));
    If (ComPath.ParmCount > 1) then
      WriteLn('Destination is ',Upper(ComPath.GetParm(2)));
    If (ComPath.CheckOpt('A') = 'A') then
      WriteLn('-a option is set');
    If (ComPath.CheckOpt('E') = 'E') then
      WriteLn('-e option is set');
    If (ComPath.CheckOpt('D') = 'D') then
      WriteLn('-d option is set');
    If (ComPath.CheckOpt('T') = 'T') then
      WriteLn('-t option is set');
     WriteLn('-v level set to ',ComPath.CheckOpt('V'));
  End;
  ComPath.Done;
End.
