You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
10 KiB

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