1
by brian
clean slate |
1 |
/* Copyright (C) 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; 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
Written by Magnus Svensson
|
|
18 |
*/
|
|
19 |
||
20 |
/*
|
|
21 |
Converts a SQL file into a C file that can be compiled and linked
|
|
22 |
into other programs
|
|
23 |
*/
|
|
24 |
||
25 |
#include <stdarg.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <stdio.h> |
|
28 |
||
29 |
FILE *in, *out; |
|
30 |
||
31 |
static void die(const char *fmt, ...) |
|
32 |
{
|
|
33 |
va_list args; |
|
34 |
||
35 |
/* Print the error message */
|
|
36 |
fprintf(stderr, "FATAL ERROR: "); |
|
37 |
if (fmt) |
|
38 |
{
|
|
39 |
va_start(args, fmt); |
|
40 |
vfprintf(stderr, fmt, args); |
|
41 |
va_end(args); |
|
42 |
}
|
|
43 |
else
|
|
44 |
fprintf(stderr, "unknown error"); |
|
45 |
fprintf(stderr, "\n"); |
|
46 |
fflush(stderr); |
|
47 |
||
48 |
/* Close any open files */
|
|
49 |
if (in) |
|
50 |
fclose(in); |
|
51 |
if (out) |
|
52 |
fclose(out); |
|
53 |
||
54 |
exit(1); |
|
55 |
}
|
|
56 |
||
57 |
||
58 |
int main(int argc, char *argv[]) |
|
59 |
{
|
|
60 |
char buff[512]; |
|
61 |
char* struct_name= argv[1]; |
|
62 |
char* infile_name= argv[2]; |
|
63 |
char* outfile_name= argv[3]; |
|
64 |
||
65 |
if (argc != 4) |
|
66 |
die("Usage: comp_sql <struct_name> <sql_filename> <c_filename>"); |
|
67 |
||
68 |
/* Open input and output file */
|
|
69 |
if (!(in= fopen(infile_name, "r"))) |
|
70 |
die("Failed to open SQL file '%s'", infile_name); |
|
71 |
if (!(out= fopen(outfile_name, "w"))) |
|
72 |
die("Failed to open output file '%s'", outfile_name); |
|
73 |
||
74 |
fprintf(out, "const char* %s={\n\"", struct_name); |
|
75 |
||
76 |
while (fgets(buff, sizeof(buff), in)) |
|
77 |
{
|
|
78 |
char *curr= buff; |
|
79 |
while (*curr) |
|
80 |
{
|
|
81 |
if (*curr == '\n') |
|
82 |
{
|
|
83 |
/*
|
|
84 |
Reached end of line, add escaped newline, escaped
|
|
85 |
backslash and a newline to outfile
|
|
86 |
*/
|
|
87 |
fprintf(out, "\\n \"\n\""); |
|
88 |
curr++; |
|
89 |
}
|
|
90 |
else if (*curr == '\r') |
|
91 |
{
|
|
92 |
curr++; /* Skip */ |
|
93 |
}
|
|
94 |
else
|
|
95 |
{
|
|
96 |
if (*curr == '"') |
|
97 |
{
|
|
98 |
/* Needs escape */
|
|
99 |
fputc('\\', out); |
|
100 |
}
|
|
101 |
||
102 |
fputc(*curr, out); |
|
103 |
curr++; |
|
104 |
}
|
|
105 |
}
|
|
106 |
if (*(curr-1) != '\n') |
|
107 |
{
|
|
108 |
/*
|
|
109 |
Some compilers have a max string length,
|
|
110 |
insert a newline at every 512th char in long
|
|
111 |
strings
|
|
112 |
*/
|
|
113 |
fprintf(out, "\"\n\""); |
|
114 |
}
|
|
115 |
}
|
|
116 |
||
117 |
fprintf(out, "\\\n\"};\n"); |
|
118 |
||
119 |
fclose(in); |
|
120 |
fclose(out); |
|
121 |
||
122 |
exit(0); |
|
123 |
||
124 |
}
|
|
125 |