2 * bin2res.cpp - generate embedded resources from binary data (based on qembed)
4 * Copyright (c) 2005-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program (see COPYING); if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
29 static void embedData( const char * _input
, int _size
, std::ostream
& _output
);
30 static std::string
convertFileNameToCIdentifier( const std::string
& _s
);
40 typedef std::vector
<std::string
> stringlist
;
42 const int MAX_FILE_SIZE
= 256*256*256; // = 16 MB
45 int main( int argc
, char * * argv
)
49 std::cerr
<< "Usage:" << std::endl
<< "\t" << argv
[0] <<
50 " files" << std::endl
;
54 std::cout
<< "// Generated by bin2res" << std::endl
;
56 std::vector
<embed
*> embedded_data
;
58 std::cout
<< "#ifndef _EMBEDDED_RESOURCES_H" << std::endl
;
59 std::cout
<< "#define _EMBEDDED_RESOURCES_H" << std::endl
;
62 for( int i
= 1; i
< argc
; ++i
)
64 files
.push_back( std::string( argv
[i
] ) );
66 for( stringlist::iterator it
= files
.begin(); it
!= files
.end(); ++it
)
68 std::ifstream
f( it
->c_str(), std::ios::binary
);
71 std::cerr
<< "Cannot open file " << *it
<<
72 ", ignoring it" << std::endl
;
75 f
.seekg( 0, std::ios::end
);
76 int fsize
= f
.tellg();
78 if( fsize
== 0 || fsize
> MAX_FILE_SIZE
)
80 std::cerr
<< "File " << *it
<< " has zero size or is "
81 "too large to be processed with bin2res." <<
84 char * data
= new char[fsize
];
85 f
.read( data
, fsize
);
86 embed
* e
= new embed
;
88 if( it
->rfind( '/' ) != std::string::npos
)
90 e
->name
= std::string( it
->c_str() +
91 it
->rfind( '/' ) + 1 );
97 e
->cname
= convertFileNameToCIdentifier( e
->name
);
98 embedded_data
.push_back( e
);
100 std::cout
<< "static const unsigned char " << e
->cname
<<
102 embedData( data
, fsize
, std::cout
);
103 std::cout
<< std::endl
<< "};" << std::endl
<< std::endl
;
107 if( embedded_data
.size() > 0 )
109 std::cout
<< "static const unsigned char dummy_data[] ="
110 "{ 0x00 };" << std::endl
<< std::endl
;
111 embed
* dummy
= new embed
;
113 dummy
->name
= "dummy";
114 dummy
->cname
= convertFileNameToCIdentifier(
115 std::string( "dummy" ) );
116 embedded_data
.push_back( dummy
);
118 std::cout
<< "#include <string.h>" << std::endl
<< std::endl
;
119 std::cout
<< "#include \"embed.h\"" << std::endl
<< std::endl
;
120 std::cout
<< "static embed::descriptor embed_vec[] = {" << std::endl
;
121 /* << "{" << std::endl
122 << " int size;" << std::endl
123 << " const unsigned char * data;" <<
125 << " const char * name;" << std::endl
126 << "} embed_vec[] = {" << std::endl;*/
127 while( embedded_data
.size() > 0 )
129 embed
* e
= embedded_data
[0];
130 std::cout
<< " { " << e
->size
<< ", " << e
->cname
<<
131 "_data, " << "\"" << e
->name
<<
132 "\" }," << std::endl
;
134 embedded_data
.erase( embedded_data
.begin() );
136 std::cout
<< " { 0, 0, 0 }" << std::endl
<< "};" << std::endl
138 << "static const embed::descriptor & "
139 "findEmbeddedData( const char * _name )"
140 << std::endl
<< "{" << std::endl
141 << " for( int i = 0; embed_vec[i].data; "
144 << " if( strcmp( embed_vec[i].name, "
145 "_name ) == 0 )" << std::endl
148 "embed_vec[i] );" << std::endl
151 /* << " printf( \"warning: embedded resource "
152 "%s not found!\\n\", _name );"
154 << " return( findEmbeddedData( "
155 "\"dummy\" ) );" << std::endl
156 << "}" << std::endl
<< std::endl
;
158 std::cout
<< "#endif" << std::endl
;
165 std::string
convertFileNameToCIdentifier( const std::string
& _s
)
168 int len
= r
.length();
169 if ( len
> 0 && !isalpha( (char)r
[0] ) )
173 for ( int i
= 1; i
< len
; i
++ )
175 if ( !isalnum( (char)r
[i
] ) )
186 void embedData( const char * _input
, int _nbytes
, std::ostream
& _output
)
188 static const char hexdigits
[] = "0123456789abcdef";
190 for( int i
= 0; i
< _nbytes
; i
++ )
198 unsigned int v
= _input
[i
];
200 s
+= hexdigits
[(v
>> 4) & 15];
201 s
+= hexdigits
[v
& 15];