~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/* Functions to handle typelib */
17
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
18
#include "config.h"
19
20
#include <stdio.h>
21
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
22
#include "drizzled/internal/m_string.h"
1241.9.61 by Monty Taylor
No more mystrings in drizzled/
23
#include "drizzled/charset_info.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
24
#include "drizzled/typelib.h"
1 by brian
clean slate
25
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
26
namespace drizzled
27
{
1 by brian
clean slate
28
29
static const char field_separator=',';
30
287.3.11 by Monty Taylor
Rolled back a couple of overly anxious consts.
31
int find_type_or_exit(char *x, TYPELIB *typelib, const char *option)
1 by brian
clean slate
32
{
33
  int res;
34
  const char **ptr;
35
287.3.11 by Monty Taylor
Rolled back a couple of overly anxious consts.
36
  if ((res= find_type(x, typelib, 2)) <= 0)
1 by brian
clean slate
37
  {
38
    ptr= typelib->type_names;
39
    if (!*x)
40
      fprintf(stderr, "No option given to %s\n", option);
41
    else
42
      fprintf(stderr, "Unknown option to %s: %s\n", option, x);
43
    fprintf(stderr, "Alternatives are: '%s'", *ptr);
44
    while (*++ptr)
45
      fprintf(stderr, ",'%s'", *ptr);
46
    fprintf(stderr, "\n");
47
    exit(1);
48
  }
49
  return res;
50
}
51
52
53
/*
54
  Search after a string in a list of strings. Endspace in x is not compared.
55
56
  SYNOPSIS
57
   find_type()
58
   x			String to find
59
   lib			TYPELIB (struct of pointer to values + count)
60
   full_name		bitmap of what to do
61
			If & 1 accept only whole names
62
			If & 2 don't expand if half field
63
			If & 4 allow #number# as type
64
			If & 8 use ',' as string terminator
65
66
  NOTES
67
    If part, uniq field is found and full_name == 0 then x is expanded
68
    to full field.
69
70
  RETURN
71
    -1	Too many matching values
72
    0	No matching value
73
    >0  Offset+1 in typelib for matched string
74
*/
75
76
482 by Brian Aker
Remove uint.
77
int find_type(char *x, const TYPELIB *typelib, uint32_t full_name)
1 by brian
clean slate
78
{
77.1.71 by Monty Taylor
Uninitialized use.
79
  int find,pos,findpos=0;
1 by brian
clean slate
80
  register char * i;
81
  register const char *j;
82
83
  if (!typelib->count)
84
  {
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
85
    return(0);
1 by brian
clean slate
86
  }
87
  find=0;
88
  for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
89
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
90
    for (i=x ;
1 by brian
clean slate
91
    	*i && (!(full_name & 8) || *i != field_separator) &&
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
92
        my_toupper(&my_charset_utf8_general_ci,*i) ==
383.1.11 by Brian Aker
Cleanup default character set.
93
    		my_toupper(&my_charset_utf8_general_ci,*j) ; i++, j++) ;
1 by brian
clean slate
94
    if (! *j)
95
    {
96
      while (*i == ' ')
97
	i++;					/* skip_end_space */
98
      if (! *i || ((full_name & 8) && *i == field_separator))
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
99
	return(pos+1);
1 by brian
clean slate
100
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
101
    if ((!*i && (!(full_name & 8) || *i != field_separator)) &&
1 by brian
clean slate
102
        (!*j || !(full_name & 1)))
103
    {
104
      find++;
105
      findpos=pos;
106
    }
107
  }
376 by Brian Aker
strend remove
108
  if (find == 0 && (full_name & 4) && x[0] == '#' && strchr(x, '\0')[-1] == '#' &&
895 by Brian Aker
Completion (?) of uint conversion.
109
      (findpos=atoi(x+1)-1) >= 0 && (uint32_t) findpos < typelib->count)
1 by brian
clean slate
110
    find=1;
111
  else if (find == 0 || ! x[0])
112
  {
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
113
    return(0);
1 by brian
clean slate
114
  }
115
  else if (find != 1 || (full_name & 1))
116
  {
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
117
    return(-1);
1 by brian
clean slate
118
  }
119
  if (!(full_name & 2))
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
120
    (void) strcpy(x,typelib->type_names[findpos]);
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
121
  return(findpos+1);
1 by brian
clean slate
122
} /* find_type */
123
124
125
	/* Get name of type nr 'nr' */
126
	/* Warning first type is 1, 0 = empty field */
127
482 by Brian Aker
Remove uint.
128
void make_type(register char * to, register uint32_t nr,
1 by brian
clean slate
129
	       register TYPELIB *typelib)
130
{
131
  if (!nr)
132
    to[0]=0;
133
  else
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
134
    (void) strcpy(to,get_type(typelib,nr-1));
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
135
  return;
1 by brian
clean slate
136
} /* make_type */
137
138
139
	/* Get type */
140
	/* Warning first type is 0 */
141
482 by Brian Aker
Remove uint.
142
const char *get_type(TYPELIB *typelib, uint32_t nr)
1 by brian
clean slate
143
{
895 by Brian Aker
Completion (?) of uint conversion.
144
  if (nr < (uint32_t) typelib->count && typelib->type_names)
1 by brian
clean slate
145
    return(typelib->type_names[nr]);
146
  return "?";
147
}
148
149
150
/*
151
  Create an integer value to represent the supplied comma-seperated
152
  string where each string in the TYPELIB denotes a bit position.
153
154
  SYNOPSIS
155
    find_typeset()
156
    x		string to decompose
157
    lib		TYPELIB (struct of pointer to values + count)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
158
    err		index (not char position) of string element which was not
1 by brian
clean slate
159
                found or 0 if there was no error
160
161
  RETURN
162
    a integer representation of the supplied string
163
*/
164
155 by Brian Aker
Removing a few "additional" ways of saying uint64_t
165
uint64_t find_typeset(char *x, TYPELIB *lib, int *err)
1 by brian
clean slate
166
{
155 by Brian Aker
Removing a few "additional" ways of saying uint64_t
167
  uint64_t result;
1 by brian
clean slate
168
  int find;
169
  char *i;
170
171
  if (!lib->count)
172
  {
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
173
    return(0);
1 by brian
clean slate
174
  }
175
  result= 0;
176
  *err= 0;
177
  while (*x)
178
  {
179
    (*err)++;
180
    i= x;
181
    while (*x && *x != field_separator) x++;
182
    if ((find= find_type(i, lib, 2 | 8) - 1) < 0)
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
183
      return(0);
80.1.1 by Brian Aker
LL() cleanup
184
    result|= (1ULL << find);
1 by brian
clean slate
185
  }
186
  *err= 0;
51.3.14 by Jay Pipes
Phase 2 removal of DBUG in mysys
187
  return(result);
1 by brian
clean slate
188
} /* find_set */
189
190
191
/*
192
  Create a copy of a specified TYPELIB structure.
193
194
  SYNOPSIS
195
    copy_typelib()
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
196
    root	pointer to a memory::Root object for allocations
1 by brian
clean slate
197
    from	pointer to a source TYPELIB structure
198
199
  RETURN
200
    pointer to the new TYPELIB structure on successful copy, or
201
    NULL otherwise
202
*/
203
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
204
TYPELIB *copy_typelib(memory::Root *root, TYPELIB *from)
1 by brian
clean slate
205
{
206
  TYPELIB *to;
482 by Brian Aker
Remove uint.
207
  uint32_t i;
1 by brian
clean slate
208
209
  if (!from)
210
    return NULL;
211
1485 by Brian Aker
Updates to confine memroot
212
  if (!(to= (TYPELIB*) root->alloc_root(sizeof(TYPELIB))))
1 by brian
clean slate
213
    return NULL;
214
215
  if (!(to->type_names= (const char **)
1485 by Brian Aker
Updates to confine memroot
216
        root->alloc_root((sizeof(char *) + sizeof(int)) * (from->count + 1))))
1 by brian
clean slate
217
    return NULL;
218
  to->type_lengths= (unsigned int *)(to->type_names + from->count + 1);
219
  to->count= from->count;
220
  if (from->name)
221
  {
1487 by Brian Aker
More updates for memory::Root
222
    if (!(to->name= root->strdup_root(from->name)))
1 by brian
clean slate
223
      return NULL;
224
  }
225
  else
226
    to->name= NULL;
227
228
  for (i= 0; i < from->count; i++)
229
  {
1487 by Brian Aker
More updates for memory::Root
230
    if (!(to->type_names[i]= root->strmake_root(from->type_names[i], from->type_lengths[i])))
1 by brian
clean slate
231
      return NULL;
232
    to->type_lengths[i]= from->type_lengths[i];
233
  }
234
  to->type_names[to->count]= NULL;
235
  to->type_lengths[to->count]= 0;
236
237
  return to;
238
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
239
240
} /* namespace drizzled */