2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
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 Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
20 /** \file blender/datatoc/datatoc.c
30 #define MAX2(x, y) ( (x) > (y) ? (x) : (y) )
31 #define MAX3(x, y, z) MAX2(MAX2((x), (y)), (z) )
33 static char *basename(char *string)
35 char *lfslash, *lbslash;
37 lfslash = strrchr(string, '/');
38 lbslash = strrchr(string, '\\');
39 if (lbslash) lbslash++;
40 if (lfslash) lfslash++;
42 return MAX3(string, lfslash, lbslash);
45 int main(int argc, char **argv)
53 printf("Usage: datatoc <data_file_from> <data_file_to>\n");
57 fpin = fopen(argv[1], "rb");
59 printf("Unable to open input <%s>\n", argv[1]);
63 argv[1] = basename(argv[1]);
65 fseek(fpin, 0L, SEEK_END);
67 fseek(fpin, 0L, SEEK_SET);
69 if (argv[1][0] == '.') argv[1]++;
72 printf("Making C file <%s>\n", argv[2]);
75 argv_len = (int)strlen(argv[1]);
76 for (i = 0; i < argv_len; i++)
77 if (argv[1][i] == '.') argv[1][i] = '_';
79 fpout = fopen(argv[2], "w");
81 fprintf(stderr, "Unable to open output <%s>\n", argv[2]);
85 fprintf(fpout, "/* DataToC output of file <%s> */\n\n", argv[1]);
87 /* Quiet 'missing-variable-declarations' warning. */
88 fprintf(fpout, "extern int datatoc_%s_size;\n", argv[1]);
89 fprintf(fpout, "extern char datatoc_%s[];\n\n", argv[1]);
91 fprintf(fpout, "int datatoc_%s_size = %d;\n", argv[1], (int)size);
92 fprintf(fpout, "char datatoc_%s[] = {\n", argv[1]);
94 /* if we want to open in an editor
95 * this is nicer to avoid very long lines */
97 if (size % 32 == 31) {
102 /* fprintf (fpout, "\\x%02x", getc(fpin)); */
103 fprintf(fpout, "%3d,", getc(fpin));
106 /* trailing NULL terminator, this isnt needed in some cases and
107 * won't be taken into account by the size variable, but its useful when dealing with
108 * NULL terminated string data */
109 fprintf(fpout, "0\n};\n\n");