Drizzled Public API Documentation

typelib.cc
1 /* Copyright (C) 2000 MySQL AB
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Functions to handle typelib */
17 
18 #include <config.h>
19 
20 #include <stdio.h>
21 
22 #include <drizzled/internal/m_string.h>
23 #include <drizzled/charset.h>
24 #include <drizzled/memory/root.h>
25 #include <drizzled/typelib.h>
26 
27 namespace drizzled {
28 
29 static const char field_separator=',';
30 
31 int TYPELIB::find_type_or_exit(const char *x, const char *option) const
32 {
33  int res= find_type(x, e_dont_complete);
34  if (res > 0)
35  return res;
36  if (!*x)
37  fprintf(stderr, "No option given to %s\n", option);
38  else
39  fprintf(stderr, "Unknown option to %s: %s\n", option, x);
40  const char **ptr= type_names;
41  fprintf(stderr, "Alternatives are: '%s'", *ptr);
42  while (*++ptr)
43  fprintf(stderr, ",'%s'", *ptr);
44  fprintf(stderr, "\n");
45  exit(1);
46 }
47 
48 
49 /*
50  Search after a string in a list of strings. Endspace in x is not compared.
51 
52  SYNOPSIS
53  find_type()
54  x String to find
55  lib TYPELIB (struct of pointer to values + count)
56  full_name bitmap of what to do
57  If & 1 accept only whole names - e_match_full
58  If & 2 don't expand if half field - e_dont_complete
59  If & 4 allow #number# as type - e_allow_int
60  If & 8 use ',' as string terminator - e_use_comma
61 
62  NOTES
63  If part, uniq field is found and full_name == 0 then x is expanded
64  to full field.
65 
66  RETURN
67  -1 Too many matching values
68  0 No matching value
69  >0 Offset+1 in typelib for matched string
70 */
71 
72 
73 int TYPELIB::find_type(const char *x, e_find_options full_name) const
74 {
75  assert(full_name & e_dont_complete);
76  if (!count)
77  return 0;
78  int find= 0;
79  int findpos= 0;
80  const char *j;
81  for (int pos= 0; (j= type_names[pos]); pos++)
82  {
83  const char *i= x;
84  for (; *i && *i != field_separator &&
85  my_charset_utf8_general_ci.toupper(*i) == my_charset_utf8_general_ci.toupper(*j); i++, j++)
86  {
87  }
88  if (not *j)
89  {
90  while (*i == ' ')
91  i++; /* skip_end_space */
92  if (not *i)
93  return pos + 1;
94  }
95  if (not *i && *i != field_separator && (not *j || not (full_name & e_match_full)))
96  {
97  find++;
98  findpos= pos;
99  }
100  }
101  if (find == 0 || not x[0])
102  return 0;
103  if (find != 1 || (full_name & e_match_full))
104  return -1;
105  return findpos + 1;
106 } /* find_type */
107 
108 /*
109  Create a copy of a specified TYPELIB structure.
110 
111  SYNOPSIS
112  copy_typelib()
113  root pointer to a memory::Root object for allocations
114  from pointer to a source TYPELIB structure
115 
116  RETURN
117  pointer to the new TYPELIB structure on successful copy, or
118  NULL otherwise
119 */
120 
121 TYPELIB *TYPELIB::copy_typelib(memory::Root& root) const
122 {
123  TYPELIB* to= new (root) TYPELIB;
124  to->type_names= (const char**)root.alloc((sizeof(char *) + sizeof(int)) * (count + 1));
125  to->type_lengths= (unsigned int*)(to->type_names + count + 1);
126  to->count= count;
127  to->name= name ? root.strdup(name) : NULL;
128  for (uint32_t i= 0; i < count; i++)
129  {
130  to->type_names[i]= root.strdup(type_names[i], type_lengths[i]);
131  to->type_lengths[i]= type_lengths[i];
132  }
133  to->type_names[to->count]= NULL;
134  to->type_lengths[to->count]= 0;
135  return to;
136 }
137 
138 } /* namespace drizzled */
Memory root declarations.