Delphi comes with a unit, Masks.pas,
which supposedly provides a class, TMask, and a
function, MatchesMask, which uses the class, that
can compare a string against a mask. However, in all Win32 versions of
Delphi, TMask is fundamentally flawed. Whenever a mask ends
with a question mark, whether the mask matches a given string is entirely
circumstantial. (For details on the causes of the flaw, refer to Quality Central entry 5744.)
Instead of the Masks unit, use the PathMatchSpec
API function. It is provided by shlwapi.dll. That DLL is
not redistributable, but it comes with Internet Explorer 4, Windows 98, and
Windows 2000. Use the function declarations below.
Listing 1
Declarations for the PatchMatchSpec function
const
shlwapi = 'shlwapi.dll';
function PathMatchSpec(const pszFileParam, pszSpec: PChar): Bool; stdcall; external shlwapi name 'PathMatchSpecA';
function PathMatchSpecA(const pszFileParam, pszSpec: PAnsiChar): Bool; stdcall; external shlwapi;
function PathMatchSpecW(const pszFileParam, pszSpec: PWideChar): Bool; stdcall; external shlwapi;
.Net
The .Net personalities of Delphi 2005 and Delphi 2006 (and perhaps
Delphi 8, but I don’t know) have a completely different version of
Masks.pas. Instead of evaluating the mask
itself, TMask converts the mask into a regular expression
and passes it along to the System.Text.RegularExpressions.Regex
class. It does not exhibit the same buggy behavior as its Win32
counterpart.