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