~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/charset.cc

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
17
 
#include "mysys_err.h"
 
16
#include "mysys/mysys_priv.h"
 
17
#include "mysys/mysys_err.h"
18
18
#include <mystrings/m_ctype.h>
19
19
#include <mystrings/m_string.h>
20
 
#include <my_dir.h>
 
20
#include <drizzled/configmake.h>
21
21
 
22
22
 
23
23
/*
24
24
  The code below implements this functionality:
25
 
  
 
25
 
26
26
    - Initializing charset related structures
27
27
    - Loading dynamic charsets
28
 
    - Searching for a proper CHARSET_INFO 
 
28
    - Searching for a proper CHARSET_INFO
29
29
      using charset name, collation name or collation ID
30
30
    - Setting server default character set
31
31
*/
44
44
       cs < all_charsets+array_elements(all_charsets)-1 ;
45
45
       cs++)
46
46
  {
47
 
    if ( cs[0] && cs[0]->name && 
 
47
    if ( cs[0] && cs[0]->name &&
48
48
         !my_strcasecmp(&my_charset_utf8_general_ci, cs[0]->name, name))
49
49
      return cs[0]->number;
50
 
  }  
 
50
  }
51
51
  return 0;
52
52
}
53
53
 
58
58
  unsigned char *state_map;
59
59
  unsigned char *ident_map;
60
60
 
61
 
  if (!(cs->state_map= (unsigned char*) my_once_alloc(256, MYF(MY_WME))))
 
61
  if (!(cs->state_map= (unsigned char*) malloc(256)))
62
62
    return 1;
63
63
    
64
 
  if (!(cs->ident_map= (unsigned char*) my_once_alloc(256, MYF(MY_WME))))
 
64
  if (!(cs->ident_map= (unsigned char*) malloc(256)))
65
65
    return 1;
66
66
 
67
67
  state_map= cs->state_map;
68
68
  ident_map= cs->ident_map;
69
 
  
 
69
 
70
70
  /* Fill state_map with states to get a faster parser */
71
71
  for (i=0; i < 256 ; i++)
72
72
  {
116
116
}
117
117
 
118
118
 
119
 
#define MY_MAX_ALLOWED_BUF 1024*1024
120
 
#define MY_CHARSET_INDEX "Index.xml"
121
 
 
122
 
const char *charsets_dir= NULL;
123
 
static int charset_initialized=0;
124
 
 
125
 
 
126
 
char *get_charsets_dir(char *buf)
127
 
{
128
 
  const char *sharedir= SHAREDIR;
129
 
  char *res;
130
 
 
131
 
  if (charsets_dir != NULL)
132
 
    strmake(buf, charsets_dir, FN_REFLEN-1);
133
 
  else
134
 
  {
135
 
    if (test_if_hard_path(sharedir) ||
136
 
        is_prefix(sharedir, DEFAULT_CHARSET_HOME))
137
 
      strxmov(buf, sharedir, "/", CHARSET_DIR, NULL);
138
 
    else
139
 
      strxmov(buf, DEFAULT_CHARSET_HOME, "/", sharedir, "/", CHARSET_DIR,
140
 
              NULL);
141
 
  }
142
 
  res= convert_dirname(buf,buf,NULL);
143
 
  return(res);
144
 
}
 
119
static bool charset_initialized= false;
145
120
 
146
121
CHARSET_INFO *all_charsets[256];
147
122
const CHARSET_INFO *default_charset_info = &my_charset_utf8_general_ci;
152
127
  cs->state|= MY_CS_AVAILABLE;
153
128
}
154
129
 
155
 
static void *cs_alloc(size_t size)
 
130
void *cs_alloc(size_t size)
156
131
{
157
 
  return my_once_alloc(size, MYF(MY_WME));
 
132
  return malloc(size);
158
133
}
159
134
 
160
135
 
161
136
static bool init_available_charsets(myf myflags)
162
137
{
163
 
  char fname[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
164
 
  bool error=false;
 
138
  bool error= false;
165
139
  /*
166
140
    We have to use charset_initialized to not lock on THR_LOCK_charset
167
141
    inside get_internal_charset...
168
142
  */
169
 
  if (!charset_initialized)
 
143
  if (charset_initialized == false)
170
144
  {
171
145
    CHARSET_INFO **cs;
172
 
    /*
173
 
      To make things thread safe we are not allowing other threads to interfere
174
 
      while we may changing the cs_info_table
175
 
    */
176
 
    pthread_mutex_lock(&THR_LOCK_charset);
177
 
    if (!charset_initialized)
 
146
    memset(&all_charsets, 0, sizeof(all_charsets));
 
147
    init_compiled_charsets(myflags);
 
148
 
 
149
    /* Copy compiled charsets */
 
150
    for (cs=all_charsets;
 
151
         cs < all_charsets+array_elements(all_charsets)-1 ;
 
152
         cs++)
178
153
    {
179
 
      memset(&all_charsets, 0, sizeof(all_charsets));
180
 
      init_compiled_charsets(myflags);
181
 
      
182
 
      /* Copy compiled charsets */
183
 
      for (cs=all_charsets;
184
 
           cs < all_charsets+array_elements(all_charsets)-1 ;
185
 
           cs++)
 
154
      if (*cs)
186
155
      {
187
 
        if (*cs)
188
 
        {
189
 
          if (cs[0]->ctype)
190
 
            if (init_state_maps(*cs))
191
 
              *cs= NULL;
192
 
        }
 
156
        if (cs[0]->ctype)
 
157
          if (init_state_maps(*cs))
 
158
            *cs= NULL;
193
159
      }
194
 
      
195
 
      my_stpcpy(get_charsets_dir(fname), MY_CHARSET_INDEX);
196
 
      charset_initialized=1;
197
160
    }
198
 
    pthread_mutex_unlock(&THR_LOCK_charset);
 
161
 
 
162
    charset_initialized= true;
199
163
  }
 
164
  assert(charset_initialized);
 
165
 
200
166
  return error;
201
167
}
202
168
 
203
169
 
204
170
void free_charsets(void)
205
171
{
206
 
  charset_initialized=0;
 
172
  charset_initialized= true;
207
173
}
208
174
 
209
175
 
218
184
{
219
185
  CHARSET_INFO **cs;
220
186
  init_available_charsets(MYF(0));
221
 
  
 
187
 
222
188
  for (cs= all_charsets;
223
189
       cs < all_charsets+array_elements(all_charsets)-1 ;
224
190
       cs++)
226
192
    if ( cs[0] && cs[0]->csname && (cs[0]->state & cs_flags) &&
227
193
         !my_strcasecmp(&my_charset_utf8_general_ci, cs[0]->csname, charset_name))
228
194
      return cs[0]->number;
229
 
  }  
 
195
  }
230
196
  return 0;
231
197
}
232
198
 
239
205
  cs=all_charsets[charset_number];
240
206
  if (cs && (cs->number == charset_number) && cs->name )
241
207
    return (char*) cs->name;
242
 
  
 
208
 
243
209
  return (char*) "?";   /* this mimics find_type() */
244
210
}
245
211
 
251
217
    To make things thread safe we are not allowing other threads to interfere
252
218
    while we may changing the cs_info_table
253
219
  */
254
 
  pthread_mutex_lock(&THR_LOCK_charset);
255
220
  if ((cs= all_charsets[cs_number]))
256
221
  {
257
222
    if (!(cs->state & MY_CS_COMPILED) && !(cs->state & MY_CS_LOADED))
268
233
    else
269
234
      cs->state|= MY_CS_READY;
270
235
  }
271
 
  pthread_mutex_unlock(&THR_LOCK_charset);
 
236
 
272
237
  return cs;
273
238
}
274
239
 
275
240
 
276
 
const const CHARSET_INFO *get_charset(uint32_t cs_number, myf flags)
 
241
const CHARSET_INFO *get_charset(uint32_t cs_number)
277
242
{
278
243
  const CHARSET_INFO *cs;
279
244
  if (cs_number == default_charset_info->number)
280
245
    return default_charset_info;
281
246
 
282
247
  (void) init_available_charsets(MYF(0));       /* If it isn't initialized */
283
 
  
 
248
 
284
249
  if (!cs_number || cs_number >= array_elements(all_charsets)-1)
285
250
    return NULL;
286
 
  
 
251
 
287
252
  cs= get_internal_charset(cs_number);
288
253
 
289
 
  if (!cs && (flags & MY_WME))
290
 
  {
291
 
    char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)], cs_string[23];
292
 
    my_stpcpy(get_charsets_dir(index_file),MY_CHARSET_INDEX);
293
 
    cs_string[0]='#';
294
 
    int10_to_str(cs_number, cs_string+1, 10);
295
 
    my_error(EE_UNKNOWN_CHARSET, MYF(ME_BELL), cs_string, index_file);
296
 
  }
297
254
  return cs;
298
255
}
299
256
 
300
 
const CHARSET_INFO *get_charset_by_name(const char *cs_name, myf flags)
 
257
const CHARSET_INFO *get_charset_by_name(const char *cs_name)
301
258
{
302
259
  uint32_t cs_number;
303
260
  const CHARSET_INFO *cs;
306
263
  cs_number=get_collation_number(cs_name);
307
264
  cs= cs_number ? get_internal_charset(cs_number) : NULL;
308
265
 
309
 
  if (!cs && (flags & MY_WME))
310
 
  {
311
 
    char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
312
 
    my_stpcpy(get_charsets_dir(index_file),MY_CHARSET_INDEX);
313
 
    my_error(EE_UNKNOWN_COLLATION, MYF(ME_BELL), cs_name, index_file);
314
 
  }
315
 
 
316
266
  return cs;
317
267
}
318
268
 
319
269
 
320
 
const CHARSET_INFO *get_charset_by_csname(const char *cs_name,
321
 
                                    uint32_t cs_flags,
322
 
                                    myf flags)
 
270
const CHARSET_INFO *get_charset_by_csname(const char *cs_name, uint32_t cs_flags)
323
271
{
324
272
  uint32_t cs_number;
325
273
  const CHARSET_INFO *cs;
329
277
  cs_number= get_charset_number(cs_name, cs_flags);
330
278
  cs= cs_number ? get_internal_charset(cs_number) : NULL;
331
279
 
332
 
  if (!cs && (flags & MY_WME))
333
 
  {
334
 
    char index_file[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
335
 
    my_stpcpy(get_charsets_dir(index_file),MY_CHARSET_INDEX);
336
 
    my_error(EE_UNKNOWN_CHARSET, MYF(ME_BELL), cs_name, index_file);
337
 
  }
338
 
 
339
280
  return(cs);
340
281
}
341
282
 
360
301
                     const CHARSET_INFO *default_cs,
361
302
                     const CHARSET_INFO **cs)
362
303
{
363
 
  *cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0));
 
304
  *cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY);
364
305
 
365
306
  if (*cs == NULL)
366
307
  {
392
333
                       const CHARSET_INFO *default_cl,
393
334
                       const CHARSET_INFO **cl)
394
335
{
395
 
  *cl= get_charset_by_name(cl_name, MYF(0));
 
336
  *cl= get_charset_by_name(cl_name);
396
337
 
397
338
  if (*cl == NULL)
398
339
  {