ZynAddSubFX: fixed identifier for filter resonance controller
[lmms.git] / buildtools / bin2res.cpp
blob54e9483ce1f48cc783ae8ce5ed3abcc5d7137ec2
1 /*
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.
24 #include <string>
25 #include <vector>
26 #include <iostream>
27 #include <fstream>
29 static void embedData( const char * _input, int _size, std::ostream & _output );
30 static std::string convertFileNameToCIdentifier( const std::string & _s );
33 struct embed
35 unsigned int size;
36 std::string name;
37 std::string cname;
38 } ;
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 )
47 if( argc < 2 )
49 std::cerr << "Usage:" << std::endl << "\t" << argv[0] <<
50 " files" << std::endl;
51 return( 1 );
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;
61 stringlist files;
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 );
69 if( f.fail() )
71 std::cerr << "Cannot open file " << *it <<
72 ", ignoring it" << std::endl;
73 continue;
75 f.seekg( 0, std::ios::end );
76 int fsize = f.tellg();
77 f.seekg( 0 );
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." <<
82 std::endl;
84 char * data = new char[fsize];
85 f.read( data, fsize );
86 embed * e = new embed;
87 e->size = fsize;
88 if( it->rfind( '/' ) != std::string::npos )
90 e->name = std::string( it->c_str() +
91 it->rfind( '/' ) + 1 );
93 else
95 e->name = *it;
97 e->cname = convertFileNameToCIdentifier( e->name );
98 embedded_data.push_back( e );
99 std::string s;
100 std::cout << "static const unsigned char " << e->cname <<
101 "_data[] = {";
102 embedData( data, fsize, std::cout );
103 std::cout << std::endl << "};" << std::endl << std::endl;
104 delete[] data;
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;
112 dummy->size = 1;
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;" <<
124 std::endl
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;
133 delete e;
134 embedded_data.erase( embedded_data.begin() );
136 std::cout << " { 0, 0, 0 }" << std::endl << "};" << std::endl
137 << 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; "
142 "i++ )" << std::endl
143 << " {" << std::endl
144 << " if( strcmp( embed_vec[i].name, "
145 "_name ) == 0 )" << std::endl
146 << " {" << std::endl
147 << " return( "
148 "embed_vec[i] );" << std::endl
149 << " }" << std::endl
150 << " }" << std::endl
151 /* << " printf( \"warning: embedded resource "
152 "%s not found!\\n\", _name );"
153 << std::endl*/
154 << " return( findEmbeddedData( "
155 "\"dummy\" ) );" << std::endl
156 << "}" << std::endl << std::endl;
158 std::cout << "#endif" << std::endl;
159 return( 0 );
165 std::string convertFileNameToCIdentifier( const std::string & _s )
167 std::string r = _s;
168 int len = r.length();
169 if ( len > 0 && !isalpha( (char)r[0] ) )
171 r[0] = '_';
173 for ( int i = 1; i < len; i++ )
175 if ( !isalnum( (char)r[i] ) )
177 r[i] = '_';
180 return( r );
186 void embedData( const char * _input, int _nbytes, std::ostream & _output )
188 static const char hexdigits[] = "0123456789abcdef";
189 std::string s;
190 for( int i = 0; i < _nbytes; i++ )
192 if( ( i%14 ) == 0 )
194 s += "\n ";
195 _output << s;
196 s = "";
198 unsigned int v = _input[i];
199 s += "0x";
200 s += hexdigits[(v >> 4) & 15];
201 s += hexdigits[v & 15];
202 if( i < _nbytes-1 )
204 s += ',';
207 if ( s.length() )
209 _output << s;