MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conf_to_src.c
1 /* Copyright (C) 2000-2004 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.
6 
7  There are special exceptions to the terms and conditions of the GPL as it
8  is applied to this software.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
18 
19 /* can't use -lmysys because this prog is used to create -lstrings */
20 
21 
22 #include <my_global.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #define CHARSETS_SUBDIR "sql/share/charsets"
28 #define CTYPE_TABLE_SIZE 257
29 #define TO_LOWER_TABLE_SIZE 256
30 #define TO_UPPER_TABLE_SIZE 256
31 #define SORT_ORDER_TABLE_SIZE 256
32 #define ROW_LEN 16
33 
34 void print_arrays_for(char *set);
35 
36 char *prog;
37 char buf[1024], *p, *endptr;
38 
39 int
40 main(int argc, char **argv)
41 {
42  prog = *argv;
43 
44  if (argc < 2) {
45  fprintf(stderr, "usage: %s source-dir [charset [, charset]]\n", prog);
46  exit(EXIT_FAILURE);
47  }
48 
49  --argc; ++argv; /* skip program name */
50 
51  if (chdir(*argv) != 0) {
52  fprintf(stderr, "%s: can't cd to %s\n", prog, *argv);
53  exit(EXIT_FAILURE);
54  }
55  --argc; ++argv;
56 
57  if (chdir(CHARSETS_SUBDIR) != 0) {
58  fprintf(stderr, "%s: can't cd to %s\n", prog, CHARSETS_SUBDIR);
59  exit(EXIT_FAILURE);
60  }
61 
62  while (argc--)
63  print_arrays_for(*argv++);
64 
65  exit(EXIT_SUCCESS);
66 }
67 
68 void
69 print_array(FILE *f, const char *set, const char *name, int n)
70 {
71  int i;
72  char val[100];
73 
74  printf("uchar %s_%s[] = {\n", name, set);
75 
76  p = buf;
77  *buf = '\0';
78  for (i = 0; i < n; ++i)
79  {
80  /* get a word from f */
81  endptr = p;
82  for (;;)
83  {
84  while (isspace(*endptr))
85  ++endptr;
86  if (*endptr && *endptr != '#') /* not comment */
87  break;
88  if ((fgets(buf, sizeof(buf), f)) == NULL)
89  return; /* XXX: break silently */
90  endptr = buf;
91  }
92 
93  p = val;
94  while (!isspace(*endptr))
95  *p++ = *endptr++;
96  *p = '\0';
97  p = endptr;
98 
99  /* write the value out */
100 
101  if (i == 0 || i % ROW_LEN == n % ROW_LEN)
102  printf(" ");
103 
104  printf("%3d", (unsigned char) strtol(val, (char **) NULL, 16));
105 
106  if (i < n - 1)
107  printf(",");
108 
109  if ((i+1) % ROW_LEN == n % ROW_LEN)
110  printf("\n");
111  }
112 
113  printf("};\n\n");
114 }
115 
116 void
117 print_arrays_for(char *set)
118 {
119  FILE *f;
120 
121  sprintf(buf, "%s.conf", set);
122 
123  if ((f = fopen(buf, "r")) == NULL) {
124  fprintf(stderr, "%s: can't read conf file for charset %s\n", prog, set);
125  exit(EXIT_FAILURE);
126  }
127 
128  printf("\
129 /* The %s character set. Generated automatically by configure and\n\
130  * the %s program\n\
131  */\n\n",
132  set, prog);
133 
134  /* it would be nice if this used the code in mysys/charset.c, but... */
135  print_array(f, set, "ctype", CTYPE_TABLE_SIZE);
136  print_array(f, set, "to_lower", TO_LOWER_TABLE_SIZE);
137  print_array(f, set, "to_upper", TO_UPPER_TABLE_SIZE);
138  print_array(f, set, "sort_order", SORT_ORDER_TABLE_SIZE);
139  printf("\n");
140 
141  fclose(f);
142 
143  return;
144 }