25 #include "../sql/sql_bootstrap.h"
35 #include "../sql/sql_bootstrap.cc"
40 static void die(
const char *
fmt, ...)
45 fprintf(stderr,
"FATAL ERROR: ");
49 vfprintf(stderr, fmt, args);
53 fprintf(stderr,
"unknown error");
54 fprintf(stderr,
"\n");
66 char *fgets_fn(
char *buffer,
size_t size, fgets_input_t input,
int *error)
68 char *line= fgets(buffer, size, (FILE*) input);
70 *error= (line == NULL) ? ferror((FILE*)input) : 0;
74 static void print_query(FILE *out,
const char *
query)
76 const char *ptr= query;
85 fprintf(out,
"\"\n \"");
95 fprintf(out,
"\\n\"\n \"");
102 fprintf(out,
"\\\"");
112 fprintf(out,
"\\n\",\n");
115 int main(
int argc,
char *argv[])
117 char query[MAX_BOOTSTRAP_QUERY_SIZE];
118 char* struct_name= argv[1];
119 char* infile_name= argv[2];
120 char* outfile_name= argv[3];
127 die(
"Usage: comp_sql <struct_name> <sql_filename> <c_filename>");
130 if (!(in= fopen(infile_name,
"r")))
131 die(
"Failed to open SQL file '%s'", infile_name);
132 if (!(out= fopen(outfile_name,
"w")))
133 die(
"Failed to open output file '%s'", outfile_name);
135 fprintf(out,
"/*\n");
136 fprintf(out,
" Do not edit this file, it is automatically generated from:\n");
137 fprintf(out,
" <%s>\n", infile_name);
138 fprintf(out,
"*/\n");
139 fprintf(out,
"const char* %s[]={\n", struct_name);
143 rc= read_bootstrap_query(query, &query_length,
144 (fgets_input_t) in, fgets_fn, &error);
146 if (rc == READ_BOOTSTRAP_EOF)
149 if (rc != READ_BOOTSTRAP_SUCCESS)
152 err_ptr= query + (query_length <= MAX_BOOTSTRAP_ERROR_LEN ?
153 0 : (query_length - MAX_BOOTSTRAP_ERROR_LEN));
156 case READ_BOOTSTRAP_ERROR:
157 die(
"Failed to read the bootstrap input file. Return code (%d).\n"
158 "Last query: '%s'\n", error, err_ptr);
161 case READ_BOOTSTRAP_QUERY_SIZE:
162 die(
"Failed to read the boostrap input file. Query size exceeded %d bytes.\n"
163 "Last query: '%s'.\n", MAX_BOOTSTRAP_LINE_SIZE, err_ptr);
167 die(
"Failed to read the boostrap input file. Unknown error.\n");
172 print_query(out, query);
175 fprintf(out,
"NULL\n};\n");