diff options
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/bin2cpp.cpp | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/src/tools/bin2cpp.cpp b/src/tools/bin2cpp.cpp index 4a0befa..2c227f5 100644 --- a/src/tools/bin2cpp.cpp +++ b/src/tools/bin2cpp.cpp | |||
@@ -14,14 +14,18 @@ class Options : public OptParser | |||
14 | { | 14 | { |
15 | public: | 15 | public: |
16 | Options( int argc, char *argv[] ) : | 16 | Options( int argc, char *argv[] ) : |
17 | sOutBase("bin") | 17 | sClass("Datafiles") |
18 | { | 18 | { |
19 | addOption( sOutBase, 'o', "Output base filename [default=\"bin\"]"); | 19 | addOption( sClass, 'c', "Class name [default=\"Datafiles\"]"); |
20 | addOption( sOutBase, 'o', "Output base filename [defaults to classname]"); | ||
20 | addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64"); | 21 | addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64"); |
21 | setNonOption( slot(this, &Options::addInput) ); | 22 | setNonOption( slot(this, &Options::addInput) ); |
22 | addHelpOption(); | 23 | addHelpOption(); |
23 | 24 | ||
24 | parse( argc, argv ); | 25 | parse( argc, argv ); |
26 | |||
27 | if( !sOutBase.isSet() ) | ||
28 | sOutBase = sClass.toLower(); | ||
25 | } | 29 | } |
26 | 30 | ||
27 | virtual ~Options() | 31 | virtual ~Options() |
@@ -36,11 +40,11 @@ public: | |||
36 | 40 | ||
37 | int addInput( Bu::StrArray aArgs ) | 41 | int addInput( Bu::StrArray aArgs ) |
38 | { | 42 | { |
39 | sio << "Input: " << aArgs[0] << sio.nl; | ||
40 | slInput.append( aArgs[0] ); | 43 | slInput.append( aArgs[0] ); |
41 | return 0; | 44 | return 0; |
42 | } | 45 | } |
43 | 46 | ||
47 | Bu::String sClass; | ||
44 | Bu::String sOutBase; | 48 | Bu::String sOutBase; |
45 | Bu::StringList slInput; | 49 | Bu::StringList slInput; |
46 | Bu::StringList slFilter; | 50 | Bu::StringList slFilter; |
@@ -55,12 +59,12 @@ int main( int argc, char *argv[] ) | |||
55 | 59 | ||
56 | Formatter fHdr( fHdrOut ); | 60 | Formatter fHdr( fHdrOut ); |
57 | Formatter fSrc( fSrcOut ); | 61 | Formatter fSrc( fSrcOut ); |
58 | fHdr << "#ifndef BIN2CPP_DATAFILE_H" << fHdr.nl | 62 | fHdr << "#ifndef BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl |
59 | << "#define BIN2CPP_DATAFILE_H" << fHdr.nl << fHdr.nl | 63 | << "#define BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl << fHdr.nl |
60 | << "#include <bu/string.h>" << fHdr.nl | 64 | << "#include <bu/string.h>" << fHdr.nl |
61 | << "#include <bu/streamstack.h>" << fHdr.nl | 65 | << "#include <bu/streamstack.h>" << fHdr.nl |
62 | << fHdr.nl | 66 | << fHdr.nl |
63 | << "class Datafiles" << fHdr.nl | 67 | << "class " << opt.sClass << fHdr.nl |
64 | << "{" << fHdr.nl | 68 | << "{" << fHdr.nl |
65 | << "public:" << fHdr.nl | 69 | << "public:" << fHdr.nl |
66 | << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl | 70 | << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl |
@@ -80,8 +84,8 @@ int main( int argc, char *argv[] ) | |||
80 | << "#include <bu/bzip2.h>" << fSrc.nl | 84 | << "#include <bu/bzip2.h>" << fSrc.nl |
81 | << "#include <bu/base64.h>" << fSrc.nl | 85 | << "#include <bu/base64.h>" << fSrc.nl |
82 | << "#include <bu/strfilter.h>" << fSrc.nl | 86 | << "#include <bu/strfilter.h>" << fSrc.nl |
83 | << "#include <bu/membuf.h>" << fSrc.nl << fSrc.nl | 87 | << "#include <bu/staticmembuf.h>" << fSrc.nl << fSrc.nl |
84 | << "const Datafiles::File Datafiles::aFile[] = {" << fSrc.nl; | 88 | << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl; |
85 | 89 | ||
86 | for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) | 90 | for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) |
87 | { | 91 | { |
@@ -128,7 +132,7 @@ int main( int argc, char *argv[] ) | |||
128 | } | 132 | } |
129 | fSrc << "};" << fSrc.nl << fSrc.nl; | 133 | fSrc << "};" << fSrc.nl << fSrc.nl; |
130 | 134 | ||
131 | fSrc << "const Datafiles::File &Datafiles::getFile( const Bu::String &sName )" | 135 | fSrc << "const " << opt.sClass << "::File &" << opt.sClass << "::getFile( const Bu::String &sName )" |
132 | << fSrc.nl | 136 | << fSrc.nl |
133 | << "{" << fSrc.nl | 137 | << "{" << fSrc.nl |
134 | << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl | 138 | << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl |
@@ -145,10 +149,10 @@ int main( int argc, char *argv[] ) | |||
145 | << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl | 149 | << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl |
146 | << "}" << fSrc.nl << fSrc.nl; | 150 | << "}" << fSrc.nl << fSrc.nl; |
147 | 151 | ||
148 | fSrc << "Bu::StreamStack *Datafiles::open( const Bu::String &sName )" << fSrc.nl | 152 | fSrc << "Bu::StreamStack *" << opt.sClass << "::open( const Bu::String &sName )" << fSrc.nl |
149 | << "{" << fSrc.nl | 153 | << "{" << fSrc.nl |
150 | << "\tconst File &f = getFile( sName );" << fSrc.nl | 154 | << "\tconst File &f = getFile( sName );" << fSrc.nl |
151 | << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::MemBuf( Bu::String( f.data, f.iSize ) ) );" << fSrc.nl | 155 | << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl |
152 | << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl | 156 | << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl |
153 | << "\t{" << fSrc.nl | 157 | << "\t{" << fSrc.nl |
154 | << "\t\tswitch( *t )" << fSrc.nl | 158 | << "\t\tswitch( *t )" << fSrc.nl |
@@ -161,29 +165,26 @@ int main( int argc, char *argv[] ) | |||
161 | << "\treturn s;" << fSrc.nl | 165 | << "\treturn s;" << fSrc.nl |
162 | << "}" << fSrc.nl << fSrc.nl; | 166 | << "}" << fSrc.nl << fSrc.nl; |
163 | 167 | ||
164 | fSrc << "Bu::StreamStack *Datafiles::openRaw( const Bu::String &sName )" << fSrc.nl | 168 | fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl |
165 | << "{" << fSrc.nl | 169 | << "{" << fSrc.nl |
166 | << "\tconst File &f = getFile( sName );" << fSrc.nl | 170 | << "\tconst File &f = getFile( sName );" << fSrc.nl |
167 | << "\treturn new Bu::StreamStack( new Bu::MemBuf( Bu::String( f.data, f.iSize ) ) );" << fSrc.nl | 171 | << "\treturn new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl |
168 | << "}" << fSrc.nl << fSrc.nl; | 172 | << "}" << fSrc.nl << fSrc.nl; |
169 | 173 | ||
170 | fSrc << "Bu::String Datafiles::getString( const Bu::String &sName )" << fSrc.nl | 174 | fSrc << "Bu::String " << opt.sClass << "::getString( const Bu::String &sName )" << fSrc.nl |
171 | << "{" << fSrc.nl | 175 | << "{" << fSrc.nl |
172 | << "\tconst File &f = getFile( sName );" << fSrc.nl | 176 | << "\tBu::StreamStack *ss = open( sName );" << fSrc.nl |
173 | << "\tBu::String s( f.data, f.iSize );" << fSrc.nl | 177 | << "\tBu::String s;" << fSrc.nl |
174 | << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl | 178 | << "\tchar buf[1024];" << fSrc.nl |
179 | << "\twhile( !ss->isEos() )" << fSrc.nl | ||
175 | << "\t{" << fSrc.nl | 180 | << "\t{" << fSrc.nl |
176 | << "\t\tswitch( *t )" << fSrc.nl | 181 | << "\t\ts.append( buf, ss->read( buf, 1024 ) );" << fSrc.nl |
177 | << "\t\t{" << fSrc.nl | ||
178 | << "\t\t\tcase 'd': s = Bu::decodeStr<Bu::Deflate>( s ); break;" << fSrc.nl | ||
179 | << "\t\t\tcase 'b': s = Bu::decodeStr<Bu::BZip2>( s ); break;" << fSrc.nl | ||
180 | << "\t\t\tcase '6': s = Bu::decodeStr<Bu::Base64>( s ); break;" << fSrc.nl | ||
181 | << "\t\t}" << fSrc.nl | ||
182 | << "\t}" << fSrc.nl | 182 | << "\t}" << fSrc.nl |
183 | << "\tdelete ss;" << fSrc.nl | ||
183 | << "\treturn s;" << fSrc.nl | 184 | << "\treturn s;" << fSrc.nl |
184 | << "}" << fSrc.nl << fSrc.nl; | 185 | << "}" << fSrc.nl << fSrc.nl; |
185 | 186 | ||
186 | fSrc << "Bu::String Datafiles::getStringRaw( const Bu::String &sName )" << fSrc.nl | 187 | fSrc << "Bu::String " << opt.sClass << "::getStringRaw( const Bu::String &sName )" << fSrc.nl |
187 | << "{" << fSrc.nl | 188 | << "{" << fSrc.nl |
188 | << "\tconst File &f = getFile( sName );" << fSrc.nl | 189 | << "\tconst File &f = getFile( sName );" << fSrc.nl |
189 | << "\treturn Bu::String( f.data, f.iSize );" << fSrc.nl | 190 | << "\treturn Bu::String( f.data, f.iSize );" << fSrc.nl |