how to write a wildcardsearch-procedure ? 
Author Message
 how to write a wildcardsearch-procedure ?

I had some problems in writing a procedure for filename with wildcard
comparison procedure like IsFilename('test.exe','test.*'). [like in
MS-Windows]
The procedure Im looking for should take in account the following search
criterion:

test*.exe = all files that begin with "test", and with ext ".exe"
test*.e* = all files that begin with "test", and with first character ".e"
in extension could be "testfile.exe","testfile.ext","test.exe" ...
My*testfile* = all files that begins with "My" and that contains "testfile"
My*??stfile* = all files that begins with "My" and that contains any 2
characters before "stfile"
*. = all files without extension
* all files
*.* = all files



Sat, 28 Jun 2003 06:17:31 GMT  
 how to write a wildcardsearch-procedure ?

Quote:
Benjamin Killer wrote...
>I had some problems in writing a procedure for filename with wildcard
>comparison procedure like IsFilename('test.exe','test.*'). [like in
>MS-Windows]

Procedure? in what?

Well, there is already program like Cygwin which contains the Unix-like
"find" utitity that should do what you want.



Sat, 28 Jun 2003 11:36:12 GMT  
 how to write a wildcardsearch-procedure ?

Quote:
> I had some problems in writing a procedure for filename with wildcard
> comparison procedure like IsFilename('test.exe','test.*'). [like in
> MS-Windows]
> The procedure Im looking for should take in account the following search
> criterion:

> test*.exe = all files that begin with "test", and with ext ".exe"
> test*.e* = all files that begin with "test", and with first character ".e"
> in extension could be "testfile.exe","testfile.ext","test.exe" ...
> My*testfile* = all files that begins with "My" and that contains "testfile"
> My*??stfile* = all files that begins with "My" and that contains any 2
> characters before "stfile"
> *. = all files without extension
> * all files
> *.* = all files

   Try this:

function Test (A,B : char): boolean;{ Test match of 2 string characters }
var Res : boolean;
begin
  if B = '?' then Res := true
  else if A = B then Res := true
       else          Res := false;
  Test := Res
end;

function Min (M,N : integer): integer;
begin
  if M < N then Min := M
  else          Min := N
end;

function Max (M,N : integer): integer;
begin
  if M > N then Max := M
  else          Max := N
end;

function Match (SB,SA : string): boolean;
var I,J,K : integer;
    T_Res : boolean;
begin
  I := Pos('*',SA);
  if I > 0 then SA := Copy(Copy(SA,1,I-1)+'????????',1,Length(SA));
  T_Res := true;
  K := Min (Length(SA),Length(SB));
  J := 1;
  while J <= K do
    begin
      if Test (SB[J],SA[J]) then Inc(J)
      else
        begin
          T_Res := false;
          J := Max(Length(SB),Length(SA))+1 { force exit }
        end;
    end;
  if J <= Length(SB) then T_Res := false;
  if (J <= Length(SA)) and (SA[J] <> '?') then T_Res := false;
  Match := T_Res
end;



Sat, 28 Jun 2003 21:49:17 GMT  
 how to write a wildcardsearch-procedure ?
The hard part is dealing with internal matches (*testfile*).  If you restrict
yourself to having "*" at the end, plus embedded "?", the search criterion is
fairly simple.

First, note that you have separate matches for the name and extension.  Assume
you have a function "namepart" that returns the name, and "extpart" that
returns the extension.

 BEGIN  { isfilename }
   IF ismatch (namepart (filename), namepart (pattern))
     THEN isfilename := ismatch (extpart (filename), extpart (pattern))
     ELSE isfilename := false
  END;

Now you need a procedure that compares a name with a pattern (which can have a
trailing "*" or embedded "?".  Since string packages are not standard, I'm
going to assume that there's a function length() which returns the length of
the string, and that one can otherwise treat the string as an array.  I will
also assume that the first element of the string is 1.

  FUNCTION ismatch (name, pattern : stringtype) : boolean;

   FUNCTION ism (index : indextype) : boolean;

   BEGIN   { ism }
    IF (index > length(name)) AND (index > length(pattern))
     THEN ism := true
     ELSE IF pattern[index] = '*'
     THEN ism := true
     ELSE IF pattern[index] = '?'
       THEN ism := ism (succ(index))
       ELSE IF (index > length(name)) OR (index > length(pattern))
       THEN ism := false
       ELSE IF name[index] <> pattern[index]
        THEN ism := false
        ELSE ism := ism (succ(index))
  END;

   BEGIN   { ismatch }
    ismatch := ism (1)
   END;

The tests of the inner, recursive "ism" function do the following:  If we've
matched the entire string, we're done; if we are matching against a trailing
"*", we're done; if we are matching against a "?", this position matches, test
the next position; if we've run out of name or out of pattern, we don't match
(this may be a logic error -- do you intend to allow "a" to match "a*"?  If so,
you need to change this condition); if the current position matches, try next
position; otherwise, we don't match.

Bob Schor
Pascal Enthusiast

Quote:

> I had some problems in writing a procedure for filename with wildcard
> comparison procedure like IsFilename('test.exe','test.*'). [like in
> MS-Windows]
> The procedure Im looking for should take in account the following search
> criterion:

> test*.exe = all files that begin with "test", and with ext ".exe"
> test*.e* = all files that begin with "test", and with first character ".e"
> in extension could be "testfile.exe","testfile.ext","test.exe" ...
> My*testfile* = all files that begins with "My" and that contains "testfile"
> My*??stfile* = all files that begins with "My" and that contains any 2
> characters before "stfile"
> *. = all files without extension
> * all files
> *.* = all files



Sun, 29 Jun 2003 01:08:28 GMT  
 how to write a wildcardsearch-procedure ?

Quote:

> I had some problems in writing a procedure for filename with wildcard
> comparison procedure like IsFilename('test.exe','test.*'). [like in
> MS-Windows]
> The procedure Im looking for should take in account the following search
> criterion:

> test*.exe = all files that begin with "test", and with ext ".exe"
> test*.e* = all files that begin with "test", and with first character ".e"
> in extension could be "testfile.exe","testfile.ext","test.exe" ...
> My*testfile* = all files that begins with "My" and that contains "testfile"
> My*??stfile* = all files that begins with "My" and that contains any 2
> characters before "stfile"
> *. = all files without extension
> * all files
> *.* = all files

What you are looking for is a Pascal equivalent to the "glob" module
in Python.

Failing that, a good regular expression unit would be the next best
thing.

Do either of these exist for Pascal?

Apologies if these are in the FAQ's.



Tue, 01 Jul 2003 09:33:25 GMT  
 how to write a wildcardsearch-procedure ?

Quote:


> > I had some problems in writing a procedure for filename with wildcard
> > comparison procedure like IsFilename('test.exe','test.*'). [like in
> > MS-Windows]

> What you are looking for is a Pascal equivalent to the "glob" module
> in Python.

> Failing that, a good regular expression unit would be the next best
> thing.

> Do either of these exist for Pascal?

regexp:
There is a regexp-Unit for FreePascal. Download Freepascal and look for
"regexpr.pp". It should be in fpc/source/packages/regexpr

glob: <*g*>
  SwapVectors;
  Exec('perl.exe', '-e ''print glob("*")''');
  SwapVectors;
You only need to capture the output...

Wolf



Tue, 01 Jul 2003 22:51:51 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. read/write access Procedure.

2. writing procedures in MASM that used in TP7

3. write procedure for turbo 6.0

4. Howto write a procedure like Writeln

5. Calling a procedure within a procedure

6. HELP - calling a Procedure from with a Procedure??

7. re : main-->procedure coomunication + Menu Procedure

8. Using a Procedure within a Procedure?

9. Procedure as parameter in procedure

10. Fast PutCharacter Procedure &/or remove cursor procedure

11. fast putchar procedure and remove cursor procedure needed for lord II type game

12. passing procedures as arguments for another procedure in Turbo pascal

 

 
Powered by phpBB® Forum Software