/[pcsx2_0.9.7]/trunk/tools/bin2cpp/bin2cpp.cpp
ViewVC logotype

Contents of /trunk/tools/bin2cpp/bin2cpp.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years, 8 months ago) by william
File size: 6007 byte(s)
committing r3113 initial commit again...
1 //
2 // BIN2CPP - Some hack-up Job of some mess I found online.
3 //
4 // Original was uncredited public domain. This is uncredited public domain.
5 // Who am I? You'll have to guess. Walrus, Eggman, or the taxman? Maybe.
6 //
7 // (Officially: Provided to the PCSX2 Dev Team by a combined effort on the part of
8 // the PCSX2 Dev Team and the glorious expanse of the information superhighway).
9 //
10 // ------------------------------------------------------------------------
11 //
12 // Changes from uncredited online version:
13 // * Lots of code cleanups.
14 // * Upgraded from K&R syntax (!) to CPP syntax.
15 // * added wxWidgets class-based interface for instantiating images in neat fashion.
16 // The class and interface used to read images from the host app can be found in
17 // wxEmbeddedImage.cpp.
18 //
19 // Actually I changed every line of code pretty much, except one that I felt really
20 // embodied the personality and spirit of this utility. It's the line that makes it
21 // print the following message upon command line misuse:
22 //
23 // Bad arguments !!! You must give me all the parameters !!!!
24 //
25 // ... I love it. I really do.
26 //
27 // Warning: This program is full of stack overflow holes and other mess. Maybe we'll
28 // rewrite it in C# someday and fix all that stuff, but for now it serves its purpose
29 // and accomplishes its menial tasks with sufficent effectiveness.
30 //
31
32 #include <cstdio>
33 #include <cstring>
34 #include <ctype.h>
35
36 #include <sys/stat.h>
37 #include <string.h>
38
39 #if defined (__linux__) && !defined(__LINUX__) // some distributions are lower case
40 #define __LINUX__
41 #endif
42
43 #if _MSC_VER
44 # pragma warning(disable:4996) // The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name.
45 #endif
46
47 typedef unsigned char u8;
48 typedef char s8;
49
50 using namespace std;
51
52 static const unsigned int BUF_LEN = 1;
53 static const unsigned int LINE = 16;
54
55 /* Tell u the file size in bytes */
56
57 long getfilesize( const char* filename )
58 {
59 struct stat result;
60
61 stat( filename, &result );
62 return result.st_size;
63 }
64
65 enum
66 {
67 ARG_SRCFILE = 1,
68 ARG_DESTFILE,
69 ARG_CLASSNAME,
70 };
71
72 int main(int argc, char* argv[])
73 {
74 FILE *source,*dest;
75 u8 buffer[BUF_LEN];
76 s8 Dummy[260];
77 s8 srcfile[260];
78 s8 classname[260];
79
80 if ( (argc <= ARG_SRCFILE) )
81 {
82
83 if ( ( argc == 2 ) && ( strcmp(argv[1],"/?")==0 ) )
84 {
85 puts(
86 " - <<< BIN2CPP V1.1 For Win32 >>> by the PCSX2 Team - \n\n"
87 "USAGE: Bin2CPP <SOURCE image> [TARGET file] [CLASS]\n"
88 " <SOURCE> = without extension!\n"
89 " [TARGET] = without extension '.h' it will be added by program.\n"
90 " (defaults to <SOURCE> if unspecified)\n"
91 " [CLASS] = name of the C++ class in the destination file name.\n"
92 " (defaults to res_<TARGET> if unspecified)\n"
93 );
94 return 0L;
95 }
96 else
97 {
98 puts( "Bad arguments !!! You must give me all the parameters !!!!\n"
99 "Type 'BIN2CPP /?' to read the help.\n"
100 );
101 return 0L;
102 }
103
104 }
105
106 // ----------------------------------------------------------------------------
107 // Determine Source Name, and Open Source for Reading
108 // ----------------------------------------------------------------------------
109
110 strcpy(srcfile,argv[ARG_SRCFILE]);
111
112 int srcfn_len = strlen( srcfile );
113 if( srcfile[srcfn_len-4] != '.' )
114 {
115 printf( "ERROR : Malformed source filename. I'm a crap utility and I demand 3-letter extensions only!\n" );
116 return 18;
117 }
118
119 if( (source=fopen( srcfile, "rb" )) == NULL )
120 {
121 printf( "ERROR : I can't find source file %s\n", srcfile );
122 return 20;
123 }
124
125 const int filesize( getfilesize( srcfile ) );
126
127 char wxImgTypeUpper[24];
128 strcpy( wxImgTypeUpper, &srcfile[srcfn_len-3] );
129
130 int i = -1;
131 while(++i, wxImgTypeUpper[i] != 0)
132 wxImgTypeUpper[i] = toupper(wxImgTypeUpper[i]);
133
134 //strcpy( wxImgTypeLower, argv[ARG_IMGEXT] );
135 //char wxImgTypeLower[24];
136
137 if( strcmp( wxImgTypeUpper, "JPG" ) == 0 )
138 strcpy( wxImgTypeUpper, "JPEG" ); // because wxWidgets defines it as JPEG >_<
139
140 argv[ARG_SRCFILE][srcfn_len-4] = 0;
141
142 // ----------------------------------------------------------------------------
143 // Determine Target Name, and Open Target File for Writing
144 // ----------------------------------------------------------------------------
145
146 strcpy( Dummy, argv[(argc <= ARG_DESTFILE) ? ARG_SRCFILE : ARG_DESTFILE] );
147
148 strcat( Dummy,".h" );
149
150 if( (dest=fopen( Dummy, "wb+" )) == NULL )
151 {
152 printf( "ERROR : I can't open destination file %s\n", Dummy );
153 (void)fcloseall();
154 return 0L;
155 }
156
157 // ----------------------------------------------------------------------------
158
159 printf( "Bin2CPP Output > %s\n", Dummy );
160
161 const char* fnameonly = NULL;
162 if( argc <= ARG_CLASSNAME )
163 {
164 fnameonly = argv[(argc <= ARG_DESTFILE) ? ARG_SRCFILE : ARG_DESTFILE];
165 strcpy( classname, "res_" );
166 }
167 else
168 {
169 fnameonly = argv[ARG_CLASSNAME];
170 classname[0] = 0;
171 }
172
173 {
174 int len = strlen(fnameonly);
175 const char* fnlast = &fnameonly[len];
176 while( --fnlast, --len, (len >= 0 && (*fnlast != '/')) );
177
178 fnameonly = fnlast+1;
179 }
180
181 strcpy( classname, "res_" );
182 strcat( classname, fnameonly );
183
184 /* It writes the header information */
185
186 fprintf( dest,
187 "#pragma once\n\n"
188 "#include \"Pcsx2Types.h\"\n"
189 "#include <wx/gdicmn.h>\n\n"
190 "class %s\n{\n"
191 "public:\n"
192 "\tstatic const uint Length = %d;\n"
193 "\tstatic const u8 Data[Length];\n"
194 "\tstatic wxBitmapType GetFormat() { return wxBITMAP_TYPE_%s; }\n};\n\n"
195 "const u8 %s::Data[Length] =\n{\n",
196 classname, filesize, wxImgTypeUpper, classname
197 );
198
199 if( ferror( dest ) )
200 {
201 printf( "ERROR writing on target file: %s\n", Dummy );
202 (void)fcloseall();
203 return 20L;
204 }
205
206 /* It writes the binary data information! */
207 do
208 {
209 fprintf(dest,"\t");
210 for ( unsigned int c=0; c <= LINE; ++c )
211 {
212 if( fread( buffer, 1, 1, source ) == 0 ) break;
213
214 if( c != 0 )
215 fprintf( dest, "," );
216 fprintf( dest,"0x%02x", *buffer );
217 }
218 if( !feof( source ) )
219 fprintf( dest, "," );
220 fprintf(dest,"\n");
221 }
222 while( ! feof( source ) );
223
224 fprintf(dest,"};\n");
225
226 fcloseall();
227
228 return 0;
229 }
230

  ViewVC Help
Powered by ViewVC 1.1.22