Click here to Skip to main content
11,437,713 members (36,007 online)
Click here to Skip to main content

Wildcard string compare (globbing)

, 15 Feb 2005
Rate this:
Please Sign up or sign in to vote.
Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.

Usage:

This is a fast, lightweight, and simple pattern matching function.

if (wildcmp("bl?h.*", "blah.jpg")) {
  //we have a match!
} else {
  //no match =(
}

Function:

int wildcmp(const char *wild, const char *string) {
  // Written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
  const char *cp = NULL, *mp = NULL;

  while ((*string) && (*wild != '*')) {
    if ((*wild != *string) && (*wild != '?')) {
      return 0;
    }
    wild++;
    string++;
  }

  while (*string) {
    if (*wild == '*') {
      if (!*++wild) {
        return 1;
      }
      mp = wild;
      cp = string+1;
    } else if ((*wild == *string) || (*wild == '?')) {
      wild++;
      string++;
    } else {
      wild = mp;
      string = cp++;
    }
  }

  while (*wild == '*') {
    wild++;
  }
  return !*wild;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jack Handy
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralRe: Cool Code Pin
hector santos1-Mar-03 2:53
memberhector santos1-Mar-03 2:53 
GeneralRe: Cool Code Pin
Kent C. Dorner2-Mar-03 6:32
sussKent C. Dorner2-Mar-03 6:32 
GeneralRe: Cool Code Pin
gebrudergrimm28-Dec-03 11:18
membergebrudergrimm28-Dec-03 11:18 
GeneralRe: Cool Code Pin
BrcKcc15-Jun-04 9:18
memberBrcKcc15-Jun-04 9:18 
GeneralYes man - you a really cool developer Pin
Stanislav Panasik23-Oct-02 20:06
memberStanislav Panasik23-Oct-02 20:06 
GeneralRe: Yes man - you a really cool developer Pin
Anonymous28-Mar-04 14:43
sussAnonymous28-Mar-04 14:43 
GeneralPseudo Code... Pin
blahblah18-Apr-02 11:45
memberblahblah18-Apr-02 11:45 
GeneralRe: Pseudo Code... Pin
Targys8-Jan-03 3:43
memberTargys8-Jan-03 3:43 
Ok, I'll try, even though I think it would be a good idea for you to learn C Smile | :)

The first loop basically goes through both strings step by step until there is a * in the wild string.
When ever the characters of the both strings don't match and the character in the wild string is no ? the function returns 0 (FALSE) = no match.
(I'm not a hundred percent sure, 'cause I don't have time to test it, but I guess this loop is for speed reasons only)

The second loop does the hard thing:
		if (*wild == '*') {
			if (!*++wild) {
				return 1;
			}
			mp = wild;
			cp = string+1;
This if stores the positions of the string pointers, when *wild is a star
(*wild is the character of wild at the current position of the pointer *wild - easy explanation, not 100% correct)
If this * is the last character in the wild string, it returns 1 (TRUE) = match.
		} else {
			wild = mp;
			string = cp++;
This part if the ifs basically solves two things in one.
Firstly it's responsible to increase the pointer position of the string string pointer.
Secondly it returns the two pointers after a wrong go through to the end.
		} else if ((*wild == *string) || (*wild == '?')) {
			wild++;
			string++;
This part does the same as the first loop, just after the first *.
	while (*wild == '*') {
		wild++;
	}
Well, this loop just ingores several * at the end of the wild string.
	return !*wild;
And now, that's a nice one Smile | :)
I like it.
After going through all the * in the last loop, the wild string can now contain either
- nothing anymore, that means *wild is NULL, or
- anything but nothing.
Is *wild NULL that means all the comparisons were successful and the function can return 1.
Or easier: it returns !*wild = not NULL = 1 Wink | ;-)
Is it not NULL, but just any character, !*wild will be 0.

So this
	return !*wild;
basically replaces
	if (*wild = '') {
		return 1;
	} else {
		return 0;
	}
or something like this.

An example to explain how it really works:
wild is 'bl?h.*g'
string is 'blah.jpgeg'

After the first loop where 'b' is 'b' and 'l' is 'l' and '?' is 'a' and 'h' is 'h' and '.' is '.'
the position of the two pointers *wild and *string look like this:

*wild         |
        'bl?h.*g'
        'blah.jpgeg'
*string       |


Now the second loop starts:
the pointer *string is increased until it points to a character that is the same as the *wild+1.
That means it looks for a 'g' in the string string beginning from the current position.
This increment is done by the last else, as explained above as firstly.
So it will look like this

*wild          |
        'bl?h.*g'
        'blah.jpgeg'
*string         |


Now the second part if the ifs increases both pointers, because 'g' == 'g'.

*wild is now NULL because the g was the last character in the wild string.
*string is 'e'

Because Null != 'e' it sets back the pointers to the values they had before the comparison rush.
This is done again by the last else part, as explained as secondly above.
But the diferrence is now, that the *string pointer is one character further than then.
It looks now like that:

*wild         |
        'bl?h.*g'
        'blah.jpgeg'
*string        |


This change compared to the first time is done by the cp++ of
		} else {
			wild = mp;
			string = cp++;
where the pointer cp is incremented.

The same game starts all over again and again it doesn't succeed.
So next time it will look like this:

*wild          |
        'bl?h.*g'
        'blah.jpgeg'
*string         |


and:

*wild          |
        'bl?h.*g'
        'blah.jpgeg'
*string          |


and finally:

*wild          |
        'bl?h.*g'
        'blah.jpgeg'
*string           |


And this will end the loop, as *string will be NULL after the next run.

Well, it's not an easy explanation, as the problem of wildcard search is not really as easy as C++ Guru wants it to have.
(Maybe for a guru, it's easy Smile | :) )

Don't hesitate to ask, if the answer is not understandable.
And of course please correct me, if something is wrong! Smile | :)

Targys
Generaltoo complicated Pin
The C++ Guru21-Mar-02 22:31
memberThe C++ Guru21-Mar-02 22:31 
GeneralRe: too complicated Pin
Big Spender22-Mar-02 1:23
memberBig Spender22-Mar-02 1:23 
GeneralRe: too complicated Pin
The C++ Guru22-Mar-02 1:34
memberThe C++ Guru22-Mar-02 1:34 
GeneralRe: too complicated Pin
Jack Handy22-Mar-02 16:23
memberJack Handy22-Mar-02 16:23 
GeneralRe: too complicated Pin
The C++ Guru22-Mar-02 22:40
memberThe C++ Guru22-Mar-02 22:40 
GeneralRe: too complicated Pin
Jack Handy22-Mar-02 23:26
memberJack Handy22-Mar-02 23:26 
GeneralRe: too complicated Pin
Hockey19-Apr-03 10:25
memberHockey19-Apr-03 10:25 
GeneralRe: too complicated Pin
douglashogan25-Jan-04 13:34
memberdouglashogan25-Jan-04 13:34 
GeneralRe: too complicated Pin
Jack Handy25-Jan-04 13:40
memberJack Handy25-Jan-04 13:40 
GeneralRe: too complicated Pin
douglashogan25-Jan-04 13:50
memberdouglashogan25-Jan-04 13:50 
GeneralRe: too complicated Pin
Jim Bob22-Mar-02 16:29
memberJim Bob22-Mar-02 16:29 
GeneralRe: too complicated Pin
Simon Capewell22-May-02 3:48
memberSimon Capewell22-May-02 3:48 
GeneralRe: too complicated Pin
Targys8-Jan-03 2:57
memberTargys8-Jan-03 2:57 
GeneralRe: too complicated Pin
The C++ Guru24-Feb-03 11:43
sussThe C++ Guru24-Feb-03 11:43 
GeneralRe: too complicated Pin
Jack Handy24-Feb-03 17:52
memberJack Handy24-Feb-03 17:52 
GeneralRe: too complicated Pin
hector santos1-Mar-03 2:49
memberhector santos1-Mar-03 2:49 
GeneralRe: too complicated Pin
Kent C. Dorner2-Mar-03 6:26
sussKent C. Dorner2-Mar-03 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150506.1 | Last Updated 15 Feb 2005
Article Copyright 2001 by Jack Handy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid