Drizzled Public API Documentation

typelib.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 /* Functions to handle typelib */
00017 
00018 #include <config.h>
00019 
00020 #include <stdio.h>
00021 
00022 #include <drizzled/internal/m_string.h>
00023 #include <drizzled/charset.h>
00024 #include <drizzled/memory/root.h>
00025 #include <drizzled/typelib.h>
00026 
00027 namespace drizzled {
00028 
00029 static const char field_separator=',';
00030 
00031 int TYPELIB::find_type_or_exit(const char *x, const char *option) const
00032 {
00033   int res= find_type(x, e_dont_complete);
00034   if (res > 0)
00035     return res;
00036   if (!*x)
00037     fprintf(stderr, "No option given to %s\n", option);
00038   else
00039     fprintf(stderr, "Unknown option to %s: %s\n", option, x);
00040   const char **ptr= type_names;
00041   fprintf(stderr, "Alternatives are: '%s'", *ptr);
00042   while (*++ptr)
00043     fprintf(stderr, ",'%s'", *ptr);
00044   fprintf(stderr, "\n");
00045   exit(1);
00046 }
00047 
00048 
00049 /*
00050   Search after a string in a list of strings. Endspace in x is not compared.
00051 
00052   SYNOPSIS
00053    find_type()
00054    x      String to find
00055    lib      TYPELIB (struct of pointer to values + count)
00056    full_name    bitmap of what to do
00057       If & 1 accept only whole names - e_match_full
00058       If & 2 don't expand if half field - e_dont_complete
00059       If & 4 allow #number# as type - e_allow_int
00060       If & 8 use ',' as string terminator - e_use_comma
00061 
00062   NOTES
00063     If part, uniq field is found and full_name == 0 then x is expanded
00064     to full field.
00065 
00066   RETURN
00067     -1  Too many matching values
00068     0 No matching value
00069     >0  Offset+1 in typelib for matched string
00070 */
00071 
00072 
00073 int TYPELIB::find_type(const char *x, e_find_options full_name) const
00074 {
00075   assert(full_name & e_dont_complete);
00076   if (!count)
00077     return 0;
00078   int find= 0;
00079   int findpos= 0;
00080   const char *j;
00081   for (int pos= 0; (j= type_names[pos]); pos++)
00082   {
00083     const char *i= x;
00084     for (; *i && *i != field_separator &&
00085       my_charset_utf8_general_ci.toupper(*i) == my_charset_utf8_general_ci.toupper(*j); i++, j++)
00086     {
00087     }
00088     if (not *j)
00089     {
00090       while (*i == ' ')
00091         i++;          /* skip_end_space */
00092       if (not *i)
00093         return pos + 1;
00094     }
00095     if (not *i && *i != field_separator && (not *j || not (full_name & e_match_full)))
00096     {
00097       find++;
00098       findpos= pos;
00099     }
00100   }
00101   if (find == 0 || not x[0])
00102     return 0;
00103   if (find != 1 || (full_name & e_match_full))
00104     return -1;
00105   return findpos + 1;
00106 } /* find_type */
00107 
00108 /*
00109   Create a copy of a specified TYPELIB structure.
00110 
00111   SYNOPSIS
00112     copy_typelib()
00113     root  pointer to a memory::Root object for allocations
00114     from  pointer to a source TYPELIB structure
00115 
00116   RETURN
00117     pointer to the new TYPELIB structure on successful copy, or
00118     NULL otherwise
00119 */
00120 
00121 TYPELIB *TYPELIB::copy_typelib(memory::Root& root) const
00122 {
00123   TYPELIB* to= new (root) TYPELIB;
00124   to->type_names= (const char**)root.alloc((sizeof(char *) + sizeof(int)) * (count + 1));
00125   to->type_lengths= (unsigned int*)(to->type_names + count + 1);
00126   to->count= count;
00127   to->name= name ? root.strdup(name) : NULL;
00128   for (uint32_t i= 0; i < count; i++)
00129   {
00130     to->type_names[i]= root.strdup(type_names[i], type_lengths[i]);
00131     to->type_lengths[i]= type_lengths[i];
00132   }
00133   to->type_names[to->count]= NULL;
00134   to->type_lengths[to->count]= 0;
00135   return to;
00136 }
00137 
00138 } /* namespace drizzled */