Drizzled Public API Documentation

strfunc.cc
00001 /* Copyright (C) 2003 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 /* Some useful string utility functions used by the MySQL server */
00017 #include <config.h>
00018 
00019 #include <drizzled/typelib.h>
00020 #include <drizzled/charset.h>
00021 
00022 namespace drizzled {
00023 
00024 /*
00025   Function to find a string in a TYPELIB
00026   (Same format as mysys/typelib.c)
00027 
00028   SYNOPSIS
00029    find_type()
00030    lib      TYPELIB (struct of pointer to values + count)
00031    find     String to find
00032    length   Length of string to find
00033    part_match   Allow part matching of value
00034 
00035  RETURN
00036   0 error
00037   > 0 position in TYPELIB->type_names +1
00038 */
00039 
00040 uint32_t TYPELIB::find_type(const char *find, uint32_t length, bool part_match) const
00041 {
00042   uint32_t found_count=0, found_pos=0;
00043   const char* end= find + length;
00044   const char* i;
00045   const char* j;
00046   for (uint32_t pos= 0 ; (j= type_names[pos++]); )
00047   {
00048     for (i= find ; i != end && system_charset_info->toupper(*i) == system_charset_info->toupper(*j); i++, j++) 
00049     {
00050     }
00051     if (i == end)
00052     {
00053       if (not *j)
00054         return pos;
00055       found_count++;
00056       found_pos= pos;
00057     }
00058   }
00059   return found_count == 1 && part_match ? found_pos : 0;
00060 }
00061 
00062 
00063 /*
00064   Find a string in a list of strings according to collation
00065 
00066   SYNOPSIS
00067    find_type2()
00068    lib      TYPELIB (struct of pointer to values + count)
00069    x      String to find
00070    length               String length
00071    cs     Character set + collation to use for comparison
00072 
00073   NOTES
00074 
00075   RETURN
00076     0 No matching value
00077     >0  Offset+1 in typelib for matched string
00078 */
00079 
00080 uint32_t TYPELIB::find_type2(const char *x, uint32_t length, const charset_info_st *cs) const
00081 {
00082   if (!count)
00083     return 0;
00084   const char *j;
00085   for (int pos=0 ; (j= type_names[pos]) ; pos++)
00086   {
00087     if (!my_strnncoll(cs, (const unsigned char*) x, length,
00088                           (const unsigned char*) j, type_lengths[pos]))
00089       return pos + 1;
00090   }
00091   return 0;
00092 } /* find_type */
00093 
00094 } /* namespace drizzled */