Blender  V2.93
datatoc.c
Go to the documentation of this file.
1 /*
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.
6  *
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.
11  *
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.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 /* #define VERBOSE */
29 
30 #define MAX2(x, y) ((x) > (y) ? (x) : (y))
31 #define MAX3(x, y, z) MAX2(MAX2((x), (y)), (z))
32 
33 static char *basename(char *string)
34 {
35  char *lfslash, *lbslash;
36 
37  lfslash = strrchr(string, '/');
38  lbslash = strrchr(string, '\\');
39  if (lbslash) {
40  lbslash++;
41  }
42  if (lfslash) {
43  lfslash++;
44  }
45 
46  return MAX3(string, lfslash, lbslash);
47 }
48 
49 int main(int argc, char **argv)
50 {
51  FILE *fpin, *fpout;
52  long size;
53  int i;
54  int argv_len;
55 
56  if (argc < 2) {
57  printf("Usage: datatoc <data_file_from> <data_file_to>\n");
58  exit(1);
59  }
60 
61  fpin = fopen(argv[1], "rb");
62  if (!fpin) {
63  printf("Unable to open input <%s>\n", argv[1]);
64  exit(1);
65  }
66 
67  argv[1] = basename(argv[1]);
68 
69  fseek(fpin, 0L, SEEK_END);
70  size = ftell(fpin);
71  fseek(fpin, 0L, SEEK_SET);
72 
73  if (argv[1][0] == '.') {
74  argv[1]++;
75  }
76 
77 #ifdef VERBOSE
78  printf("Making C file <%s>\n", argv[2]);
79 #endif
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  while (size--) {
103  /* if we want to open in an editor
104  * this is nicer to avoid very long lines */
105 #ifdef VERBOSE
106  if (size % 32 == 31) {
107  fprintf(fpout, "\n");
108  }
109 #endif
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
int main(int argc, char **argv)
Definition: datatoc.c:49
static char * basename(char *string)
Definition: datatoc.c:33
#define MAX3(x, y, z)
Definition: datatoc.c:31
#define L