Drizzled Public API Documentation

mf_wcomp.cc

00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* Funktions for comparing with wild-cards */
00017 
00018 #include <config.h>
00019 
00020 #include <drizzled/internal/my_sys.h>
00021 
00022 namespace drizzled
00023 {
00024 namespace internal
00025 {
00026 
00027   /* Test if a string is "comparable" to a wild-card string */
00028   /* returns 0 if the strings are "comparable" */
00029 
00030 char wild_many='%';
00031 char wild_one='_';
00032 char wild_prefix= '\\';
00033 
00034 int wild_compare(const char *str, const char *wildstr, bool str_is_pattern)
00035 {
00036   char cmp;
00037 
00038   while (*wildstr)
00039   {
00040     while (*wildstr && *wildstr != wild_many && *wildstr != wild_one)
00041     {
00042       if (*wildstr == wild_prefix && wildstr[1])
00043       {
00044   wildstr++;
00045         if (str_is_pattern && *str++ != wild_prefix)
00046           return(1);
00047       }
00048       if (*wildstr++ != *str++)
00049         return(1);
00050     }
00051     if (! *wildstr )
00052       return(*str != 0);
00053     if (*wildstr++ == wild_one)
00054     {
00055       if (! *str || (str_is_pattern && *str == wild_many))
00056         return(1);                     /* One char; skip */
00057       if (*str++ == wild_prefix && str_is_pattern && *str)
00058         str++;
00059     }
00060     else
00061     {           /* Found '*' */
00062       while (str_is_pattern && *str == wild_many)
00063         str++;
00064       for (; *wildstr ==  wild_many || *wildstr == wild_one; wildstr++)
00065         if (*wildstr == wild_many)
00066         {
00067           while (str_is_pattern && *str == wild_many)
00068             str++;
00069         }
00070         else
00071         {
00072           if (str_is_pattern && *str == wild_prefix && str[1])
00073             str+=2;
00074           else if (! *str++)
00075             return (1);
00076         }
00077       if (!*wildstr)
00078         return(0);    /* '*' as last char: OK */
00079       if ((cmp= *wildstr) == wild_prefix && wildstr[1] && !str_is_pattern)
00080         cmp=wildstr[1];
00081       for (;;str++)
00082       {
00083         while (*str && *str != cmp)
00084           str++;
00085         if (!*str)
00086           return (1);
00087   if (wild_compare(str,wildstr,str_is_pattern) == 0)
00088           return (0);
00089       }
00090       /* We will never come here */
00091     }
00092   }
00093   return (*str != 0);
00094 } /* wild_compare */
00095 
00096 } /* namespace internal */
00097 } /* namespace drizzled */