-const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $";
+const char miscutil_rcs[] = "$Id: miscutil.c,v 1.4 2001/05/31 17:32:31 oes Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
*
* Revisions :
* $Log: miscutil.c,v $
+ * Revision 1.4 2001/05/31 17:32:31 oes
+ *
+ * - Enhanced domain part globbing with infix and prefix asterisk
+ * matching and optional unanchored operation
+ *
* Revision 1.3 2001/05/29 23:10:09 oes
*
*
/*********************************************************************
*
- * Function : trivimatch
- *
- * Description : Trivial string matching, with only one metacharacter,
- * namely '*', which stands for zero or more arbitrary
- * characters.
+ * Function : simplematch
*
- * Note: The * is greedy, i.e. it will try to match as
- * much text es possible.
+ * Description : String matching, with a (greedy) '*' wildcard that
+ * stands for zero or more arbitrary characters and
+ * character classes in [], which take both enumerations
+ * and ranges.
*
* Parameters :
* 1 : pattern = pattern for matching
* Returns : 0 if match, else nonzero
*
*********************************************************************/
-int trivimatch(char *pattern, char *text)
+int simplematch(char *pattern, char *text)
{
- char *fallback;
- char *pat = pattern;
- char *txt = text;
- int wildcard = 0;
+ char *fallback;
+ char *pat = pattern;
+ char *txt = text;
+ int wildcard = 0;
+
+ char lastchar = 'a';
+ unsigned i;
+ unsigned char charmap[32];
+
while (*txt)
{
+
/* EOF pattern but !EOF text? */
if (*pat == '\0')
{
/* '*' in the pattern? */
if (*pat == '*')
{
-
+
/* The pattern ends afterwards? Speed up the return. */
if (*++pat == '\0')
{
return 0;
}
-
+
/* Else, set wildcard mode and remember position after '*' */
wildcard = 1;
fallback = pat;
}
- /* Compare: */
- if (*pat != *txt)
+ /* Character range specification? */
+ if (*pat == '[')
{
- /* In wildcard mode, just try again */
+ memset(charmap, '\0', sizeof(charmap));
+
+ while (*++pat != ']')
+ {
+ if (!*pat)
+ {
+ return 1;
+ }
+ else if (*pat == '-')
+ {
+ if ((*++pat == ']') || *pat == '\0')
+ {
+ return(1);
+ }
+ for(i = lastchar; i <= *pat; i++)
+ {
+ charmap[i / 8] |= (1 << (i % 8));
+ }
+ }
+ else
+ {
+ charmap[*pat / 8] |= (1 << (*pat % 8));
+ lastchar = *pat;
+ }
+ }
+ } /* -END- if Character range specification */
+
+
+ /* Compare: Char match, or char range match*/
+ if ((*pat == *txt)
+ || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
+ {
+ /* Sucess, go ahead */
+ pat++;
+ }
+ else
+ {
+ /* In wildcard mode, just try again after failiure */
if(wildcard)
{
- /* Without wildcard mode, this is fatal! */
pat = fallback;
}
- /* Bad luck otherwise */
+ /* Else, bad luck */
else
{
return 1;
}
}
- /* We had a match, advance */
- else
- {
- pat++;
- }
txt++;
- }
+ }
- if(*pat == '*') pat++;
+ /* Cut off extra '*'s */
+ if(*pat == '*') pat++;
- /* Hey, we've made it all the way through! */
- return(*pat);
+ /* If this is the pattern's end, fine! */
+ return(*pat);
}
#ifndef _MISCUTIL_H
#define _MISCUTIL_H
-#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.3 2001/05/29 23:10:09 oes Exp $"
+#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.4 2001/05/31 17:32:31 oes Exp $"
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/miscutil.h,v $
*
* Revisions :
* $Log: miscutil.h,v $
+ * Revision 1.4 2001/05/31 17:32:31 oes
+ *
+ * - Enhanced domain part globbing with infix and prefix asterisk
+ * matching and optional unanchored operation
+ *
* Revision 1.3 2001/05/29 23:10:09 oes
*
*
extern char *strsav(char *old, const char *text_to_append);
extern char *chomp(char *string);
-extern int trivimatch(char *pattern, char *text);
+extern int simplematch(char *pattern, char *text);
#ifdef __MINGW32__
extern char *strdup(const char *s);