Merge branch 'lua_versions' into main/rendor-staging
[ryzomcore.git] / tool / bin2c / bin2c.cpp
blob9782af119bed2180172596f21bd1e4f807d75ea7
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 int main(int argc, char* argv[])
23 if (argc<2)
25 printf ("bin2c <filename.bin> [filename.c]\n");
27 else
29 // path and filename but without extension
30 char sPathWithoutExtension[256];
32 // basename of file
33 char sName[256];
35 // copy fullpath from command-line
36 strcpy(sPathWithoutExtension, argv[1]);
38 char *tmpExt = strrchr(sPathWithoutExtension, '.');
40 // remove extension
41 if (tmpExt) *tmpExt = 0;
43 // look for last directory separator
44 const char *tmpName1 = strrchr(sPathWithoutExtension, '/');
45 #ifdef _WIN32
46 const char *tmpName2 = strrchr(sPathWithoutExtension, '\\');
47 #else
48 const char *tmpName2 = NULL;
49 #endif
51 // take last separator
52 const char *tmpName = tmpName1 > tmpName2 ? tmpName1:tmpName2;
54 // keep only path
55 if (tmpName)
57 // computes position in path
58 size_t pos = tmpName - sPathWithoutExtension;
60 // copy basename
61 strcpy(sName, sPathWithoutExtension+pos+1);
64 char sOutput[256];
66 if (argc>2)
68 strcpy (sOutput, argv[2]);
70 else
72 strcpy(sOutput, sPathWithoutExtension);
73 strcat(sOutput, ".cpp");
76 FILE *pIn=fopen( argv[1], "rb");
77 if (pIn==NULL)
78 printf ("Can't open %s.", argv[1]);
79 else
81 FILE *pOut=fopen( sOutput, "w");
82 if (pOut==NULL)
83 printf ("Can't open %s.", sOutput);
84 else
86 fprintf (pOut,
87 "/**\n"
88 " * Generated by bin2c.exe\n"
89 " * binfile: %s\n"
90 " */\n"
91 "\n"
92 "extern const unsigned char %s[];\n"
93 "extern const unsigned int %sSize;\n\n"
94 "static const unsigned char %s[] =\n"
95 "{\n", argv[1], sName, sName, sName);
97 unsigned int size=0;
98 int i;
99 while (1)
101 fprintf (pOut, "\t");
102 for (i=0; i<8; i++)
104 int c=fgetc (pIn);
105 if (c==EOF)
106 break;
107 fprintf (pOut, "0x%02x, ", c);
108 size++;
110 fprintf (pOut, "\n");
111 if (i!=8)
112 break;
114 fprintf (pOut, "};\n\n");
116 fprintf (pOut, "static const unsigned int %sSize = %d;\n\n", sName, size);
118 fclose (pOut);
119 fclose (pIn);
124 return 0;