MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndb_dump_frm_data.cpp
1 /* Copyright (C) 2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #include <ndb_global.h>
17 #include <ndb_opts.h>
18 
19 #include <NdbOut.hpp>
20 #include <NdbApi.hpp>
21 #include <NDBT.hpp>
22 
23 static struct my_option
24 my_long_options[] =
25 {
26  { "help", '?',
27  "Display this help and exit.",
28  0, 0, 0,
29  GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
30  { 0, 0,
31  0,
32  0, 0, 0,
33  GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }
34 };
35 
36 const char*
37 load_default_groups[]= { 0 };
38 
39 static void
40 short_usage_sub(void)
41 {
42  ndb_short_usage_sub("*.frm ...");
43 }
44 
45 static void
46 usage()
47 {
48  printf("%s: pack and dump *.frm as C arrays\n", my_progname);
49  ndb_usage(short_usage_sub, load_default_groups, my_long_options);
50 }
51 
52 static void
53 dodump(const char* name, const uchar* frm_data, uint frm_len)
54 {
55  printf("const uint g_%s_frm_len = %u;\n\n", name, frm_len);
56  printf("const uint8 g_%s_frm_data[%u] =\n{\n", name, frm_len);
57  uint n = 0;
58  while (n < frm_len) {
59  if (n % 8 == 0) {
60  if (n != 0) {
61  printf("\n");
62  }
63  printf(" ");
64  }
65  printf("0x%02x", frm_data[n]);
66  if (n + 1 < frm_len) {
67  printf(",");
68  }
69  n++;
70  }
71  if (n % 8 != 0) {
72  printf("\n");
73  }
74  printf("};\n");
75 }
76 
77 static int
78 dofile(const char* file)
79 {
80  struct stat st;
81  size_t size = 0;
82  uchar* data = 0;
83  int fd = -1;
84  uchar* pack_data = 0;
85  size_t pack_len = 0;
86  char* namebuf = 0;
87  int ret = -1;
88  do
89  {
90  if (stat(file, &st) == -1)
91  {
92  fprintf(stderr, "%s: stat: %s\n", file, strerror(errno));
93  break;
94  }
95  size = st.st_size;
96  if ((data = (uchar*)malloc(size)) == 0)
97  {
98  fprintf(stderr, "%s: malloc %u: %s\n", file, (uint)size, strerror(errno));
99  break;
100  }
101  if ((fd = open(file, O_RDONLY)) == -1)
102  {
103  fprintf(stderr, "%s: open: %s\n", file, strerror(errno));
104  break;
105  }
106  ssize_t size2;
107  if ((size2 = read(fd, data, size)) == -1)
108  {
109  fprintf(stderr, "%s: read: %s\n", file, strerror(errno));
110  break;
111  }
112  if ((size_t)size2 != size)
113  {
114  fprintf(stderr, "%s: short read: %u != %u\n", file, (uint)size2, (uint)size);
115  break;
116  }
117  int error;
118  if ((error = packfrm(data, size, &pack_data, &pack_len)) != 0)
119  {
120  fprintf(stderr, "%s: packfrm: error %d\n", file, error);
121  break;
122  }
123  namebuf = strdup(file);
124  if (namebuf == 0)
125  {
126  fprintf(stderr, "%s: strdup: %s\n", file, strerror(errno));
127  break;
128  }
129  char* name = namebuf;
130  if (strchr(name, '/') != 0)
131  name = strrchr(name, '/') + 1;
132  char* dot;
133  if ((dot = strchr(name, '.')) != 0)
134  *dot = 0;
135  printf("\n/*\n");
136  printf(" name: %s\n", name);
137  printf(" orig: %u\n", (uint)size);
138  printf(" pack: %u\n", (uint)pack_len);
139  printf("*/\n\n");
140  dodump(name, pack_data, pack_len);
141  ret = 0;
142  }
143  while (0);
144  if (namebuf != 0)
145  free(namebuf);
146  if (pack_data != 0)
147  my_free(pack_data); // Free data returned from packfrm with my_free
148  if (fd != -1)
149  (void)close(fd);
150  if (data != 0)
151  free(data);
152  return ret;
153 }
154 
155 int
156 main(int argc, char** argv)
157 {
158  my_progname = "ndb_pack_frm";
159  int ret;
160 
161  ndb_init();
162  ndb_opt_set_usage_funcs(short_usage_sub, usage);
163  ret = handle_options(&argc, &argv, my_long_options, ndb_std_get_one_option);
164  if (ret != 0)
165  return NDBT_WRONGARGS;
166 
167  for (int i = 0; i < argc; i++)
168  {
169  ret = dofile(argv[i]);
170  if (ret != 0)
171  return NDBT_FAILED;
172  }
173 
174  return NDBT_OK;
175 }