~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/charset.cc

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include <config.h>
 
16
#include "config.h"
17
17
 
18
 
#include <drizzled/charset.h>
19
 
#include <drizzled/error.h>
20
 
#include <drizzled/charset_info.h>
21
 
#include <drizzled/internal/m_string.h>
 
18
#include "drizzled/charset.h"
 
19
#include "drizzled/error.h"
 
20
#include "drizzled/charset_info.h"
 
21
#include "drizzled/internal/m_string.h"
22
22
#include <drizzled/configmake.h>
23
23
#include <vector>
24
24
 
25
 
#include <drizzled/visibility.h>
26
 
 
27
25
using namespace std;
28
26
 
29
27
namespace drizzled
32
30
/*
33
31
  We collect memory in this vector that we free on delete.
34
32
*/
35
 
static vector<unsigned char*> memory_vector;
 
33
static vector<void *>memory_vector;
36
34
 
37
35
/*
38
36
  The code below implements this functionality:
53
51
static uint
54
52
get_collation_number_internal(const char *name)
55
53
{
56
 
  for (CHARSET_INFO **cs= all_charsets;
57
 
       cs < all_charsets+array_elements(all_charsets)-1;
 
54
  CHARSET_INFO **cs;
 
55
  for (cs= all_charsets;
 
56
       cs < all_charsets+array_elements(all_charsets)-1 ;
58
57
       cs++)
59
58
  {
60
59
    if ( cs[0] && cs[0]->name && !my_strcasecmp(&my_charset_utf8_general_ci, cs[0]->name, name))
65
64
  return 0;
66
65
}
67
66
 
68
 
static unsigned char *cs_alloc(size_t size)
69
 
{
70
 
  memory_vector.push_back(new unsigned char[size]);
71
 
  return memory_vector.back();
72
 
}
73
67
 
74
68
static bool init_state_maps(CHARSET_INFO *cs)
75
69
{
76
 
  if (!(cs->state_map= cs_alloc(256)))
 
70
  uint32_t i;
 
71
  unsigned char *state_map;
 
72
  unsigned char *ident_map;
 
73
 
 
74
  if (!(cs->state_map= (unsigned char*) cs_alloc(256)))
77
75
    return 1;
78
76
    
79
 
  if (!(cs->ident_map= cs_alloc(256)))
 
77
  if (!(cs->ident_map= (unsigned char*) cs_alloc(256)))
80
78
    return 1;
81
79
 
82
 
  unsigned char *state_map= cs->state_map;
83
 
  unsigned char *ident_map= cs->ident_map;
 
80
  state_map= cs->state_map;
 
81
  ident_map= cs->ident_map;
84
82
 
85
83
  /* Fill state_map with states to get a faster parser */
86
 
  for (int i= 0; i < 256; i++)
 
84
  for (i=0; i < 256 ; i++)
87
85
  {
88
86
    if (my_isalpha(cs,i))
89
 
      state_map[i]= MY_LEX_IDENT;
 
87
      state_map[i]=(unsigned char) MY_LEX_IDENT;
90
88
    else if (my_isdigit(cs,i))
91
 
      state_map[i]= MY_LEX_NUMBER_IDENT;
 
89
      state_map[i]=(unsigned char) MY_LEX_NUMBER_IDENT;
92
90
    else if (my_mbcharlen(cs, i)>1)
93
 
      state_map[i]= MY_LEX_IDENT;
 
91
      state_map[i]=(unsigned char) MY_LEX_IDENT;
94
92
    else if (my_isspace(cs,i))
95
 
      state_map[i]= MY_LEX_SKIP;
 
93
      state_map[i]=(unsigned char) MY_LEX_SKIP;
96
94
    else
97
 
      state_map[i]= MY_LEX_CHAR;
 
95
      state_map[i]=(unsigned char) MY_LEX_CHAR;
98
96
  }
99
 
  state_map['_']=state_map['$']= MY_LEX_IDENT;
100
 
  state_map['\'']= MY_LEX_STRING;
101
 
  state_map['.']= MY_LEX_REAL_OR_POINT;
102
 
  state_map['>']=state_map['=']=state_map['!']=  MY_LEX_CMP_OP;
103
 
  state_map['<']=  MY_LEX_LONG_CMP_OP;
104
 
  state_map['&']=state_map['|']= MY_LEX_BOOL;
105
 
  state_map['#']= MY_LEX_COMMENT;
106
 
  state_map[';']= MY_LEX_SEMICOLON;
107
 
  state_map[':']= MY_LEX_SET_VAR;
108
 
  state_map[0]= MY_LEX_EOL;
109
 
  state_map['\\']=  MY_LEX_ESCAPE;
110
 
  state_map['/']=  MY_LEX_LONG_COMMENT;
111
 
  state_map['*']=  MY_LEX_END_LONG_COMMENT;
112
 
  state_map['@']=  MY_LEX_USER_END;
113
 
  state_map['`']=  MY_LEX_USER_VARIABLE_DELIMITER;
114
 
  state_map['"']=  MY_LEX_STRING_OR_DELIMITER;
 
97
  state_map[(unsigned char)'_']=state_map[(unsigned char)'$']=(unsigned char) MY_LEX_IDENT;
 
98
  state_map[(unsigned char)'\'']=(unsigned char) MY_LEX_STRING;
 
99
  state_map[(unsigned char)'.']=(unsigned char) MY_LEX_REAL_OR_POINT;
 
100
  state_map[(unsigned char)'>']=state_map[(unsigned char)'=']=state_map[(unsigned char)'!']= (unsigned char) MY_LEX_CMP_OP;
 
101
  state_map[(unsigned char)'<']= (unsigned char) MY_LEX_LONG_CMP_OP;
 
102
  state_map[(unsigned char)'&']=state_map[(unsigned char)'|']=(unsigned char) MY_LEX_BOOL;
 
103
  state_map[(unsigned char)'#']=(unsigned char) MY_LEX_COMMENT;
 
104
  state_map[(unsigned char)';']=(unsigned char) MY_LEX_SEMICOLON;
 
105
  state_map[(unsigned char)':']=(unsigned char) MY_LEX_SET_VAR;
 
106
  state_map[0]=(unsigned char) MY_LEX_EOL;
 
107
  state_map[(unsigned char)'\\']= (unsigned char) MY_LEX_ESCAPE;
 
108
  state_map[(unsigned char)'/']= (unsigned char) MY_LEX_LONG_COMMENT;
 
109
  state_map[(unsigned char)'*']= (unsigned char) MY_LEX_END_LONG_COMMENT;
 
110
  state_map[(unsigned char)'@']= (unsigned char) MY_LEX_USER_END;
 
111
  state_map[(unsigned char) '`']= (unsigned char) MY_LEX_USER_VARIABLE_DELIMITER;
 
112
  state_map[(unsigned char)'"']= (unsigned char) MY_LEX_STRING_OR_DELIMITER;
115
113
 
116
114
  /*
117
115
    Create a second map to make it faster to find identifiers
118
116
  */
119
 
  for (int i= 0; i < 256; i++)
 
117
  for (i=0; i < 256 ; i++)
120
118
  {
121
 
    ident_map[i]= state_map[i] == MY_LEX_IDENT || state_map[i] == MY_LEX_NUMBER_IDENT;
 
119
    ident_map[i]= (unsigned char) (state_map[i] == MY_LEX_IDENT ||
 
120
                           state_map[i] == MY_LEX_NUMBER_IDENT);
122
121
  }
123
122
 
124
123
  /* Special handling of hex and binary strings */
125
 
  state_map['x']= state_map['X']=  MY_LEX_IDENT_OR_HEX;
126
 
  state_map['b']= state_map['B']=  MY_LEX_IDENT_OR_BIN;
 
124
  state_map[(unsigned char)'x']= state_map[(unsigned char)'X']= (unsigned char) MY_LEX_IDENT_OR_HEX;
 
125
  state_map[(unsigned char)'b']= state_map[(unsigned char)'B']= (unsigned char) MY_LEX_IDENT_OR_BIN;
127
126
  return 0;
128
127
}
129
128
 
 
129
 
130
130
static bool charset_initialized= false;
131
131
 
132
 
DRIZZLED_API CHARSET_INFO *all_charsets[256];
133
 
const DRIZZLED_API CHARSET_INFO *default_charset_info = &my_charset_utf8_general_ci;
 
132
CHARSET_INFO *all_charsets[256];
 
133
const CHARSET_INFO *default_charset_info = &my_charset_utf8_general_ci;
134
134
 
135
135
void add_compiled_collation(CHARSET_INFO * cs)
136
136
{
138
138
  cs->state|= MY_CS_AVAILABLE;
139
139
}
140
140
 
 
141
void *cs_alloc(size_t size)
 
142
{
 
143
  void *ptr= malloc(size);
 
144
 
 
145
  memory_vector.push_back(ptr);
 
146
 
 
147
  return ptr;
 
148
}
 
149
 
 
150
 
 
151
 
141
152
static bool init_available_charsets(myf myflags)
142
153
{
143
154
  bool error= false;
172
183
}
173
184
 
174
185
 
175
 
void free_charsets()
 
186
void free_charsets(void)
176
187
{
177
 
  charset_initialized= false;
 
188
  charset_initialized= true;
178
189
 
179
 
  while (not memory_vector.empty())
 
190
  while (memory_vector.empty() == false)
180
191
  {
181
 
    delete[] memory_vector.back();
 
192
    void *ptr= memory_vector.back();
182
193
    memory_vector.pop_back();
 
194
    free(ptr);
183
195
  }
 
196
  memory_vector.clear();
 
197
 
184
198
}
185
199
 
186
200
 
209
223
 
210
224
const char *get_charset_name(uint32_t charset_number)
211
225
{
 
226
  const CHARSET_INFO *cs;
212
227
  init_available_charsets(MYF(0));
213
228
 
214
 
  const CHARSET_INFO *cs= all_charsets[charset_number];
 
229
  cs=all_charsets[charset_number];
215
230
  if (cs && (cs->number == charset_number) && cs->name )
216
 
    return cs->name;
 
231
    return (char*) cs->name;
217
232
 
218
 
  return "?";   /* this mimics find_type() */
 
233
  return (char*) "?";   /* this mimics find_type() */
219
234
}
220
235
 
221
236
 
364
379
    }
365
380
  }
366
381
  *to= 0;
367
 
  return overflow ? UINT32_MAX : to - to_start;
 
382
  return overflow ? UINT32_MAX : (uint32_t) (to - to_start);
368
383
}
369
384
 
370
385
} /* namespace drizzled */