-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwxInclude.cpp
More file actions
414 lines (342 loc) · 14.3 KB
/
wxInclude.cpp
File metadata and controls
414 lines (342 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
wxInclude
Kim De Deyn
*/
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/chrono.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <boost/timer/timer.hpp>
namespace po = boost::program_options;
namespace fs = boost::filesystem;
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#define WXINCLUDE_INFO "wxInclude by Kim De Deyn, use --help for more information.\n"
#define WXINCLUDE_HELP "This tool can be used to convert binary files into a useful C/C++ header.\n" \
"The primary goal is to provide wxWidgets users an easy way of integrating images in their programs. " \
"The addition of extra helper macros for wxWidgets can be disabled.\n\n" \
"It is able to convert multiple files into one header. " \
"Input can be defined by passing files or the extension masks you want to convert.\n\n" \
"Example of use:\n\n" \
" wxInclude.exe --const --input-file=mydata1.bin\n" \
" --input-type=.png --input-type=.bmp\n" \
" --output-file=myheader.h mydata2.bin myimage.png\n\n" \
"Recommended usage (at Space Dynamics Lab):\n" \
" wxInclude --const --appendtype --wxnone --respectcase --output-file=foo.hrc foo.png\n"
#define WXINCLUDE_VERSION "Version 1.2, compiled on " __DATE__ " at " __TIME__
#define BUFFER_SIZE 4096
void defineheader_start( std::ostringstream& data, std::string& headername, bool usemacro = true, bool useconst = false )
{
/* Write info header */
data << "/*" << std::endl;
data << " Automatic generated header by:" << std::endl << std::endl;
data << " " << WXINCLUDE_INFO;
data << " " << WXINCLUDE_VERSION << std::endl << std::endl;
data << " Header: " << headername << std::endl;
data << " Macros: " << ( usemacro ? "yes" : "no" ) << std::endl;
data << " Const: " << ( useconst ? "yes" : "no" ) << std::endl;
data << "*/" << std::endl << std::endl;
/* Prevent multiple defines */
std::string temp( headername );
boost::to_upper( temp );
data << "#ifndef _WXINCLUDE_" << temp << "_H_" << std::endl;
data << "#define _WXINCLUDE_" << temp << "_H_" << std::endl << std::endl;
}
void defineheader_end( std::ostringstream& data, std::string& name )
{
/* Prevent multiple defines */
data << "#endif" << std::endl << std::endl;
}
void definemacros( std::ostringstream& data, std::string& includename, bool definestream = true )
{
/* Include wxWidgets header */
data << "#include \"" << includename << "\"" << std::endl;
/* When using default header, include memory stream header!*/
if ( definestream )
data << "#include \"wx/mstream.h\"" << std::endl;
data << std::endl;
/* Define some useful macros */
data << "#define wxMEMORY_IMAGE( name ) _wxConvertMemoryToImage( name, sizeof( name ) )" << std::endl;
data << "#define wxMEMORY_IMAGEEX( name, type ) _wxConvertMemoryToImage( name, sizeof( name ), type )" << std::endl;
data << "#define wxMEMORY_BITMAP( name ) _wxConvertMemoryToBitmap( name, sizeof( name ) )" << std::endl;
data << "#define wxMEMORY_BITMAPEX( name, type ) _wxConvertMemoryToBitmap( name, sizeof( name ), type )" << std::endl << std::endl;
data << "inline wxImage _wxConvertMemoryToImage(const unsigned char* data, int length, long type = wxBITMAP_TYPE_ANY )" << std::endl;
data << "{" << std::endl;
data << " wxMemoryInputStream stream( data, length );" << std::endl;
data << " return wxImage( stream, type, -1 );" << std::endl;
data << "}" << std::endl << std::endl;
data << "inline wxBitmap _wxConvertMemoryToBitmap(const unsigned char* data, int length, long type = wxBITMAP_TYPE_ANY )" << std::endl;
data << "{" << std::endl;
data << " wxMemoryInputStream stream( data, length );" << std::endl;
data << " return wxBitmap( wxImage( stream, type, -1 ), -1 );" << std::endl;
data << "}" << std::endl << std::endl;
}
static std::vector<std::string> list;
void definefile( std::ostringstream& data, fs::ifstream& input, std::string& name, bool useconst = false )
{
/* Check if already defined */
std::vector<std::string>::iterator search = std::find( list.begin(), list.end(), name );
if ( search == list.end() )
{
list.push_back( name );
}
else
{
/* Show warning, object of this name is already processed! */
std::cout << "Warning: '" << name << "' already defined, processing of new one stopped." << std::endl;
return;
}
/* Define array */
data << "static" << ( useconst ? " const " : " " ) << "unsigned char " << name << "[] = {" << std::endl;
int size = static_cast<int>(input.tellg()); // conversion from std::streamoff to int, possible loss of data
input.seekg( 0, std::ios::beg );
int c = 0;
int col = 0;
for ( int i = 1; i <= size; ++i )
{
/* Get character and add to array */
c = input.get();
/*
Using a static copy of the boost::format string gives a nice performance boost!
Boost help says using const boost::format fmter(fstring);
But static is faster and using the object without copy constructor is even faster!
*/
//static boost::format fmt( "0x%02X" );
//data << fmt % c;
/*
Fast option then... this code is executed allot!
Still faster then the optimized boost::format use, but not that much!
*/
static char temp[5];
snprintf( temp, 5, "0x%02X", c );
data << temp;
if ( i >= size )
{
/* Last character */
data << std::endl;
}
else
{
/* Next */
data << ", ";
}
/* New colume? */
int curcol = ( i / 10 );
if ( col < curcol )
{
col = curcol;
data << std::endl;
}
}
data << "};" << std::endl << std::endl;
}
int main(int argc, char* argv[])
{
try
{
po::options_description desc( "Options" );
desc.add_options()
( "help,h", "Show detailed help." )
( "options,p", "Show parameter information." )
( "version,v", "Show version information." )
( "quiet,q", "Quiet at runtime, not verbose." )
( "input-file,i", po::value<std::vector<std::string> >(), "Define file(s) for the convertion input." )
( "input-type,I", po::value<std::vector<std::string> >(), "Define file type(s) for automatic conversion of files in the working directory." )
( "output-file,o", po::value<std::string>(), "Define file for the convertion output." )
( "noheader,h", "Disable adding of header support defines." )
( "const,C", "Define array as const." )
( "respectcase,r", "Disable converting file types into lower case." )
( "wxnone,w", "Disable adding of wxWidgets support macro's." )
( "wxheader,W", po::value<std::string>()->default_value( "wx/wx.h" ), "Select the header that includes wxWidgets (precompiled header?)." )
( "appendtype,t", "Add the file type at the end of the identifier (myimage_png)." )
;
po::positional_options_description posdesc;
posdesc.add( "input-file", -1 );
po::variables_map opt;
po::store( po::command_line_parser( argc, argv ).options( desc ).positional( posdesc ).run(), opt );
fs::ifstream ifs(fs::path("default.cfg"));
po::store( po::parse_config_file( ifs, desc ), opt );
po::notify( opt );
if ( !opt.count( "quiet" ) )
std::cout << WXINCLUDE_INFO << std::endl;
/* Show options when requested */
if ( opt.count( "options" ) )
{
std::cout << desc << std::endl << std::endl;
exit( 0 );
}
/* Show help when requested */
if ( opt.count( "help" ) )
{
std::cout << WXINCLUDE_HELP;
std::cout << std::endl << desc << std::endl << std::endl;
exit( 0 );
}
/* Show version when requested */
if ( opt.count( "version" ) )
{
std::cout << WXINCLUDE_VERSION << std::endl;
exit( 0 );
}
/* Process */
if ( opt.count( "input-file" ) || opt.count( "input-type" ) )
{
if ( opt.count( "output-file" ) )
{
/* Create timer */
boost::timer::cpu_timer timer;
/* Create output file */
std::string headername( opt[ "output-file" ].as<std::string>() );
fs::path outputpath( headername );
fs::ofstream output( outputpath, std::ios::out | std::ios::trunc | std::ios::binary );
/* Use buffer */
char outbuffer[BUFFER_SIZE];
output.rdbuf()->pubsetbuf( outbuffer, BUFFER_SIZE );
if ( !output )
throw std::runtime_error( "Failed to create output file!" );
if ( !opt.count( "quiet" ) ) /* Show status */
std::cout << "Build : file '" << outputpath.filename().string() << "'..." << std::endl;
/* Get base name of file */
headername = outputpath.stem().string();
/* Data string stream */
std::ostringstream data;
/* Write header start when wanted */
if ( !opt.count( "noheader" ) )
defineheader_start( data, headername, opt.count( "wxnone" ) ? false : true, opt.count( "const" ) ? true : false );
/* Get defined or else default wx header */
std::string includename( opt[ "wxheader" ].as<std::string>() );
/* Write macros when wanted */
if ( !opt.count( "wxnone" ) )
definemacros( data, includename, opt[ "wxheader" ].defaulted() );
/* Common input buffer */
char inbuffer[BUFFER_SIZE];
/* Process input files based on provided list */
if ( opt.count( "input-file" ) )
{
std::vector<std::string> files( opt[ "input-file" ].as<std::vector<std::string> >() );
BOOST_FOREACH( std::string& file, files )
{
fs::path inputpath( file );
std::string fileext = inputpath.extension().string();
fs::ifstream input( inputpath, std::ios::in | std::ios::binary | std::ios::ate );
input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE );
if ( input.is_open() )
{
if ( !opt.count( "quiet" ) ) /* Show status */
std::cout << "Process: file '" << file << "'..." << std::endl;
/* Remove extension */
boost::erase_last( file, fileext );
if ( !opt.count( "respectcase" ) )
boost::to_lower( fileext );
/* Append type */
if ( opt.count( "appendtype" ) )
{
boost::erase_first( fileext, "." );
/* Static and NO copy constructor for speed */
static boost::format fmt( "%1%_%2%" );
file = boost::str( fmt % file % fileext );
}
/* Lower case names when wanted */
if ( !opt.count( "respectcase" ) )
boost::to_lower( file );
/* Process file */
definefile( data, input, file, opt.count( "const" ) ? true : false );
}
else
{
/* Only show warning, other files need to be processed */
std::cout << "Warning: input file '" << file << "' failed to open." << std::endl;
}
}
}
/* Process input files based on provided type */
if ( opt.count( "input-type" ) )
{
std::vector<std::string> types( opt[ "input-type" ].as<std::vector<std::string> >() );
for ( fs::directory_iterator dir_itr( fs::initial_path() ); dir_itr != fs::directory_iterator(); ++dir_itr )
{
BOOST_FOREACH( std::string& type, types )
{
/* Normal file? */
if ( fs::is_regular_file( dir_itr->status() ) )
{
/* Wanted type? */
std::string fileext = dir_itr->path().extension().string();
bool equals = false;
if ( opt.count( "respectcase" ) )
equals = boost::equals( fileext, type );
else
equals = boost::iequals( fileext, type );
if ( equals )
{
fs::ifstream input( dir_itr->path(), std::ios::in | std::ios::binary | std::ios::ate );
input.rdbuf()->pubsetbuf( inbuffer, BUFFER_SIZE );
std::string file = dir_itr->path().filename().string();
if ( input.is_open() )
{
if ( !opt.count( "quiet" ) ) /* Show status */
std::cout << "Process: file '" << file << "'..." << std::endl;
/* Remove extension */
boost::erase_last( file, fileext );
/* Append type */
if ( opt.count( "appendtype" ) )
{
boost::erase_first( fileext, "." );
/* Static and NO copy constructor for speed */
static boost::format fmt( "%1%_%2%" );
file = boost::str( fmt % file % fileext );
}
/* Lower case names when wanted */
if ( !opt.count( "respectcase" ) )
boost::to_lower( file );
/* Process file */
definefile( data, input, file, opt.count( "const" ) ? true : false );
}
else
{
/* Only show warning, other files need to be processed */
std::cout << "Warning: input file '" << file << "' failed to open." << std::endl;
}
}
}
}
}
}
/* Write header end when wanted */
if ( !opt.count( "noheader" ) )
defineheader_end( data, headername );
/* Write data to output file */
output.seekp( 0, std::ios::beg );
output << data.str();
if ( !opt.count( "quiet" ) ) /* Show status */
{
auto nanoseconds = boost::chrono::nanoseconds(timer.elapsed().user + timer.elapsed().system);
auto seconds = boost::chrono::duration_cast<boost::chrono::seconds>(nanoseconds);
std::cout << "Build : " << seconds.count() << "s needed for conversion of " << list.size() << " files." << std::endl;
}
}
else
{
throw std::invalid_argument( "No output defined!" );
}
}
else
{
throw std::invalid_argument( "No input defined!" );
}
}
catch( std::exception& e )
{
std::cerr << "Error: " << e.what() << std::endl;
}
catch( ... )
{
std::cerr << "Error: Exception of unknown type!" << std::endl;
}
return 0;
}