~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2003 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
/* Some useful string utility functions used by the MySQL server */
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
17
#include "config.h"
18
19
#include "drizzled/strfunc.h"
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
20
#include "drizzled/typelib.h"
1241.9.61 by Monty Taylor
No more mystrings in drizzled/
21
#include "drizzled/charset_info.h"
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
22
#include "drizzled/global_charset_info.h"
1 by brian
clean slate
23
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
24
namespace drizzled
25
{
26
1 by brian
clean slate
27
/*
28
  Return bitmap for strings used in a set
29
30
  SYNOPSIS
31
  find_set()
32
  lib			Strings in set
33
  str			Strings of set-strings separated by ','
34
  err_pos		If error, set to point to start of wrong set string
35
  err_len		If error, set to the length of wrong set string
36
  set_warning		Set to 1 if some string in set couldn't be used
37
38
  NOTE
39
    We delete all end space from str before comparison
40
41
  RETURN
42
    bitmap of all sets found in x.
43
    set_warning is set to 1 if there was any sets that couldn't be set
44
*/
45
46
static const char field_separator=',';
47
482 by Brian Aker
Remove uint.
48
uint64_t find_set(TYPELIB *lib, const char *str, uint32_t length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
49
                  const CHARSET_INFO * const cs,
482 by Brian Aker
Remove uint.
50
                  char **err_pos, uint32_t *err_len, bool *set_warning)
1 by brian
clean slate
51
{
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
52
  const CHARSET_INFO * const strip= cs ? cs : &my_charset_utf8_general_ci;
1 by brian
clean slate
53
  const char *end= str + strip->cset->lengthsp(strip, str, length);
151 by Brian Aker
Ulonglong to uint64_t
54
  uint64_t found= 0;
1 by brian
clean slate
55
  *err_pos= 0;                  // No error yet
56
  if (str != end)
57
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
58
    const char *start= str;
1 by brian
clean slate
59
    for (;;)
60
    {
61
      const char *pos= start;
482 by Brian Aker
Remove uint.
62
      uint32_t var_len;
1 by brian
clean slate
63
      int mblen= 1;
64
1054.2.12 by Monty Taylor
First pass at removing strconvert.
65
      for (; pos != end && *pos != field_separator; pos++) 
66
      {}
895 by Brian Aker
Completion (?) of uint conversion.
67
      var_len= (uint32_t) (pos - start);
482 by Brian Aker
Remove uint.
68
      uint32_t find= cs ? find_type2(lib, start, var_len, cs) :
1 by brian
clean slate
69
                      find_type(lib, start, var_len, (bool) 0);
70
      if (!find)
71
      {
72
        *err_pos= (char*) start;
73
        *err_len= var_len;
74
        *set_warning= 1;
75
      }
76
      else
152 by Brian Aker
longlong replacement
77
        found|= ((int64_t) 1 << (find - 1));
1 by brian
clean slate
78
      if (pos >= end)
79
        break;
80
      start= pos + mblen;
81
    }
82
  }
83
  return found;
84
}
85
86
87
/*
88
  Function to find a string in a TYPELIB
89
  (Same format as mysys/typelib.c)
90
91
  SYNOPSIS
92
   find_type()
93
   lib			TYPELIB (struct of pointer to values + count)
94
   find			String to find
95
   length		Length of string to find
96
   part_match		Allow part matching of value
97
98
 RETURN
99
  0 error
100
  > 0 position in TYPELIB->type_names +1
101
*/
102
482 by Brian Aker
Remove uint.
103
uint32_t find_type(const TYPELIB *lib, const char *find, uint32_t length,
1 by brian
clean slate
104
               bool part_match)
105
{
482 by Brian Aker
Remove uint.
106
  uint32_t found_count=0, found_pos=0;
1 by brian
clean slate
107
  const char *end= find+length;
108
  const char *i;
109
  const char *j;
482 by Brian Aker
Remove uint.
110
  for (uint32_t pos=0 ; (j=lib->type_names[pos++]) ; )
1 by brian
clean slate
111
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
112
    for (i=find ; i != end &&
113
	   my_toupper(system_charset_info,*i) ==
1 by brian
clean slate
114
	   my_toupper(system_charset_info,*j) ; i++, j++) ;
115
    if (i == end)
116
    {
117
      if (! *j)
118
	return(pos);
119
      found_count++;
120
      found_pos= pos;
121
    }
122
  }
123
  return(found_count == 1 && part_match ? found_pos : 0);
124
}
125
126
127
/*
128
  Find a string in a list of strings according to collation
129
130
  SYNOPSIS
131
   find_type2()
132
   lib			TYPELIB (struct of pointer to values + count)
133
   x			String to find
134
   length               String length
135
   cs			Character set + collation to use for comparison
136
137
  NOTES
138
139
  RETURN
140
    0	No matching value
141
    >0  Offset+1 in typelib for matched string
142
*/
143
482 by Brian Aker
Remove uint.
144
uint32_t find_type2(const TYPELIB *typelib, const char *x, uint32_t length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
145
                const CHARSET_INFO * const cs)
1 by brian
clean slate
146
{
147
  int pos;
148
  const char *j;
149
150
  if (!typelib->count)
151
  {
51.1.69 by Jay Pipes
Removed/replaced DBUG symbols
152
    return(0);
1 by brian
clean slate
153
  }
154
155
  for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
156
  {
481 by Brian Aker
Remove all of uchar.
157
    if (!my_strnncoll(cs, (const unsigned char*) x, length,
158
                          (const unsigned char*) j, typelib->type_lengths[pos]))
51.1.69 by Jay Pipes
Removed/replaced DBUG symbols
159
      return(pos+1);
1 by brian
clean slate
160
  }
51.1.69 by Jay Pipes
Removed/replaced DBUG symbols
161
  return(0);
1 by brian
clean slate
162
} /* find_type */
163
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
164
} /* namespace drizzled */