18 #include "mysys_priv.h"
23 #define is_field_separator(X) ((X) == ',' || (X) == '=')
25 int find_type_or_exit(
const char *x,
TYPELIB *typelib,
const char *
option)
30 if ((res= find_type((
char *) x, typelib, FIND_TYPE_BASIC)) <= 0)
32 ptr= typelib->type_names;
34 fprintf(stderr,
"No option given to %s\n", option);
36 fprintf(stderr,
"Unknown option to %s: %s\n", option, x);
37 fprintf(stderr,
"Alternatives are: '%s'", *ptr);
39 fprintf(stderr,
",'%s'", *ptr);
40 fprintf(stderr,
"\n");
68 int find_type(
const char *x,
const TYPELIB *typelib, uint
flags)
71 int UNINIT_VAR(findpos);
74 DBUG_ENTER(
"find_type");
75 DBUG_PRINT(
"enter",(
"x: '%s' lib: 0x%lx", x, (
long) typelib));
77 DBUG_ASSERT(!(flags & ~(FIND_TYPE_NO_PREFIX | FIND_TYPE_ALLOW_NUMBER |
78 FIND_TYPE_NO_OVERWRITE | FIND_TYPE_COMMA_TERM)));
81 DBUG_PRINT(
"exit",(
"no count"));
85 for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
88 *i && (!(flags & FIND_TYPE_COMMA_TERM) || !is_field_separator(*i)) &&
89 my_toupper(&my_charset_latin1,*i) ==
90 my_toupper(&my_charset_latin1,*j) ; i++, j++) ;
95 if (! *i || ((flags & FIND_TYPE_COMMA_TERM) && is_field_separator(*i)))
99 (!(flags & FIND_TYPE_COMMA_TERM) || !is_field_separator(*i))) &&
100 (!*j || !(flags & FIND_TYPE_NO_PREFIX)))
106 if (find == 0 && (flags & FIND_TYPE_ALLOW_NUMBER) && x[0] ==
'#' &&
107 strend(x)[-1] ==
'#' &&
108 (findpos=atoi(x+1)-1) >= 0 && (uint) findpos < typelib->count)
110 else if (find == 0 || ! x[0])
112 DBUG_PRINT(
"exit",(
"Couldn't find type"));
115 else if (find != 1 || (flags & FIND_TYPE_NO_PREFIX))
117 DBUG_PRINT(
"exit",(
"Too many possybilities"));
120 DBUG_RETURN(findpos+1);
131 void make_type(
register char *
to,
register uint nr,
134 DBUG_ENTER(
"make_type");
138 (
void) strmov(to,get_type(typelib,nr-1));
150 const char *get_type(
TYPELIB *typelib, uint nr)
152 if (nr < (uint) typelib->count && typelib->type_names)
153 return(typelib->type_names[nr]);
171 my_ulonglong find_typeset(
char *x,
TYPELIB *lib,
int *err)
176 DBUG_ENTER(
"find_set");
177 DBUG_PRINT(
"enter",(
"x: '%s' lib: 0x%lx", x, (
long) lib));
181 DBUG_PRINT(
"exit",(
"no count"));
190 while (*x && !is_field_separator(*x))
194 if ((find= find_type(i, lib, FIND_TYPE_COMMA_TERM) - 1) < 0)
196 result|= (ULL(1) << find);
226 if (!(to->type_names= (
const char **)
227 alloc_root(root, (
sizeof(
char *) +
sizeof(
int)) * (from->count + 1))))
229 to->type_lengths= (
unsigned int *)(to->type_names + from->count + 1);
230 to->count= from->count;
233 if (!(to->name= strdup_root(root, from->name)))
239 for (i= 0; i < from->count; i++)
241 if (!(to->type_names[i]= strmake_root(root, from->type_names[i],
242 from->type_lengths[i])))
244 to->type_lengths[
i]= from->type_lengths[
i];
246 to->type_names[to->count]= NULL;
247 to->type_lengths[to->count]= 0;
253 static const char *on_off_default_names[]= {
"off",
"on",
"default", 0};
254 static TYPELIB on_off_default_typelib= {array_elements(on_off_default_names)-1,
255 "", on_off_default_names, 0};
275 static uint parse_name(
const TYPELIB *lib,
const char **strpos,
const char *end)
277 const char *pos= *strpos;
278 uint find= find_type(pos, lib, FIND_TYPE_COMMA_TERM);
279 for (; pos != end && *pos !=
'=' && *pos !=
',' ; pos++);
319 my_ulonglong find_set_from_flags(
const TYPELIB *lib, uint default_name,
320 my_ulonglong cur_set, my_ulonglong default_set,
321 const char *str, uint length,
322 char **err_pos, uint *err_len)
324 const char *end= str + length;
325 my_ulonglong flags_to_set= 0, flags_to_clear= 0, res;
326 my_bool set_defaults= 0;
331 const char *start= str;
334 const char *pos= start;
337 if (!(flag_no= parse_name(lib, &pos, end)))
340 if (flag_no == default_name)
349 my_ulonglong bit= (1ULL << (flag_no - 1));
351 if ((flags_to_clear | flags_to_set) & bit ||
352 pos >= end || *pos++ !=
'=' ||
353 !(value= parse_name(&on_off_default_typelib, &pos, end)))
357 flags_to_clear|= bit;
362 if (default_set & bit)
365 flags_to_clear|= bit;
377 *err_pos= (
char*)start;
378 *err_len= end - start;
382 res= set_defaults? default_set : cur_set;
384 res&= ~flags_to_clear;