summaryrefslogtreecommitdiff
path: root/strings/conf_to_src.c
diff options
context:
space:
mode:
Diffstat (limited to 'strings/conf_to_src.c')
-rw-r--r--strings/conf_to_src.c342
1 files changed, 253 insertions, 89 deletions
diff --git a/strings/conf_to_src.c b/strings/conf_to_src.c
index 2f88c9ad22e..69dccbca0a7 100644
--- a/strings/conf_to_src.c
+++ b/strings/conf_to_src.c
@@ -14,129 +14,293 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
-/* can't use -lmysys because this prog is used to create -lstrings */
-
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <fcntl.h>
-#define CHARSETS_SUBDIR "sql/share/charsets"
-#define CTYPE_TABLE_SIZE 257
-#define TO_LOWER_TABLE_SIZE 256
-#define TO_UPPER_TABLE_SIZE 256
-#define SORT_ORDER_TABLE_SIZE 256
-#define ROW_LEN 16
+#include <my_global.h>
+#include <m_ctype.h>
+#include <my_xml.h>
-void print_arrays_for(char *set);
-char *prog;
-char buf[1024], *p, *endptr;
+#define ROW_LEN 16
+#define ROW16_LEN 8
+#define MAX_BUF 16*1024
-int
-main(int argc, char **argv)
+static CHARSET_INFO all_charsets[256];
+
+
+void
+print_array(FILE *f, const char *set, const char *name, uchar *a, int n)
{
- prog = *argv;
+ int i;
- if (argc < 2) {
- fprintf(stderr, "usage: %s source-dir [charset [, charset]]\n", prog);
- exit(EXIT_FAILURE);
+ fprintf(f,"uchar %s_%s[] = {\n", name, set);
+
+ for (i=0 ;i<n ; i++)
+ {
+ fprintf(f,"0x%02X",a[i]);
+ fprintf(f, (i+1<n) ? "," :"" );
+ fprintf(f, ((i+1) % ROW_LEN == n % ROW_LEN) ? "\n" : "" );
}
+ fprintf(f,"};\n\n");
+}
- --argc; ++argv; /* skip program name */
- if (chdir(*argv) != 0) {
- fprintf(stderr, "%s: can't cd to %s\n", prog, *argv);
- exit(EXIT_FAILURE);
- }
- --argc; ++argv;
+void
+print_array16(FILE *f, const char *set, const char *name, uint16 *a, int n)
+{
+ int i;
- if (chdir(CHARSETS_SUBDIR) != 0) {
- fprintf(stderr, "%s: can't cd to %s\n", prog, CHARSETS_SUBDIR);
- exit(EXIT_FAILURE);
+ fprintf(f,"uchar %s_%s[] = {\n", name, set);
+
+ for (i=0 ;i<n ; i++)
+ {
+ fprintf(f,"0x%04X",a[i]);
+ fprintf(f, (i+1<n) ? "," :"" );
+ fprintf(f, ((i+1) % ROW16_LEN == n % ROW16_LEN) ? "\n" : "" );
}
+ fprintf(f,"};\n\n");
+}
- while (argc--)
- print_arrays_for(*argv++);
- exit(EXIT_SUCCESS);
+static int get_charset_number(const char *charset_name)
+{
+ CHARSET_INFO *cs;
+ for (cs= all_charsets; cs < all_charsets+255; ++cs)
+ {
+ if ( cs->name && !strcmp(cs->name, charset_name))
+ return cs->number;
+ }
+ return 0;
}
-void
-print_array(FILE *f, const char *set, const char *name, int n)
+char *mdup(const char *src, uint len)
{
- int i;
- char val[100];
+ char *dst=(char*)malloc(len);
+ memcpy(dst,src,len);
+ return dst;
+}
- printf("uchar %s_%s[] = {\n", name, set);
+static void simple_cs_copy_data(CHARSET_INFO *to, CHARSET_INFO *from)
+{
+ to->number= from->number ? from->number : to->number;
+ to->state|= from->state;
+
+ if (from->csname)
+ to->csname= strdup(from->csname);
+
+ if (from->name)
+ to->name= strdup(from->name);
+
+ if (from->ctype)
+ to->ctype= (uchar*) mdup((char*) from->ctype, MY_CS_CTYPE_TABLE_SIZE);
+ if (from->to_lower)
+ to->to_lower= (uchar*) mdup((char*) from->to_lower, MY_CS_TO_LOWER_TABLE_SIZE);
+ if (from->to_upper)
+ to->to_upper= (uchar*) mdup((char*) from->to_upper, MY_CS_TO_UPPER_TABLE_SIZE);
+ if (from->sort_order)
+ {
+ to->sort_order= (uchar*) mdup((char*) from->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
+ /*
+ set_max_sort_char(to);
+ */
+ }
+ if (from->tab_to_uni)
+ {
+ uint sz= MY_CS_TO_UNI_TABLE_SIZE*sizeof(uint16);
+ to->tab_to_uni= (uint16*) mdup((char*)from->tab_to_uni, sz);
+ /*
+ create_fromuni(to);
+ */
+ }
+}
+
+static my_bool simple_cs_is_full(CHARSET_INFO *cs)
+{
+ return ((cs->csname && cs->tab_to_uni && cs->ctype && cs->to_upper &&
+ cs->to_lower) &&
+ (cs->number && cs->name && cs->sort_order));
+}
- p = buf;
- *buf = '\0';
- for (i = 0; i < n; ++i)
+static int add_collation(CHARSET_INFO *cs)
+{
+ if (cs->name && (cs->number || (cs->number=get_charset_number(cs->name))))
{
- /* get a word from f */
- endptr = p;
- for (;;)
+ if (!(all_charsets[cs->number].state & MY_CS_COMPILED))
{
- while (isspace((* (unsigned char*) endptr)))
- ++endptr;
- if (*endptr && *endptr != '#') /* not comment */
- break;
- if ((fgets(buf, sizeof(buf), f)) == NULL)
- return; /* XXX: break silently */
- endptr = buf;
+ simple_cs_copy_data(&all_charsets[cs->number],cs);
+
}
+
+ cs->number= 0;
+ cs->name= NULL;
+ cs->state= 0;
+ cs->sort_order= NULL;
+ cs->state= 0;
+ }
+ return MY_XML_OK;
+}
- p = val;
- while (!isspace((* (unsigned char*) endptr)))
- *p++ = *endptr++;
- *p = '\0';
- p = endptr;
-
- /* write the value out */
-
- if (i == 0 || i % ROW_LEN == n % ROW_LEN)
- printf(" ");
-
- printf("%3d", (unsigned char) strtol(val, (char **) NULL, 16));
-
- if (i < n - 1)
- printf(",");
- if ((i+1) % ROW_LEN == n % ROW_LEN)
- printf("\n");
+static int my_read_charset_file(const char *filename)
+{
+ char buf[MAX_BUF];
+ int fd;
+ uint len;
+
+ if ((fd=open(filename,O_RDONLY)) < 0)
+ {
+ fprintf(stderr,"Can't open '%s'\n",filename);
+ return 1;
}
-
- printf("};\n\n");
+
+ len=read(fd,buf,MAX_BUF);
+ close(fd);
+
+ if (my_parse_charset_xml(buf,len,add_collation))
+ {
+#if 0
+ printf("ERROR at line %d pos %d '%s'\n",
+ my_xml_error_lineno(&p)+1,
+ my_xml_error_pos(&p),
+ my_xml_error_string(&p));
+#endif
+ }
+
+ return FALSE;
}
-void
-print_arrays_for(char *set)
+void dispcset(FILE *f,CHARSET_INFO *cs)
{
- FILE *f;
+ fprintf(f,"{\n");
+ fprintf(f," %d,\n",cs->number);
+ fprintf(f," MY_CS_COMPILED,\n");
+
+ if (cs->name)
+ {
+ fprintf(f," \"%s\",\n",cs->name);
+ fprintf(f," \"%s\",\n",cs->csname);
+ fprintf(f," \"\",\n");
+ fprintf(f," ctype_%s,\n",cs->name);
+ fprintf(f," to_lower_%s,\n",cs->name);
+ fprintf(f," to_upper_%s,\n",cs->name);
+ fprintf(f," sort_order_%s,\n",cs->name);
+ fprintf(f," to_uni_%s,\n",cs->name);
+ fprintf(f," from_uni_%s,\n",cs->name);
+ }
+ else
+ {
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ }
+
+ fprintf(f," %d,\n",cs->strxfrm_multiply);
+ fprintf(f," my_strnncoll_simple,\n");
+ fprintf(f," my_strnxfrm_simple,\n");
+ fprintf(f," my_like_range_simple,\n");
+ fprintf(f," my_wild_cmp_8bit,\n");
+ fprintf(f," %d,\n",cs->mbmaxlen);
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," NULL,\n");
+ fprintf(f," my_mb_wc_8bit,\n");
+ fprintf(f," my_wc_mb_8bit,\n");
+ fprintf(f," my_caseup_str_8bit,\n");
+ fprintf(f," my_casedn_str_8bit,\n");
+ fprintf(f," my_caseup_8bit,\n");
+ fprintf(f," my_casedn_8bit,\n");
+ fprintf(f," my_tosort_8bit,\n");
+ fprintf(f," my_strcasecmp_8bit,\n");
+ fprintf(f," my_strncasecmp_8bit,\n");
+ fprintf(f," my_hash_caseup_simple,\n");
+ fprintf(f," my_hash_sort_simple,\n");
+ fprintf(f," 0,\n");
+ fprintf(f," my_snprintf_8bit,\n");
+ fprintf(f," my_long10_to_str_8bit,\n");
+ fprintf(f," my_longlong10_to_str_8bit,\n");
+ fprintf(f," my_fill_8bit,\n");
+ fprintf(f," my_strntol_8bit,\n");
+ fprintf(f," my_strntoul_8bit,\n");
+ fprintf(f," my_strntoll_8bit,\n");
+ fprintf(f," my_strntoull_8bit,\n");
+ fprintf(f," my_strntod_8bit,\n");
+ fprintf(f," my_scan_8bit\n");
+ fprintf(f,"}\n");
+}
- sprintf(buf, "%s.conf", set);
- if ((f = fopen(buf, "r")) == NULL) {
- fprintf(stderr, "%s: can't read conf file for charset %s\n", prog, set);
+int
+main(int argc, char **argv __attribute__((unused)))
+{
+ CHARSET_INFO ncs;
+ CHARSET_INFO *cs;
+ char filename[256];
+ FILE *f= stdout;
+
+ if (argc < 2)
+ {
+ fprintf(stderr, "usage: %s source-dir\n", argv[0]);
exit(EXIT_FAILURE);
}
-
- printf("\
-/* The %s character set. Generated automatically by\n\
- * the %s program\n\
- */\n\n",
- set, prog);
-
- /* it would be nice if this used the code in mysys/charset.c, but... */
- print_array(f, set, "ctype", CTYPE_TABLE_SIZE);
- print_array(f, set, "to_lower", TO_LOWER_TABLE_SIZE);
- print_array(f, set, "to_upper", TO_UPPER_TABLE_SIZE);
- print_array(f, set, "sort_order", SORT_ORDER_TABLE_SIZE);
- printf("\n");
-
- fclose(f);
-
- return;
+
+ bzero((void*)&ncs,sizeof(ncs));
+ bzero((void*)&all_charsets,sizeof(all_charsets));
+
+ sprintf(filename,"%s/%s",argv[1],"Index.xml");
+ my_read_charset_file(filename);
+
+ for (cs=all_charsets; cs < all_charsets+256; cs++)
+ {
+ if (cs->number)
+ {
+ if ( (!simple_cs_is_full(cs)) && (cs->csname) )
+ {
+ sprintf(filename,"%s/%s.xml",argv[1],cs->csname);
+ my_read_charset_file(filename);
+ }
+ }
+ }
+
+ for (cs=all_charsets; cs < all_charsets+256; cs++)
+ {
+ if (simple_cs_is_full(cs))
+ {
+ fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
+ print_array(f, cs->name, "ctype", cs->ctype, MY_CS_CTYPE_TABLE_SIZE);
+ print_array(f, cs->name, "to_lower", cs->to_lower, MY_CS_TO_LOWER_TABLE_SIZE);
+ print_array(f, cs->name, "to_upper", cs->to_upper, MY_CS_TO_UPPER_TABLE_SIZE);
+ print_array(f, cs->name, "sort_order", cs->sort_order, MY_CS_SORT_ORDER_TABLE_SIZE);
+ print_array16(f, cs->name, "to_uni", cs->tab_to_uni, MY_CS_TO_UNI_TABLE_SIZE);
+ fprintf(f,"#endif\n");
+ fprintf(f,"\n");
+ }
+ }
+
+ fprintf(f,"CHARSET_INFO compiled_charsets[] = {\n");
+ for (cs=all_charsets; cs < all_charsets+256; cs++)
+ {
+ if (simple_cs_is_full(cs))
+ {
+ fprintf(f,"#ifdef HAVE_CHARSET_%s\n",cs->csname);
+ dispcset(f,cs);
+ fprintf(f,",\n");
+ fprintf(f,"#endif\n");
+ }
+ }
+
+ dispcset(f,&ncs);
+ fprintf(f,"};\n");
+
+ return 0;
}