1 | /*
|
---|
2 | * OSCam WebIf pages generator
|
---|
3 | * Copyright (C) 2013 Unix Solutions Ltd.
|
---|
4 | *
|
---|
5 | * Authors: Georgi Chorbadzhiyski (gf@unixsol.org)
|
---|
6 | *
|
---|
7 | * This program is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #include <ctype.h>
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <stdarg.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <stdbool.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <errno.h>
|
---|
27 | #include <sys/types.h>
|
---|
28 | #include <sys/stat.h>
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <fcntl.h>
|
---|
31 | #include <inttypes.h>
|
---|
32 | #include <libgen.h>
|
---|
33 |
|
---|
34 | #define USE_COMPRESSION 1
|
---|
35 |
|
---|
36 | #include "../minilzo/minilzo.h"
|
---|
37 |
|
---|
38 | #define MAX_TEMPLATES 256
|
---|
39 | static char *index_filename = "pages_index.txt";
|
---|
40 | static char *output_pages_c = "pages.c";
|
---|
41 | static char *output_pages_h = "pages.h";
|
---|
42 |
|
---|
43 | struct template
|
---|
44 | {
|
---|
45 | char ident[64];
|
---|
46 | char file[128];
|
---|
47 | char deps[256];
|
---|
48 | uint32_t data_len;
|
---|
49 | enum { TXT, BIN } type;
|
---|
50 | uint8_t mime_type;
|
---|
51 | #ifdef USE_COMPRESSION
|
---|
52 | uint8_t *buf;
|
---|
53 | size_t buf_len;
|
---|
54 | uint32_t ident_ofs;
|
---|
55 | uint32_t data_ofs;
|
---|
56 | uint32_t deps_ofs;
|
---|
57 | #endif
|
---|
58 | };
|
---|
59 |
|
---|
60 | struct templates
|
---|
61 | {
|
---|
62 | unsigned int num;
|
---|
63 | struct template data[MAX_TEMPLATES];
|
---|
64 | };
|
---|
65 |
|
---|
66 | static struct templates templates;
|
---|
67 | static FILE *output_file;
|
---|
68 |
|
---|
69 | static void die(const char *s, ...)
|
---|
70 | {
|
---|
71 | va_list args;
|
---|
72 | va_start(args, s);
|
---|
73 | fprintf(stderr, "ERROR: ");
|
---|
74 | vfprintf(stderr, s, args);
|
---|
75 | if (s[strlen(s) - 1] != '\n')
|
---|
76 | fprintf(stderr, "\n");
|
---|
77 | va_end(args);
|
---|
78 | exit(EXIT_FAILURE);
|
---|
79 | }
|
---|
80 |
|
---|
81 | static FILE *xfopen(char *filename, char *mode)
|
---|
82 | {
|
---|
83 | FILE *fh = fopen(filename, mode);
|
---|
84 | if (!fh)
|
---|
85 | die("fopen(%s, %s): %s\n", filename, mode, strerror(errno));
|
---|
86 | return fh;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void readfile(const char *filename, uint8_t **data, size_t *data_len)
|
---|
90 | {
|
---|
91 | struct stat sb;
|
---|
92 | if (stat(filename, &sb) != 0)
|
---|
93 | die("stat(%s): %s\n", filename, strerror(errno));
|
---|
94 | int fd = open(filename, O_RDONLY);
|
---|
95 | if (fd < 0)
|
---|
96 | die("open(%s): %s\n", filename, strerror(errno));
|
---|
97 | *data_len = sb.st_size;
|
---|
98 | *data = malloc(*data_len);
|
---|
99 | if (!*data)
|
---|
100 | die("%s(%s): can't alloc %zd bytes\n", __func__, filename, *data_len);
|
---|
101 | if (read(fd, *data, *data_len) < 0)
|
---|
102 | die("read(%d, %zd): %s\n", fd, *data_len, strerror(errno));
|
---|
103 | }
|
---|
104 |
|
---|
105 | static bool is_text(char *filename)
|
---|
106 | {
|
---|
107 | char *ext = strchr(basename(filename), '.');
|
---|
108 | if (ext)
|
---|
109 | {
|
---|
110 | ext++;
|
---|
111 | if (strcmp(ext, "html") == 0) return true;
|
---|
112 | else if (strcmp(ext, "json") == 0) return true;
|
---|
113 | else if (strcmp(ext, "xml") == 0) return true;
|
---|
114 | else if (strcmp(ext, "css") == 0) return true;
|
---|
115 | else if (strcmp(ext, "svg") == 0) return true;
|
---|
116 | else if (strcmp(ext, "js") == 0) return true;
|
---|
117 | }
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | static uint8_t mime_type_from_filename(char *filename)
|
---|
122 | {
|
---|
123 | char *ext = strchr(basename(filename), '.');
|
---|
124 | if (ext)
|
---|
125 | {
|
---|
126 | ext++;
|
---|
127 | // See "enum template_types" bellow
|
---|
128 | if (strcmp(ext, "png") == 0) return 1;
|
---|
129 | else if (strcmp(ext, "gif") == 0) return 2;
|
---|
130 | else if (strcmp(ext, "ico") == 0) return 3;
|
---|
131 | else if (strcmp(ext, "jpg") == 0) return 4;
|
---|
132 | }
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void parse_index_file(char *filename)
|
---|
137 | {
|
---|
138 | FILE *f = xfopen(filename, "r");
|
---|
139 | int max_fields = 3;
|
---|
140 | char line[1024];
|
---|
141 | while (fgets(line, sizeof(line) - 1, f))
|
---|
142 | {
|
---|
143 | int field = 0, pos = 0;
|
---|
144 | char *ident = "", *file = "", *deps = "";
|
---|
145 | int len = strlen(line);
|
---|
146 | if (!len || !isalnum(line[0])) // Skip comments and junk
|
---|
147 | continue;
|
---|
148 | // Parse text[ ]text[ ]text
|
---|
149 | do
|
---|
150 | {
|
---|
151 | while (line[pos] == ' ' || line[pos] == '\t') // Skip white space
|
---|
152 | pos++;
|
---|
153 | if (line[pos] == '\n')
|
---|
154 | break;
|
---|
155 | int start = pos;
|
---|
156 | while (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\n') // Data
|
---|
157 | pos++;
|
---|
158 | switch (++field)
|
---|
159 | {
|
---|
160 | case 1: ident = line + start; line[pos] = '\0'; break;
|
---|
161 | case 2: file = line + start; line[pos] = '\0'; break;
|
---|
162 | case 3: deps = line + start; line[pos] = '\0'; break;
|
---|
163 | }
|
---|
164 | if (field >= max_fields)
|
---|
165 | break;
|
---|
166 | pos++;
|
---|
167 | }
|
---|
168 | while (pos < len);
|
---|
169 | if (!strlen(ident) || !strlen(file))
|
---|
170 | continue;
|
---|
171 |
|
---|
172 | #define template_set(var) \
|
---|
173 | do { \
|
---|
174 | len = strlen(var); \
|
---|
175 | pos = sizeof(templates.data[0].var); \
|
---|
176 | if (len > pos - 1) \
|
---|
177 | die("%s=%s length exceeds maxlen (%d > %d)\n", #var, var, len, pos - 1); \
|
---|
178 | snprintf(templates.data[templates.num].var, pos, "%s", var); \
|
---|
179 | } while (0)
|
---|
180 | template_set(ident);
|
---|
181 | template_set(file);
|
---|
182 | template_set(deps);
|
---|
183 |
|
---|
184 | templates.data[templates.num].type = is_text(file) ? TXT : BIN;
|
---|
185 | templates.data[templates.num].mime_type = mime_type_from_filename(file);
|
---|
186 | templates.num++;
|
---|
187 | if (templates.num == MAX_TEMPLATES - 1)
|
---|
188 | {
|
---|
189 | die("Too many templates in %s. Maximum is %d. Increase MAX_TEMPLATES!\n",
|
---|
190 | filename, MAX_TEMPLATES);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | fclose(f);
|
---|
194 | }
|
---|
195 |
|
---|
196 | static void print_template(int tpl_idx)
|
---|
197 | {
|
---|
198 | static bool ifdef_open = 0;
|
---|
199 | char *prev_deps = "";
|
---|
200 | char *next_deps = "";
|
---|
201 | char *ident = templates.data[tpl_idx].ident;
|
---|
202 | char *deps = templates.data[tpl_idx].deps;
|
---|
203 | if (tpl_idx > 0)
|
---|
204 | prev_deps = templates.data[tpl_idx - 1].deps;
|
---|
205 | if (tpl_idx + 1 < templates.num)
|
---|
206 | next_deps = templates.data[tpl_idx + 1].deps;
|
---|
207 | int deps_len = strlen(deps);
|
---|
208 |
|
---|
209 | // Put guards
|
---|
210 | if (deps_len && strcmp(deps, prev_deps) != 0)
|
---|
211 | {
|
---|
212 | int i, commas = 0;
|
---|
213 | for (i = 0; i < deps_len; i++)
|
---|
214 | {
|
---|
215 | if (deps[i] == ',')
|
---|
216 | commas++;
|
---|
217 | }
|
---|
218 | if (commas == 0)
|
---|
219 | {
|
---|
220 | fprintf(output_file, "#ifdef %s\n", deps);
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | char *ptr, *saveptr1 = NULL;
|
---|
225 | char *split_deps = strdup(deps);
|
---|
226 | for (i = 0, ptr = strtok_r(split_deps, ",", &saveptr1); ptr; ptr = strtok_r(NULL, ",", &saveptr1), i++)
|
---|
227 | {
|
---|
228 | if (i == 0)
|
---|
229 | fprintf(output_file, "#if defined(%s)", ptr);
|
---|
230 | else
|
---|
231 | fprintf(output_file, " || defined(%s)", ptr);
|
---|
232 | }
|
---|
233 | fprintf(output_file, "\n");
|
---|
234 | free(split_deps);
|
---|
235 | }
|
---|
236 | ifdef_open = 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | #ifdef USE_COMPRESSION
|
---|
240 | fprintf(output_file, "\t{ .tpl_name_ofs=%5u, .tpl_data_ofs=%5u, .tpl_deps_ofs=%5u, .tpl_data_len=%5u, .tpl_type=%u }, /* %s %s %s */\n",
|
---|
241 | templates.data[tpl_idx].ident_ofs,
|
---|
242 | templates.data[tpl_idx].data_ofs,
|
---|
243 | templates.data[tpl_idx].deps_ofs,
|
---|
244 | templates.data[tpl_idx].data_len,
|
---|
245 | templates.data[tpl_idx].mime_type,
|
---|
246 | ident,
|
---|
247 | templates.data[tpl_idx].file,
|
---|
248 | deps
|
---|
249 | );
|
---|
250 | #else
|
---|
251 | fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=TPL%s, .tpl_deps=\"%s\", .tpl_data_len=%u, .tpl_type=%u },\n",
|
---|
252 | ident, ident, deps, templates.data[tpl_idx].data_len, templates.data[tpl_idx].mime_type
|
---|
253 | );
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | if (ifdef_open && strcmp(deps, next_deps) != 0)
|
---|
257 | {
|
---|
258 | fprintf(output_file, "#endif\n");
|
---|
259 | ifdef_open = 0;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | #ifdef USE_COMPRESSION
|
---|
264 | static void dump_cbinary(char *var_name, uint8_t *buf, size_t buf_len, size_t obuf_len)
|
---|
265 | {
|
---|
266 | fprintf(output_file, "static const char *%s = \"", var_name);
|
---|
267 | int i;
|
---|
268 | for (i = 0; i < buf_len; i++)
|
---|
269 | {
|
---|
270 | fprintf(output_file, "\\x%02x", buf[i]);
|
---|
271 | }
|
---|
272 | fprintf(output_file, "\";\n");
|
---|
273 | fprintf(output_file, "static const size_t %s_len = %zu;\n" , var_name, buf_len);
|
---|
274 | fprintf(output_file, "static const size_t %s_olen = %zu;\n\n", var_name, obuf_len);
|
---|
275 | }
|
---|
276 |
|
---|
277 | #define HEAP_ALLOC(var, size) \
|
---|
278 | lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
|
---|
279 |
|
---|
280 | static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
|
---|
281 | #else
|
---|
282 |
|
---|
283 | static void dump_text(char *ident, uint8_t *buf, size_t buf_len)
|
---|
284 | {
|
---|
285 | int i;
|
---|
286 | fprintf(output_file, "#define TPL%s \\\n\"", ident);
|
---|
287 | for (i = 0; i < buf_len; i++)
|
---|
288 | {
|
---|
289 | switch (buf[i])
|
---|
290 | {
|
---|
291 | case '\n':
|
---|
292 | if (i < buf_len - 1)
|
---|
293 | fprintf(output_file, "\\n\\\n");
|
---|
294 | else
|
---|
295 | fprintf(output_file, "\\n");
|
---|
296 | break;
|
---|
297 | case '\\': fprintf(output_file, "\\\\"); break;
|
---|
298 | case '"' : fprintf(output_file, "\\\""); break;
|
---|
299 | default : fprintf(output_file, "%c", buf[i]); break;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | fprintf(output_file, "\"\n\n");
|
---|
303 | }
|
---|
304 |
|
---|
305 | static void dump_binary(char *ident, uint8_t *buf, size_t buf_len)
|
---|
306 | {
|
---|
307 | fprintf(output_file, "#define TPL%s \\\n\"", ident);
|
---|
308 | int i;
|
---|
309 | for (i = 0; i < buf_len; i++)
|
---|
310 | {
|
---|
311 | fprintf(output_file, "\\x%02x", buf[i]);
|
---|
312 | }
|
---|
313 | fprintf(output_file, "\"\n\n");
|
---|
314 | }
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | int main(void)
|
---|
318 | {
|
---|
319 | int i;
|
---|
320 |
|
---|
321 | parse_index_file(index_filename);
|
---|
322 |
|
---|
323 | output_file = xfopen(output_pages_h, "w");
|
---|
324 | fprintf(output_file, "#ifndef WEBIF_PAGES_H_\n");
|
---|
325 | fprintf(output_file, "#define WEBIF_PAGES_H_\n");
|
---|
326 | fprintf(output_file, "\n");
|
---|
327 | fprintf(output_file, "enum template_types {\n");
|
---|
328 | fprintf(output_file, " TEMPLATE_TYPE_TEXT = 0,\n");
|
---|
329 | fprintf(output_file, " TEMPLATE_TYPE_PNG = 1,\n");
|
---|
330 | fprintf(output_file, " TEMPLATE_TYPE_GIF = 2,\n");
|
---|
331 | fprintf(output_file, " TEMPLATE_TYPE_ICO = 3,\n");
|
---|
332 | fprintf(output_file, " TEMPLATE_TYPE_JPG = 4,\n");
|
---|
333 | fprintf(output_file, "};\n");
|
---|
334 | fprintf(output_file, "\n");
|
---|
335 | #ifdef USE_COMPRESSION
|
---|
336 | fprintf(output_file, "#define COMPRESSED_TEMPLATES 1\n\n");
|
---|
337 | fprintf(output_file, "struct template {\n");
|
---|
338 | fprintf(output_file, " uint32_t tpl_name_ofs;\n");
|
---|
339 | fprintf(output_file, " uint32_t tpl_data_ofs;\n");
|
---|
340 | fprintf(output_file, " uint32_t tpl_deps_ofs;\n");
|
---|
341 | fprintf(output_file, " uint32_t tpl_data_len;\n");
|
---|
342 | fprintf(output_file, " uint8_t tpl_type;\n");
|
---|
343 | fprintf(output_file, "};\n");
|
---|
344 | #else
|
---|
345 | fprintf(output_file, "struct template {\n");
|
---|
346 | fprintf(output_file, " char *tpl_name;\n");
|
---|
347 | fprintf(output_file, " char *tpl_data;\n");
|
---|
348 | fprintf(output_file, " char *tpl_deps;\n");
|
---|
349 | fprintf(output_file, " uint32_t tpl_data_len;\n");
|
---|
350 | fprintf(output_file, " uint8_t tpl_type;\n");
|
---|
351 | fprintf(output_file, "};\n");
|
---|
352 | #endif
|
---|
353 | fprintf(output_file, "\n");
|
---|
354 | fprintf(output_file, "int32_t templates_count(void);\n");
|
---|
355 | fprintf(output_file, "bool template_is_image(enum template_types tpl_type);\n");
|
---|
356 | fprintf(output_file, "const char *template_get_mimetype(enum template_types tpl_type);\n");
|
---|
357 | fprintf(output_file, "const struct template *templates_get(void);\n");
|
---|
358 | #ifdef USE_COMPRESSION
|
---|
359 | fprintf(output_file, "void templates_get_data(const char **data, size_t *data_len, size_t *odata_len);\n");
|
---|
360 | #endif
|
---|
361 | fprintf(output_file, "\n");
|
---|
362 | fprintf(output_file, "#endif\n");
|
---|
363 | fclose(output_file);
|
---|
364 |
|
---|
365 | output_file = xfopen(output_pages_c, "w");
|
---|
366 | fprintf(output_file, "#include \"../globals.h\"\n");
|
---|
367 | fprintf(output_file, "\n");
|
---|
368 | fprintf(output_file, "#ifdef WEBIF\n");
|
---|
369 | fprintf(output_file, "\n");
|
---|
370 | fprintf(output_file, "#include \"pages.h\"\n");
|
---|
371 | fprintf(output_file, "\n");
|
---|
372 |
|
---|
373 | #ifdef USE_COMPRESSION
|
---|
374 | // Calculate positions at which the values would be storred
|
---|
375 | uint32_t cur_pos = 0;
|
---|
376 | #define align_up(val, align) (val += (align - val % align))
|
---|
377 | for (i = 0; i < templates.num; i++)
|
---|
378 | {
|
---|
379 | struct template *t = &templates.data[i];
|
---|
380 | readfile(t->file, &t->buf, &t->buf_len);
|
---|
381 | t->data_len = t->buf_len;
|
---|
382 | // +1 to leave space for \0
|
---|
383 | t->ident_ofs = cur_pos; cur_pos += strlen(t->ident) + 1; align_up(cur_pos, sizeof(void *));
|
---|
384 | t->data_ofs = cur_pos; cur_pos += t->data_len + 1; align_up(cur_pos, sizeof(void *));
|
---|
385 | t->deps_ofs = cur_pos; cur_pos += strlen(t->deps) + 1; align_up(cur_pos, sizeof(void *));
|
---|
386 | }
|
---|
387 |
|
---|
388 | // Allocate template data and populate it
|
---|
389 | #define data_len cur_pos
|
---|
390 | uint8_t *data = calloc(1, data_len);
|
---|
391 | if (!data)
|
---|
392 | die("Can't alloc %u bytes", data_len);
|
---|
393 | for (i = 0; i < templates.num; i++)
|
---|
394 | {
|
---|
395 | struct template *t = &templates.data[i];
|
---|
396 | memcpy(data + t->ident_ofs, t->ident, strlen(t->ident));
|
---|
397 | memcpy(data + t->data_ofs , t->buf , t->buf_len);
|
---|
398 | free(t->buf);
|
---|
399 | if (!t->deps[0]) // No need to copy empty deps
|
---|
400 | continue;
|
---|
401 | memcpy(data + t->deps_ofs, t->deps, strlen(t->deps));
|
---|
402 | }
|
---|
403 | FILE *bin = xfopen("pages.bin", "w");
|
---|
404 | fwrite(data, data_len, 1, bin);
|
---|
405 | fclose(bin);
|
---|
406 |
|
---|
407 | // Compress template data
|
---|
408 | lzo_uint in_len = data_len;
|
---|
409 | lzo_uint out_len = data_len + data_len / 16 + 64 + 3; // Leave enough space in the output
|
---|
410 | uint8_t *out = malloc(out_len);
|
---|
411 | if (!out)
|
---|
412 | die("Can't alloc %zu bytes", out_len);
|
---|
413 |
|
---|
414 | if (lzo_init() != LZO_E_OK)
|
---|
415 | {
|
---|
416 | fprintf(stderr, "internal error - lzo_init() failed !!!\n");
|
---|
417 | fprintf(stderr, "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
|
---|
418 | return 3;
|
---|
419 | }
|
---|
420 |
|
---|
421 | int r = lzo1x_1_compress(data, in_len, out, &out_len, wrkmem);
|
---|
422 | if (r == LZO_E_OK)
|
---|
423 | {
|
---|
424 | printf("GEN\tCompressed %lu template bytes into %lu bytes. %ld saved bytes (%.2f%%).\n",
|
---|
425 | (unsigned long)in_len, (unsigned long)out_len,
|
---|
426 | (long)in_len - (long)out_len, 100 - ((float)out_len / in_len) * 100);
|
---|
427 | }
|
---|
428 | else
|
---|
429 | {
|
---|
430 | /* this should NEVER happen */
|
---|
431 | printf("internal error - compression failed: %d\n", r);
|
---|
432 | return 2;
|
---|
433 | }
|
---|
434 |
|
---|
435 | bin = xfopen("pages.bin.compressed", "w");
|
---|
436 | fwrite(out, out_len, 1, bin);
|
---|
437 | fclose(bin);
|
---|
438 |
|
---|
439 | dump_cbinary("templates_data", out, out_len, data_len);
|
---|
440 | free(out);
|
---|
441 | free(data);
|
---|
442 | #else
|
---|
443 | for (i = 0; i < templates.num; i++)
|
---|
444 | {
|
---|
445 | uint8_t *buf;
|
---|
446 | size_t buf_len;
|
---|
447 | readfile(templates.data[i].file, &buf, &buf_len);
|
---|
448 | templates.data[i].data_len = buf_len;
|
---|
449 | switch (templates.data[i].type)
|
---|
450 | {
|
---|
451 | case TXT: dump_text(templates.data[i].ident, buf, buf_len); break;
|
---|
452 | case BIN: dump_binary(templates.data[i].ident, buf, buf_len); break;
|
---|
453 | }
|
---|
454 | free(buf);
|
---|
455 | }
|
---|
456 | #endif
|
---|
457 |
|
---|
458 | fprintf(output_file, "static const struct template templates[] = {\n");
|
---|
459 | for (i = 0; i < templates.num; i++)
|
---|
460 | {
|
---|
461 | print_template(i);
|
---|
462 | }
|
---|
463 | fprintf(output_file, "};\n");
|
---|
464 | fprintf(output_file, "\n");
|
---|
465 | fprintf(output_file, "int32_t templates_count(void) { return sizeof(templates) / sizeof(struct template); }\n");
|
---|
466 | fprintf(output_file, "const struct template *templates_get(void) { return templates; }\n");
|
---|
467 | #ifdef USE_COMPRESSION
|
---|
468 | fprintf(output_file, "void templates_get_data(const char **data, size_t *data_len, size_t *data_olen) { *data = templates_data; *data_len = templates_data_len; *data_olen = templates_data_olen; }\n");
|
---|
469 | #endif
|
---|
470 | fprintf(output_file, "\n");
|
---|
471 | fprintf(output_file, "bool template_is_image(enum template_types tpl_type) {\n");
|
---|
472 | fprintf(output_file, " switch (tpl_type) {\n");
|
---|
473 | fprintf(output_file, " case TEMPLATE_TYPE_PNG:\n");
|
---|
474 | fprintf(output_file, " case TEMPLATE_TYPE_GIF:\n");
|
---|
475 | fprintf(output_file, " case TEMPLATE_TYPE_ICO:\n");
|
---|
476 | fprintf(output_file, " case TEMPLATE_TYPE_JPG:\n");
|
---|
477 | fprintf(output_file, " return true;\n");
|
---|
478 | fprintf(output_file, " default:\n");
|
---|
479 | fprintf(output_file, " return false;\n");
|
---|
480 | fprintf(output_file, " }\n");
|
---|
481 | fprintf(output_file, " return false;\n");
|
---|
482 | fprintf(output_file, "}\n");
|
---|
483 | fprintf(output_file, "\n");
|
---|
484 | fprintf(output_file, "const char *template_get_mimetype(enum template_types tpl_type) {\n");
|
---|
485 | fprintf(output_file, " switch (tpl_type) {\n");
|
---|
486 | fprintf(output_file, " case TEMPLATE_TYPE_TEXT: return \"text/plain\";\n");
|
---|
487 | fprintf(output_file, " case TEMPLATE_TYPE_PNG : return \"image/png\";\n");
|
---|
488 | fprintf(output_file, " case TEMPLATE_TYPE_GIF : return \"image/gif\";\n");
|
---|
489 | fprintf(output_file, " case TEMPLATE_TYPE_ICO : return \"image/x-icon\";\n");
|
---|
490 | fprintf(output_file, " case TEMPLATE_TYPE_JPG : return \"image/jpg\";\n");
|
---|
491 | fprintf(output_file, " }\n");
|
---|
492 | fprintf(output_file, " return \"\";\n");
|
---|
493 | fprintf(output_file, "}\n");
|
---|
494 | fprintf(output_file, "\n");
|
---|
495 | fprintf(output_file, "#endif\n");
|
---|
496 | fclose(output_file);
|
---|
497 |
|
---|
498 | return 0;
|
---|
499 | }
|
---|