Important alert: (current site time 9/17/2014 3:10:53 AM EDT)
 

VB icon

^NEW^ -- wildcard string compare (globbing)

Email
Submitted on: 5/2/2001 12:49:06 AM
By: Jack Handy 
Level: Beginner
User Rating: By 10 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 25861
 
     matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ^NEW^ -- wildcard string compare (globbing)
// Description:matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.
// By: Jack Handy
//
// Returns:1 on match, 0 on no match.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1680&lngWId=3//for details.//**************************************

int wildcmp(char *wild, char *string) {
 char *cp, *mp;
 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;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/25/2001 3:11:57 PMVorlonTech

Man, I can't believe it's that simple! The function I started looks like bloat-ware in comparison to this one, and mine has its share of bugs :-) VERY NICE CODE, Mr. Handy!!
(If this comment was disrespectful, please report it.)

 
12/25/2001 3:13:41 PMVorlonTech

I can't believe it's this simple! I was in the process of writing my own function when I found this... And my function looks like bloat-ware in comparison (not to mention mine has quite a few bugs :-). VERY NICE CODE, Mr. Handy!
(If this comment was disrespectful, please report it.)

 
7/31/2002 9:52:01 AMTsunami

Nice work, Mr Handy! I'm using this piece of code quite regularly, but I'm lacking the option of case-insensitive globbing. Would welcome an "add-on" to this code :)
(If this comment was disrespectful, please report it.)

 
1/6/2003 6:39:58 PM

Very nice! I was looking for a wildcmp function like this. Five globes from me!
(If this comment was disrespectful, please report it.)

 
5/26/2003 1:31:59 PMMud Blud

very inspiring
(If this comment was disrespectful, please report it.)

 
11/3/2003 12:26:24 AMSanchit Karve

Nice Work! I never knew the code is so simple...Great piece of code..Removing The Case-Sensitive Feature would make the code a lot better..4 Globes by me.
(If this comment was disrespectful, please report it.)

 
3/7/2007 8:22:04 AMDevanand

I LOVE C.But Iam only a beginner.
so I find your prg.s fascinating.
(If this comment was disrespectful, please report it.)

 
11/14/2012 12:29:02 PMEvrisD

Great piece of code Mr. Handy! it certainly helps a lot. I am beginner in C and C++ so i wonder if it is possible to have a small explanasion on how the cp and mp work in your code.
tnx in advance
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.