conf_to_src.c

Go to the documentation of this file.
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; either version 2 of the License, or
00006    (at your option) any later version.
00007 
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; if not, write to the Free Software
00015    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
00016 
00017 #include <my_global.h>
00018 #include <m_string.h>
00019 #include <m_ctype.h>
00020 #include <fcntl.h>
00021 #include <my_xml.h>
00022 
00023 #define ROW_LEN         16
00024 #define ROW16_LEN       8
00025 #define MAX_BUF         16*1024
00026 
00027 static CHARSET_INFO all_charsets[256];
00028 
00029 
00030 void
00031 print_array(FILE *f, const char *set, const char *name, uchar *a, int n)
00032 {
00033   int i;
00034 
00035   fprintf(f,"uchar %s_%s[] = {\n", name, set);
00036   
00037   for (i=0 ;i<n ; i++)
00038   {
00039     fprintf(f,"0x%02X",a[i]);
00040     fprintf(f, (i+1<n) ? "," :"" );
00041     fprintf(f, ((i+1) % ROW_LEN == n % ROW_LEN) ? "\n" : "" );
00042   }
00043   fprintf(f,"};\n\n");
00044 }
00045 
00046 
00047 void
00048 print_array16(FILE *f, const char *set, const char *name, uint16 *a, int n)
00049 {
00050   int i;
00051 
00052   fprintf(f,"uint16 %s_%s[] = {\n", name, set);
00053   
00054   for (i=0 ;i<n ; i++)
00055   {
00056     fprintf(f,"0x%04X",a[i]);
00057     fprintf(f, (i+1<n) ? "," :"" );
00058     fprintf(f, ((i+1) % ROW16_LEN == n % ROW16_LEN) ? "\n" : "" );
00059   }
00060   fprintf(f,"};\n\n");
00061 }
00062 
00063 
00064 static int get_charset_number(const char *charset_name)
00065 {
00066   CHARSET_INFO *cs;
00067   for (cs= all_charsets; cs < all_charsets+255; ++cs)
00068   {
00069     if ( cs->name && !strcmp(cs->name, charset_name))
00070       return cs->number;
00071   }  
00072   return 0;
00073 }
00074 
00075 char *mdup(const char *src, uint len)
00076 {
00077   char *dst=(char*)malloc(len);
00078   memcpy(dst,src,len);
00079   return dst;
00080 }
00081 
00082 static void simple_cs_copy_data(CHARSET_INFO *to, CHARSET_INFO *from)
00083 {
00084   to->number= from->number ? from->number : to->number;
00085   to->state|= from->state;
00086 
00087   if (from->csname)
00088     to->csname= strdup(from->csname);
00089   
00090   if (from->name)
00091     to->name= strdup(from->name);
00092   
00093   if (from->ctype)
00094     to->ctype= (uchar*) mdup((char*) from->ctype, MY_CS_CTYPE_TABLE_SIZE);
00095   if (from->to_lower)
00096     to->to_lower= (uchar*) mdup((char*) from->to_lower, MY_CS_TO_LOWER_TABLE_SIZE);
00097   if (from->to_upper)
00098     to->to_upper= (uchar*) mdup((char*) from->to_upper, MY_CS_TO_UPPER_TABLE_SIZE);
00099   if (from->sort_order)
00100   {
00101     to->sort_order= (uchar*) mdup((char*) from->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
00102     /*
00103       set_max_sort_char(to);
00104     */
00105   }
00106   if (from->tab_to_uni)
00107   {
00108     uint sz= MY_CS_TO_UNI_TABLE_SIZE*sizeof(uint16);
00109     to->tab_to_uni= (uint16*)  mdup((char*)from->tab_to_uni, sz);
00110     /*
00111     create_fromuni(to);
00112     */
00113   }
00114 }
00115 
00116 static my_bool simple_cs_is_full(CHARSET_INFO *cs)
00117 {
00118   return ((cs->csname && cs->tab_to_uni && cs->ctype && cs->to_upper &&
00119            cs->to_lower) &&
00120           (cs->number && cs->name && 
00121           (cs->sort_order || (cs->state & MY_CS_BINSORT))));
00122 }
00123 
00124 static int add_collation(CHARSET_INFO *cs)
00125 {
00126   if (cs->name && (cs->number || (cs->number=get_charset_number(cs->name))))
00127   {
00128     if (!(all_charsets[cs->number].state & MY_CS_COMPILED))
00129     {
00130       simple_cs_copy_data(&all_charsets[cs->number],cs);
00131       
00132     }
00133     
00134     cs->number= 0;
00135     cs->name= NULL;
00136     cs->state= 0;
00137     cs->sort_order= NULL;
00138     cs->state= 0;
00139   }
00140   return MY_XML_OK;
00141 }
00142 
00143 
00144 static int my_read_charset_file(const char *filename)
00145 {
00146   char buf[MAX_BUF];
00147   int  fd;
00148   uint len;
00149   
00150   if ((fd=open(filename,O_RDONLY)) < 0)
00151   {
00152     fprintf(stderr,"Can't open '%s'\n",filename);
00153     return 1;
00154   }
00155   
00156   len=read(fd,buf,MAX_BUF);
00157   close(fd);
00158   
00159   if (my_parse_charset_xml(buf,len,add_collation))
00160   {
00161 #if 0
00162     printf("ERROR at line %d pos %d '%s'\n",
00163            my_xml_error_lineno(&p)+1,
00164            my_xml_error_pos(&p),
00165            my_xml_error_string(&p));
00166 #endif
00167   }
00168   
00169   return FALSE;
00170 }
00171 
00172 void dispcset(FILE *f,CHARSET_INFO *cs)
00173 {
00174   fprintf(f,"{\n");
00175   fprintf(f,"  %d,%d,%d,\n",cs->number,0,0);
00176   fprintf(f,"  MY_CS_COMPILED%s%s,\n",
00177           cs->state & MY_CS_BINSORT ? "|MY_CS_BINSORT" : "",
00178           cs->state & MY_CS_PRIMARY ? "|MY_CS_PRIMARY" : "");
00179   
00180   if (cs->name)
00181   {
00182     fprintf(f,"  \"%s\",\n",cs->csname);
00183     fprintf(f,"  \"%s\",\n",cs->name);
00184     fprintf(f,"  \"\",\n");
00185     fprintf(f,"  ctype_%s,\n",cs->name);
00186     fprintf(f,"  to_lower_%s,\n",cs->name);
00187     fprintf(f,"  to_upper_%s,\n",cs->name);
00188     if (cs->sort_order)
00189       fprintf(f,"  sort_order_%s,\n",cs->name);
00190     else
00191       fprintf(f,"  NULL,\n");
00192     fprintf(f,"  to_uni_%s,\n",cs->name);
00193     fprintf(f,"  from_uni_%s,\n",cs->name);
00194   }
00195   else
00196   {
00197     fprintf(f,"  NULL,\n");
00198     fprintf(f,"  NULL,\n");
00199     fprintf(f,"  NULL,\n");
00200     fprintf(f,"  NULL,\n");
00201     fprintf(f,"  NULL,\n");
00202     fprintf(f,"  NULL,\n");
00203     fprintf(f,"  NULL,\n");
00204     fprintf(f,"  NULL,\n");
00205     fprintf(f,"  NULL,\n");
00206   }
00207   
00208   fprintf(f,"  \"\",\n");
00209   fprintf(f,"  \"\",\n");
00210   fprintf(f,"  0,\n");
00211   fprintf(f,"  0,\n");
00212   fprintf(f,"  0,\n");
00213   fprintf(f,"  &my_charset_8bit_handler,\n");
00214   if (cs->state & MY_CS_BINSORT)
00215     fprintf(f,"  &my_collation_bin_handler,\n");
00216   else
00217     fprintf(f,"  &my_collation_8bit_simple_ci_handler,\n");
00218   fprintf(f,"}\n");
00219 }
00220 
00221 
00222 int
00223 main(int argc, char **argv  __attribute__((unused)))
00224 {
00225   CHARSET_INFO  ncs;
00226   CHARSET_INFO  *cs;
00227   char filename[256];
00228   FILE *f= stdout;
00229   
00230   if (argc < 2)
00231   {
00232     fprintf(stderr, "usage: %s source-dir\n", argv[0]);
00233     exit(EXIT_FAILURE);
00234   }
00235   
00236   bzero((void*)&ncs,sizeof(ncs));
00237   bzero((void*)&all_charsets,sizeof(all_charsets));
00238   
00239   sprintf(filename,"%s/%s",argv[1],"Index.xml");
00240   my_read_charset_file(filename);
00241   
00242   for (cs=all_charsets; cs < all_charsets+256; cs++)
00243   {
00244     if (cs->number && !(cs->state & MY_CS_COMPILED))
00245     {
00246       if ( (!simple_cs_is_full(cs)) && (cs->csname))
00247       {
00248         sprintf(filename,"%s/%s.xml",argv[1],cs->csname);
00249         my_read_charset_file(filename);
00250       }
00251     }
00252   }
00253   
00254   for (cs=all_charsets; cs < all_charsets+256; cs++)
00255   {
00256     if (simple_cs_is_full(cs))
00257     {
00258       fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
00259       print_array(f, cs->name, "ctype",      cs->ctype,      MY_CS_CTYPE_TABLE_SIZE);
00260       print_array(f, cs->name, "to_lower",   cs->to_lower,   MY_CS_TO_LOWER_TABLE_SIZE);
00261       print_array(f, cs->name, "to_upper",   cs->to_upper,   MY_CS_TO_UPPER_TABLE_SIZE);
00262       if (cs->sort_order)
00263         print_array(f, cs->name, "sort_order", cs->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
00264       print_array16(f, cs->name, "to_uni",     cs->tab_to_uni, MY_CS_TO_UNI_TABLE_SIZE);
00265       fprintf(f,"#endif\n");
00266       fprintf(f,"\n");
00267     }
00268   }
00269   
00270   fprintf(f,"CHARSET_INFO compiled_charsets[] = {\n");
00271   for (cs=all_charsets; cs < all_charsets+256; cs++)
00272   {
00273     if (simple_cs_is_full(cs))
00274     {
00275       fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
00276       dispcset(f,cs);
00277       fprintf(f,",\n");
00278       fprintf(f,"#endif\n");
00279     }
00280   }
00281   
00282   dispcset(f,&ncs);
00283   fprintf(f,"};\n");
00284   
00285   return 0;
00286 }

Generated on Wed Jul 20 21:04:21 2005 for MySQL 5.0.9 Beta by  doxygen 1.4.3