~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/charset.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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