From 7d7bd049ed6815d3d2bac03331ff56102d224580 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 9 Feb 2012 15:59:43 +0000 Subject: May as well involve all the filters. No point in including lzma until we can link it in in windows though. --- src/tools/bin2cpp.cpp | 54 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/tools/bin2cpp.cpp b/src/tools/bin2cpp.cpp index 2c227f5..53da0b5 100644 --- a/src/tools/bin2cpp.cpp +++ b/src/tools/bin2cpp.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,8 @@ public: { addOption( sClass, 'c', "Class name [default=\"Datafiles\"]"); addOption( sOutBase, 'o', "Output base filename [defaults to classname]"); - addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64"); + addOption( sOutDir, 'd', "Output directory [defaults to current dir]"); + addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64, hex"); setNonOption( slot(this, &Options::addInput) ); addHelpOption(); @@ -26,6 +28,8 @@ public: if( !sOutBase.isSet() ) sOutBase = sClass.toLower(); + if( sOutDir.isSet() ) + sOutDir += "/"; } virtual ~Options() @@ -46,6 +50,7 @@ public: Bu::String sClass; Bu::String sOutBase; + Bu::String sOutDir; Bu::StringList slInput; Bu::StringList slFilter; }; @@ -54,8 +59,10 @@ int main( int argc, char *argv[] ) { Options opt( argc, argv ); - File fHdrOut( opt.sOutBase + ".h", File::WriteNew ); - File fSrcOut( opt.sOutBase + ".cpp", File::WriteNew ); + File fHdrOut( opt.sOutDir + opt.sOutBase + ".h", File::WriteNew ); + File fSrcOut( opt.sOutDir + opt.sOutBase + ".cpp", File::WriteNew ); + + Bu::Hash hFilters; Formatter fHdr( fHdrOut ); Formatter fSrc( fSrcOut ); @@ -83,6 +90,7 @@ int main( int argc, char *argv[] ) << "#include " << fSrc.nl << "#include " << fSrc.nl << "#include " << fSrc.nl + << "#include " << fSrc.nl << "#include " << fSrc.nl << "#include " << fSrc.nl << fSrc.nl << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl; @@ -104,16 +112,25 @@ int main( int argc, char *argv[] ) { sDat = encodeStr( sDat ); sFltDesc.prepend("d"); + hFilters.insert('d', true ); } else if( *f == "bzip2" ) { sDat = encodeStr( sDat ); sFltDesc.prepend("b"); + hFilters.insert('b', true ); } else if( *f == "base64" ) { sDat = encodeStr( sDat ); sFltDesc.prepend("6"); + hFilters.insert('6', true ); + } + else if( *f == "hex" ) + { + sDat = encodeStr( sDat ); + sFltDesc.prepend("h"); + hFilters.insert('h', true ); } else { @@ -152,17 +169,26 @@ int main( int argc, char *argv[] ) fSrc << "Bu::StreamStack *" << opt.sClass << "::open( const Bu::String &sName )" << fSrc.nl << "{" << fSrc.nl << "\tconst File &f = getFile( sName );" << fSrc.nl - << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl - << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl - << "\t{" << fSrc.nl - << "\t\tswitch( *t )" << fSrc.nl - << "\t\t{" << fSrc.nl - << "\t\t\tcase 'd': s->pushFilter(); break;" << fSrc.nl - << "\t\t\tcase 'b': s->pushFilter(); break;" << fSrc.nl - << "\t\t\tcase '6': s->pushFilter(); break;" << fSrc.nl - << "\t\t}" << fSrc.nl - << "\t}" << fSrc.nl - << "\treturn s;" << fSrc.nl + << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl; + + if( !hFilters.isEmpty() ) + { + fSrc << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl + << "\t{" << fSrc.nl + << "\t\tswitch( *t )" << fSrc.nl + << "\t\t{" << fSrc.nl; + if( hFilters.has('d') ) + fSrc << "\t\t\tcase 'd': s->pushFilter(); break;" << fSrc.nl; + if( hFilters.has('b') ) + fSrc << "\t\t\tcase 'b': s->pushFilter(); break;" << fSrc.nl; + if( hFilters.has('6') ) + fSrc << "\t\t\tcase '6': s->pushFilter(); break;" << fSrc.nl; + if( hFilters.has('h') ) + fSrc << "\t\t\tcase 'h': s->pushFilter(); break;" << fSrc.nl; + fSrc << "\t\t}" << fSrc.nl + << "\t}" << fSrc.nl; + } + fSrc << "\treturn s;" << fSrc.nl << "}" << fSrc.nl << fSrc.nl; fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl -- cgit v1.2.3