1
/* Copyright (C) 2004 MySQL AB
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.
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.
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 */
17
Written by Magnus Svensson
21
Converts a SQL file into a C file that can be compiled and linked
30
/* Compiler-dependent constant for maximum string constant */
31
#define MAX_STRING_CONSTANT_LENGTH 65535
35
static void die(const char *fmt, ...)
39
/* Print the error message */
40
fprintf(stderr, "FATAL ERROR: ");
44
vfprintf(stderr, fmt, args);
48
fprintf(stderr, "unknown error");
49
fprintf(stderr, "\n");
52
/* Close any open files */
62
int main(int argc, char *argv[])
66
char* struct_name= argv[1];
67
char* infile_name= argv[2];
68
char* outfile_name= argv[3];
72
die("Usage: comp_sql <struct_name> <sql_filename> <c_filename>");
74
/* Open input and output file */
75
if (!(in= fopen(infile_name, "r")))
76
die("Failed to open SQL file '%s'", infile_name);
79
if (!(out= fopen(outfile_name, "w")))
80
die("Failed to open output file '%s'", outfile_name);
81
fprintf(out, "const char %s[]={\n",struct_name);
84
Some compilers have limitations how long a string constant can be.
85
We'll output very long strings as hexadecimal arrays, and short ones
88
stat(infile_name, &st);
89
if (st.st_size > MAX_STRING_CONSTANT_LENGTH)
102
/* Put line break after each 16 hex characters */
103
if(cnt && (cnt%16 == 0))
106
fprintf(out,"0x%02x",c);
108
fprintf(out,",0x00");
113
while (fgets(buff, sizeof(buff), in))
121
Reached end of line, add escaped newline, escaped
122
backslash and a newline to outfile
124
fprintf(out, "\\n \"\n\"");
127
else if (*curr == '\r')
143
if (*(curr-1) != '\n')
146
Some compilers have a max string length,
147
insert a newline at every 512th char in long
150
fprintf(out, "\"\n\"");
153
fprintf(out, "\\\n\"");
156
fprintf(out, "};\n");