home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff339.lzh / PCQ / Runtime.lzh / Runtime / Extras / SameName.p < prev    next >
Text File  |  1989-10-21  |  2KB  |  81 lines

  1. External;
  2.  
  3. {
  4.     SameName.p
  5.  
  6.     This include file contains only one function: SameName.
  7. This routine implements the simplest parts of AmigaDOS pattern matching.
  8. At this point that's just the  # and ? operators, plus the single quote ',
  9. which you place before a # or ? meant to be matched literaly.  Check out
  10. the AmigaDOS books for more information.
  11.     The source for this is in Runtime/Extras, and the object code
  12. is in PCQ.lib
  13. }
  14.  
  15.  
  16. Function SameName(Mask, Target : String) : Boolean;
  17.  
  18. type
  19.     CompState = (initial, two_char, char_series, any_char, star);
  20. var
  21.     MaskPos,
  22.     TargetPos : Short;
  23.     State : CompState;
  24. begin
  25.     MaskPos := 0;
  26.     TargetPos := 0;
  27.     State := initial;
  28.     while true do
  29.     case State of
  30.       initial : case Mask[MaskPos] of
  31.               '#' : begin
  32.                 MaskPos := Succ(MaskPos);
  33.                 State := char_series;
  34.                 end;
  35.               '?' : begin
  36.                 MaskPos := Succ(MaskPos);
  37.                 State := any_char;
  38.                 end;
  39.              '\'' : begin
  40.                 MaskPos := Succ(MaskPos);
  41.                 State := two_char;
  42.                 end;
  43.             else
  44.             State := two_char;
  45.             end;
  46.       two_char: if Mask[MaskPos] = Target[TargetPos] then begin
  47.             if Mask[MaskPos] = '\0' then
  48.                 SameName := True;
  49.             MaskPos := Succ(MaskPos);
  50.             TargetPos := Succ(TargetPos);
  51.             State := initial;
  52.             end else
  53.             SameName := False;
  54.       char_series :
  55.             case Mask[MaskPos] of
  56.               '?' : begin
  57.                 MaskPos := Succ(MaskPos);
  58.                 State := Star;
  59.                 end;
  60.              '\0' : SameName := False;
  61.             else begin
  62.                  while Target[TargetPos] = Mask[MaskPos] do
  63.                  TargetPos := Succ(TargetPos);
  64.                  MaskPos := Succ(MaskPos);
  65.                  State := initial;
  66.              end;
  67.             end;
  68.       any_char: begin
  69.             TargetPos := Succ(TargetPos);
  70.             State := initial;
  71.             end;
  72.       star    : begin
  73.             while (Target[TargetPos] <> Mask[MaskPos]) and
  74.                 (Target[TargetPos] <> '\0') do
  75.                 TargetPos := Succ(TargetPos);
  76.             state := initial;
  77.             end;
  78.     end;
  79. end;
  80.  
  81.