views:

740

answers:

5

Hello, I am looking for a wildcard string match API (not regex match). I cannot use anything other than Win32 APIs.

+2  A: 

The FindFirstFile and FindNextFile APIs do wildcard matches, but only against filenames.

You can't use anything but Win32? What about STL or CRT? Are you using Boost?

Without the Win32 API restriction, I would recommend using the code from some open-source project. Another option would be to translate the glob into a regex, which I believe can be done with a regex replace operation.

edit: First google match is the PHP code:

http://cvs.php.net/viewvc.cgi/php-src/win32/

Tim Sylvester
A: 

WHat exactly is your requirement? Are you just looking to use the '' symbol to match 0 or more characters or are you planning on using the '?' symbol as well. If it is just '', do you need to look for a, a, a*b, a*b*c, etc type patterns? If your requirement is limited, you could easily get away with the C++ runtime library's strstr function.

msvcyc
+3  A: 

The easiest thing would be to just convert your glob pattern to a regex, by the following rules:

  • * becomes .*
  • ? becomes .
  • Any of \|.^$+()[]{} are escaped by preceding them with \
Pavel Minaev
+2  A: 

There is PathMatchSpec - but handling is specialized for files, so results might not be what you expect if you need general wildcard matching.

Otherwise, you should probably go with an RegEx, as Pavel detailed.

[edit] I incorrectly assumed PathMatchSpec shares the properties of FindFirstFile/FindNextFile. I've ran a few tests - it doesn't. So it looks like the best candidate.

peterchen
A very interesting find. I wonder how is that thing specialized for paths, though (if it actually is).
Pavel Minaev
Pavel, I checked - the special handling is only in FindFirst/NextFile. Yay! :D
peterchen
+2  A: 

If you're after a simple wildcard compare (globbing), some people have written their own, including this one (which we use in our code)

Alan