Blender V4.5
datatoc.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14// #define VERBOSE
15
16#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
17
18static char *arg_basename(char *string)
19{
20 char *lfslash, *lbslash;
21
22 lfslash = strrchr(string, '/');
23 lbslash = strrchr(string, '\\');
24 if (lbslash) {
25 lbslash++;
26 }
27 if (lfslash) {
28 lfslash++;
29 }
30
31 return std::max({string, lfslash, lbslash});
32}
33
34int main(int argc, char **argv)
35{
36 FILE *fpin, *fpout;
37 long size;
38 int i;
39 int argv_len;
40
41 if (argc != 3) {
42 printf("Usage: datatoc <data_file_from> <data_file_to>\n");
43 exit(1);
44 }
45
46 fpin = fopen(argv[1], "rb");
47 if (!fpin) {
48 printf("Unable to open input <%s>\n", argv[1]);
49 exit(1);
50 }
51
52 argv[1] = arg_basename(argv[1]);
53
54 fseek(fpin, 0L, SEEK_END);
55 size = ftell(fpin);
56 fseek(fpin, 0L, SEEK_SET);
57
58 if (argv[1][0] == '.') {
59 argv[1]++;
60 }
61
62#ifdef VERBOSE
63 printf("Making C file <%s>\n", argv[2]);
64#endif
65
66 argv_len = int(strlen(argv[1]));
67 for (i = 0; i < argv_len; i++) {
68 if (argv[1][i] == '.') {
69 argv[1][i] = '_';
70 }
71 }
72
73 fpout = fopen(argv[2], "w");
74 if (!fpout) {
75 fprintf(stderr, "Unable to open output <%s>\n", argv[2]);
76 exit(1);
77 }
78
79 fprintf(fpout, "/* DataToC output of file <%s> */\n\n", argv[1]);
80
81 /* Quiet 'missing-variable-declarations' warning. */
82 fprintf(fpout, "extern const int datatoc_%s_size;\n", argv[1]);
83 fprintf(fpout, "extern const char datatoc_%s[];\n\n", argv[1]);
84
85 fprintf(fpout, "const int datatoc_%s_size = %d;\n", argv[1], int(size));
86 fprintf(fpout, "const char datatoc_%s[] = {\n", argv[1]);
87
88 while (size--) {
89 /* Even though this file is generated and doesn't need new-lines,
90 * these files may be loaded by developers when looking up symbols.
91 * Avoid a very long single line that may lock-up some editors. */
92 if (size % 32 == 31) {
93 fprintf(fpout, "\n");
94 }
95
96 // fprintf(fpout, "\\x%02x", getc(fpin));
97 fprintf(fpout, "%3d,", getc(fpin));
98 }
99
100 /* Trailing null terminator, this isn't needed in some cases and
101 * won't be taken into account by the size variable, but its useful when dealing with
102 * null terminated string data. */
103 fprintf(fpout, "0\n};\n\n");
104
105 fclose(fpin);
106 fclose(fpout);
107 return 0;
108}
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static char * arg_basename(char *string)
Definition datatoc.cc:18
#define main()
#define printf(...)
#define L
i
Definition text_draw.cc:230