Blender V5.3
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#include <filesystem>
14#include <iostream>
15
16// #define VERBOSE
17
18#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
19
20static char *arg_basename(char *string)
21{
22 char *lfslash, *lbslash;
23
24 lfslash = strrchr(string, '/');
25 lbslash = strrchr(string, '\\');
26 if (lbslash) {
27 lbslash++;
28 }
29 if (lfslash) {
30 lfslash++;
31 }
32
33 return std::max({string, lfslash, lbslash});
34}
35
36int main(int argc, char **argv)
37{
38 FILE *fpin, *fpout;
39 long size;
40 int i;
41 int argv_len;
42
43 if (argc < 3 || argc > 4) {
44 printf("Usage: datatoc <data_file_from> <data_file_to> [<symbol_name_override>]\n");
45 exit(1);
46 }
47
48 fpin = fopen(argv[1], "rb");
49 if (!fpin) {
50 printf("Unable to open input <%s>\n", argv[1]);
51 exit(1);
52 }
53
54 /* Use the optional symbol name override, otherwise derive from the input filename. */
55 argv[1] = (argc >= 4) ? argv[3] : arg_basename(argv[1]);
56
57 fseek(fpin, 0L, SEEK_END);
58 size = ftell(fpin);
59 fseek(fpin, 0L, SEEK_SET);
60
61 if (argv[1][0] == '.') {
62 argv[1]++;
63 }
64
65#ifdef VERBOSE
66 printf("Making C file <%s>\n", argv[2]);
67#endif
68
69 /* We make the required directories here rather than having the build system
70 * do the work for us, as having cmake do it leads to several thousand cmake
71 * instances being launched, leading to significant overhead, see pr #141404
72 * for details. */
73 std::filesystem::path parent_dir = std::filesystem::path(argv[2]).parent_path();
74 std::error_code ec;
75 if (!std::filesystem::create_directories(parent_dir, ec)) {
76 if (ec) {
77 std::cerr << "Unable to create " << parent_dir << " : " << ec.message() << std::endl;
78 exit(1);
79 }
80 }
81
82 argv_len = int(strlen(argv[1]));
83 for (i = 0; i < argv_len; i++) {
84 if (argv[1][i] == '.') {
85 argv[1][i] = '_';
86 }
87 }
88
89 fpout = fopen(argv[2], "w");
90 if (!fpout) {
91 fprintf(stderr, "Unable to open output <%s>\n", argv[2]);
92 exit(1);
93 }
94
95 fprintf(fpout, "/* DataToC output of file <%s> */\n\n", argv[1]);
96
97 /* Quiet 'missing-variable-declarations' warning. */
98 fprintf(fpout, "extern const int datatoc_%s_size;\n", argv[1]);
99 fprintf(fpout, "extern const char datatoc_%s[];\n\n", argv[1]);
100
101 fprintf(fpout, "const int datatoc_%s_size = %d;\n", argv[1], int(size));
102 fprintf(fpout, "const char datatoc_%s[] = {\n", argv[1]);
103
104 while (size--) {
105 /* Even though this file is generated and doesn't need new-lines,
106 * these files may be loaded by developers when looking up symbols.
107 * Avoid a very long single line that may lock-up some editors. */
108 if (size % 32 == 31) {
109 fprintf(fpout, "\n");
110 }
111
112 // fprintf(fpout, "\\x%02x", getc(fpin));
113 fprintf(fpout, "%3d,", getc(fpin));
114 }
115
116 /* Trailing null terminator, this isn't needed in some cases and
117 * won't be taken into account by the size variable, but its useful when dealing with
118 * null terminated string data. */
119 fprintf(fpout, "0\n};\n\n");
120
121 fclose(fpin);
122 fclose(fpout);
123 return 0;
124}
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define printf
static char * arg_basename(char *string)
Definition datatoc.cc:20
#define main()
#define L