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_info.h>
00024 #include <drizzled/typelib.h>
00025 
00026 namespace drizzled
00027 {
00028 
00029 static const char field_separator=',';
00030 
00031 int st_typelib::find_type_or_exit(const char *x, const char *option) const
00032 {
00033   int res= find_type(const_cast<char*>(x), 2);
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
00058       If & 2 don't expand if half field
00059       If & 4 allow #number# as type
00060       If & 8 use ',' as string terminator
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 st_typelib::find_type(const char *x, uint32_t full_name) const
00074 {
00075   assert(full_name & 2);
00076   return find_type(const_cast<char*>(x), full_name);
00077 }
00078 
00079 int st_typelib::find_type(char *x, uint32_t full_name) const
00080 {
00081   if (!count)
00082     return 0;
00083   int find= 0;
00084   int findpos= 0;
00085   const char *j;
00086   for (int pos= 0; (j= type_names[pos]); pos++)
00087   {
00088     const char *i;
00089     for (i= x;
00090       *i && (!(full_name & 8) || *i != field_separator) &&
00091         my_toupper(&my_charset_utf8_general_ci,*i) ==
00092         my_toupper(&my_charset_utf8_general_ci,*j) ; i++, j++) ;
00093     if (! *j)
00094     {
00095       while (*i == ' ')
00096   i++;          /* skip_end_space */
00097       if (! *i || ((full_name & 8) && *i == field_separator))
00098   return(pos+1);
00099     }
00100     if ((!*i && (!(full_name & 8) || *i != field_separator)) &&
00101         (!*j || !(full_name & 1)))
00102     {
00103       find++;
00104       findpos=pos;
00105     }
00106   }
00107   if (find == 0 && (full_name & 4) && x[0] == '#' && strchr(x, '\0')[-1] == '#' &&
00108       (findpos=atoi(x+1)-1) >= 0 && (uint32_t) findpos < count)
00109     find=1;
00110   else if (find == 0 || ! x[0])
00111   {
00112     return(0);
00113   }
00114   else if (find != 1 || (full_name & 1))
00115   {
00116     return(-1);
00117   }
00118   if (!(full_name & 2))
00119     strcpy(x, type_names[findpos]);
00120   return findpos + 1;
00121 } /* find_type */
00122 
00123 
00124   /* Get name of type nr 'nr' */
00125   /* Warning first type is 1, 0 = empty field */
00126 
00127 void st_typelib::make_type(char *to, uint32_t nr) const
00128 {
00129   if (!nr)
00130     to[0]= 0;
00131   else
00132     strcpy(to, get_type(nr - 1));
00133 } /* make_type */
00134 
00135 
00136   /* Get type */
00137   /* Warning first type is 0 */
00138 
00139 const char *st_typelib::get_type(uint32_t nr) const
00140 {
00141   if (nr < count && type_names)
00142     return type_names[nr];
00143   return "?";
00144 }
00145 
00146 
00147 /*
00148   Create an integer value to represent the supplied comma-seperated
00149   string where each string in the TYPELIB denotes a bit position.
00150 
00151   SYNOPSIS
00152     find_typeset()
00153     x   string to decompose
00154     lib   TYPELIB (struct of pointer to values + count)
00155     err   index (not char position) of string element which was not
00156                 found or 0 if there was no error
00157 
00158   RETURN
00159     a integer representation of the supplied string
00160 */
00161 
00162 uint64_t st_typelib::find_typeset(const char *x, int *err) const
00163 {
00164   if (!count)
00165     return 0;
00166   uint64_t result= 0;
00167   *err= 0;
00168   while (*x)
00169   {
00170     (*err)++;
00171     const char *i= x;
00172     while (*x && *x != field_separator) x++;
00173     int find= find_type(i, 2 | 8) - 1;
00174     if (find < 0)
00175       return 0;
00176     result|= (1ULL << find);
00177   }
00178   *err= 0;
00179   return result;
00180 } /* find_set */
00181 
00182 
00183 /*
00184   Create a copy of a specified TYPELIB structure.
00185 
00186   SYNOPSIS
00187     copy_typelib()
00188     root  pointer to a memory::Root object for allocations
00189     from  pointer to a source TYPELIB structure
00190 
00191   RETURN
00192     pointer to the new TYPELIB structure on successful copy, or
00193     NULL otherwise
00194 */
00195 
00196 TYPELIB *st_typelib::copy_typelib(memory::Root *root) const
00197 {
00198   TYPELIB *to;
00199   uint32_t i;
00200 
00201   if (!this)
00202     return NULL;
00203 
00204   if (!(to= (TYPELIB*) root->alloc_root(sizeof(TYPELIB))))
00205     return NULL;
00206 
00207   if (!(to->type_names= (const char **)
00208         root->alloc_root((sizeof(char *) + sizeof(int)) * (count + 1))))
00209     return NULL; // leaking
00210   to->type_lengths= (unsigned int *)(to->type_names + count + 1);
00211   to->count= count;
00212   if (name)
00213   {
00214     if (!(to->name= root->strdup_root(name)))
00215       return NULL; // leaking
00216   }
00217   else
00218     to->name= NULL;
00219 
00220   for (i= 0; i < count; i++)
00221   {
00222     if (!(to->type_names[i]= root->strmake_root(type_names[i], type_lengths[i])))
00223       return NULL; // leaking
00224     to->type_lengths[i]= type_lengths[i];
00225   }
00226   to->type_names[to->count]= NULL;
00227   to->type_lengths[to->count]= 0;
00228 
00229   return to;
00230 }
00231 
00232 } /* namespace drizzled */