~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 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
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include <my_global.h>
17
#include <m_string.h>
18
#include <m_ctype.h>
19
#include <fcntl.h>
20
#include <my_xml.h>
21
22
#define ROW_LEN		16
23
#define ROW16_LEN	8
24
#define MAX_BUF		64*1024
25
26
static CHARSET_INFO all_charsets[256];
27
28
29
void
30
print_array(FILE *f, const char *set, const char *name, uchar *a, int n)
31
{
32
  int i;
33
34
  fprintf(f,"uchar %s_%s[] = {\n", name, set);
35
  
36
  for (i=0 ;i<n ; i++)
37
  {
38
    fprintf(f,"0x%02X",a[i]);
39
    fprintf(f, (i+1<n) ? "," :"" );
40
    fprintf(f, ((i+1) % ROW_LEN == n % ROW_LEN) ? "\n" : "" );
41
  }
42
  fprintf(f,"};\n\n");
43
}
44
45
46
void
47
print_array16(FILE *f, const char *set, const char *name, uint16 *a, int n)
48
{
49
  int i;
50
51
  fprintf(f,"uint16 %s_%s[] = {\n", name, set);
52
  
53
  for (i=0 ;i<n ; i++)
54
  {
55
    fprintf(f,"0x%04X",a[i]);
56
    fprintf(f, (i+1<n) ? "," :"" );
57
    fprintf(f, ((i+1) % ROW16_LEN == n % ROW16_LEN) ? "\n" : "" );
58
  }
59
  fprintf(f,"};\n\n");
60
}
61
62
63
static int get_charset_number(const char *charset_name)
64
{
65
  CHARSET_INFO *cs;
66
  for (cs= all_charsets; cs < all_charsets+255; ++cs)
67
  {
68
    if ( cs->name && !strcmp(cs->name, charset_name))
69
      return cs->number;
70
  }  
71
  return 0;
72
}
73
74
char *mdup(const char *src, uint len)
75
{
76
  char *dst=(char*)malloc(len);
77
  if (!dst)
78
    exit(1);
79
  memcpy(dst,src,len);
80
  return dst;
81
}
82
83
static void simple_cs_copy_data(CHARSET_INFO *to, CHARSET_INFO *from)
84
{
85
  to->number= from->number ? from->number : to->number;
86
  to->state|= from->state;
87
88
  if (from->csname)
89
    to->csname= strdup(from->csname);
90
  
91
  if (from->name)
92
    to->name= strdup(from->name);
93
  
94
  if (from->ctype)
95
    to->ctype= (uchar*) mdup((char*) from->ctype, MY_CS_CTYPE_TABLE_SIZE);
96
  if (from->to_lower)
97
    to->to_lower= (uchar*) mdup((char*) from->to_lower, MY_CS_TO_LOWER_TABLE_SIZE);
98
  if (from->to_upper)
99
    to->to_upper= (uchar*) mdup((char*) from->to_upper, MY_CS_TO_UPPER_TABLE_SIZE);
100
  if (from->sort_order)
101
  {
102
    to->sort_order= (uchar*) mdup((char*) from->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
103
    /*
104
      set_max_sort_char(to);
105
    */
106
  }
107
  if (from->tab_to_uni)
108
  {
109
    uint sz= MY_CS_TO_UNI_TABLE_SIZE*sizeof(uint16);
110
    to->tab_to_uni= (uint16*)  mdup((char*)from->tab_to_uni, sz);
111
    /*
112
    create_fromuni(to);
113
    */
114
  }
115
}
116
117
static my_bool simple_cs_is_full(CHARSET_INFO *cs)
118
{
119
  return ((cs->csname && cs->tab_to_uni && cs->ctype && cs->to_upper &&
120
	   cs->to_lower) &&
121
	  (cs->number && cs->name && 
122
	  (cs->sort_order || (cs->state & MY_CS_BINSORT))));
123
}
124
125
static int add_collation(CHARSET_INFO *cs)
126
{
127
  if (cs->name && (cs->number || (cs->number=get_charset_number(cs->name))))
128
  {
129
    if (!(all_charsets[cs->number].state & MY_CS_COMPILED))
130
    {
131
      simple_cs_copy_data(&all_charsets[cs->number],cs);
132
      
133
    }
134
    
135
    cs->number= 0;
136
    cs->name= NULL;
137
    cs->state= 0;
138
    cs->sort_order= NULL;
139
    cs->state= 0;
140
  }
141
  return MY_XML_OK;
142
}
143
144
145
static int my_read_charset_file(const char *filename)
146
{
147
  char buf[MAX_BUF];
148
  int  fd;
149
  uint len;
150
  
151
  if ((fd=open(filename,O_RDONLY)) < 0)
152
  {
153
    fprintf(stderr,"Can't open '%s'\n",filename);
154
    return 1;
155
  }
156
  
157
  len=read(fd,buf,MAX_BUF);
158
  DBUG_ASSERT(len < MAX_BUF);
159
  close(fd);
160
  
161
  if (my_parse_charset_xml(buf,len,add_collation))
162
  {
163
#if 0
164
    printf("ERROR at line %d pos %d '%s'\n",
165
	   my_xml_error_lineno(&p)+1,
166
	   my_xml_error_pos(&p),
167
	   my_xml_error_string(&p));
168
#endif
169
  }
170
  
171
  return FALSE;
172
}
173
174
static int
175
is_case_sensitive(CHARSET_INFO *cs)
176
{
177
 return (cs->sort_order &&
178
         cs->sort_order['A'] < cs->sort_order['a'] &&
179
         cs->sort_order['a'] < cs->sort_order['B']) ? 1 : 0;
180
}
181
182
183
void dispcset(FILE *f,CHARSET_INFO *cs)
184
{
185
  fprintf(f,"{\n");
186
  fprintf(f,"  %d,%d,%d,\n",cs->number,0,0);
187
  fprintf(f,"  MY_CS_COMPILED%s%s%s%s%s,\n",
188
          cs->state & MY_CS_BINSORT         ? "|MY_CS_BINSORT"   : "",
189
          cs->state & MY_CS_PRIMARY         ? "|MY_CS_PRIMARY"   : "",
190
          is_case_sensitive(cs)             ? "|MY_CS_CSSORT"    : "",
191
          my_charset_is_8bit_pure_ascii(cs) ? "|MY_CS_PUREASCII" : "",
192
          !my_charset_is_ascii_compatible(cs) ? "|MY_CS_NONASCII": "");
193
  
194
  if (cs->name)
195
  {
196
    fprintf(f,"  \"%s\",                     /* cset name     */\n",cs->csname);
197
    fprintf(f,"  \"%s\",                     /* coll name     */\n",cs->name);
198
    fprintf(f,"  \"\",                       /* comment       */\n");
199
    fprintf(f,"  NULL,                       /* tailoring     */\n");
200
    fprintf(f,"  ctype_%s,                   /* ctype         */\n",cs->name);
201
    fprintf(f,"  to_lower_%s,                /* lower         */\n",cs->name);
202
    fprintf(f,"  to_upper_%s,                /* upper         */\n",cs->name);
203
    if (cs->sort_order)
204
      fprintf(f,"  sort_order_%s,            /* sort_order    */\n",cs->name);
205
    else
206
      fprintf(f,"  NULL,                     /* sort_order    */\n");
207
    fprintf(f,"  NULL,                       /* contractions  */\n");
208
    fprintf(f,"  NULL,                       /* sort_order_big*/\n");
209
    fprintf(f,"  to_uni_%s,                  /* to_uni        */\n",cs->name);
210
  }
211
  else
212
  {
213
    fprintf(f,"  NULL,                       /* cset name     */\n");
214
    fprintf(f,"  NULL,                       /* coll name     */\n");
215
    fprintf(f,"  NULL,                       /* comment       */\n");
216
    fprintf(f,"  NULL,                       /* tailoging     */\n");
217
    fprintf(f,"  NULL,                       /* ctype         */\n");
218
    fprintf(f,"  NULL,                       /* lower         */\n");
219
    fprintf(f,"  NULL,                       /* upper         */\n");
220
    fprintf(f,"  NULL,                       /* sort order    */\n");
221
    fprintf(f,"  NULL,                       /* contractions  */\n");
222
    fprintf(f,"  NULL,                       /* sort_order_big*/\n");
223
    fprintf(f,"  NULL,                       /* to_uni        */\n");
224
  }
225
226
  fprintf(f,"  NULL,                       /* from_uni      */\n");
227
  fprintf(f,"  my_unicase_default,         /* caseinfo      */\n");
228
  fprintf(f,"  NULL,                       /* state map     */\n");
229
  fprintf(f,"  NULL,                       /* ident map     */\n");
230
  fprintf(f,"  1,                          /* strxfrm_multiply*/\n");
231
  fprintf(f,"  1,                          /* caseup_multiply*/\n");
232
  fprintf(f,"  1,                          /* casedn_multiply*/\n");
233
  fprintf(f,"  1,                          /* mbminlen      */\n");
234
  fprintf(f,"  1,                          /* mbmaxlen      */\n");
235
  fprintf(f,"  0,                          /* min_sort_char */\n");
236
  fprintf(f,"  255,                        /* max_sort_char */\n");
237
  fprintf(f,"  ' ',                        /* pad_char      */\n");
238
  fprintf(f,"  0,                          /* escape_with_backslash_is_dangerous */\n");
239
  fprintf(f,"  1,                          /* levels_for_compare */\n");
240
  fprintf(f,"  1,                          /* levels_for_order   */\n");
241
  
242
  fprintf(f,"  &my_charset_8bit_handler,\n");
243
  if (cs->state & MY_CS_BINSORT)
244
    fprintf(f,"  &my_collation_8bit_bin_handler,\n");
245
  else
246
    fprintf(f,"  &my_collation_8bit_simple_ci_handler,\n");
247
  fprintf(f,"}\n");
248
}
249
250
251
static void
252
fprint_copyright(FILE *file)
253
{
254
  fprintf(file,
255
"/* Copyright (C) 2000-2007 MySQL AB\n"
256
"\n"
257
"   This program is free software; you can redistribute it and/or modify\n"
258
"   it under the terms of the GNU General Public License as published by\n"
259
"   the Free Software Foundation; version 2 of the License.\n"
260
"\n"
261
"   This program is distributed in the hope that it will be useful,\n"
262
"   but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
263
"   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
264
"   GNU General Public License for more details.\n"
265
"\n"
266
"   You should have received a copy of the GNU General Public License\n"
267
"   along with this program; if not, write to the Free Software\n"
268
"   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */\n"
269
"\n");
270
}
271
272
273
int
274
main(int argc, char **argv  __attribute__((unused)))
275
{
276
  CHARSET_INFO  ncs;
277
  CHARSET_INFO  *cs;
278
  char filename[256];
279
  FILE *f= stdout;
280
  
281
  if (argc < 2)
282
  {
283
    fprintf(stderr, "usage: %s source-dir\n", argv[0]);
284
    exit(EXIT_FAILURE);
285
  }
286
  
287
  bzero((void*)&ncs,sizeof(ncs));
288
  bzero((void*)&all_charsets,sizeof(all_charsets));
289
  
290
  sprintf(filename,"%s/%s",argv[1],"Index.xml");
291
  my_read_charset_file(filename);
292
  
293
  for (cs=all_charsets; cs < all_charsets+256; cs++)
294
  {
295
    if (cs->number && !(cs->state & MY_CS_COMPILED))
296
    {
297
      if ( (!simple_cs_is_full(cs)) && (cs->csname))
298
      {
299
        sprintf(filename,"%s/%s.xml",argv[1],cs->csname);
300
        my_read_charset_file(filename);
301
      }
302
    }
303
  }
304
  
305
  fprintf(f, "/*\n");
306
  fprintf(f, "  This file was generated by the conf_to_src utility. "
307
          "Do not edit it directly,\n");
308
  fprintf(f, "  edit the XML definitions in sql/share/charsets/ instead.\n\n");
309
  fprintf(f, "  To re-generate, run the following in the strings/ "
310
          "directory:\n");
311
  fprintf(f, "    ./conf_to_src ../sql/share/charsets/ > FILE\n");
312
  fprintf(f, "*/\n\n");
313
  fprint_copyright(f);
314
  fprintf(f,"#include <my_global.h>\n");
315
  fprintf(f,"#include <m_ctype.h>\n\n");
316
  
317
  
318
  for (cs=all_charsets; cs < all_charsets+256; cs++)
319
  {
320
    if (simple_cs_is_full(cs))
321
    {
322
      fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
323
      print_array(f, cs->name, "ctype",      cs->ctype,      MY_CS_CTYPE_TABLE_SIZE);
324
      print_array(f, cs->name, "to_lower",   cs->to_lower,   MY_CS_TO_LOWER_TABLE_SIZE);
325
      print_array(f, cs->name, "to_upper",   cs->to_upper,   MY_CS_TO_UPPER_TABLE_SIZE);
326
      if (cs->sort_order)
327
        print_array(f, cs->name, "sort_order", cs->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
328
      print_array16(f, cs->name, "to_uni",     cs->tab_to_uni, MY_CS_TO_UNI_TABLE_SIZE);
329
      fprintf(f,"#endif\n");
330
      fprintf(f,"\n");
331
    }
332
  }
333
  
334
  fprintf(f,"CHARSET_INFO compiled_charsets[] = {\n");
335
  for (cs=all_charsets; cs < all_charsets+256; cs++)
336
  {
337
    if (simple_cs_is_full(cs))
338
    {
339
      fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
340
      dispcset(f,cs);
341
      fprintf(f,",\n");
342
      fprintf(f,"#endif\n");
343
    }
344
  }
345
  
346
  dispcset(f,&ncs);
347
  fprintf(f,"};\n");
348
  
349
  return 0;
350
}