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