aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tools/bin2cpp.cpp54
1 files changed, 40 insertions, 14 deletions
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 @@
4#include <bu/deflate.h> 4#include <bu/deflate.h>
5#include <bu/bzip2.h> 5#include <bu/bzip2.h>
6#include <bu/base64.h> 6#include <bu/base64.h>
7#include <bu/hex.h>
7#include <bu/streamstack.h> 8#include <bu/streamstack.h>
8 9
9#include <bu/strfilter.h> 10#include <bu/strfilter.h>
@@ -18,7 +19,8 @@ public:
18 { 19 {
19 addOption( sClass, 'c', "Class name [default=\"Datafiles\"]"); 20 addOption( sClass, 'c', "Class name [default=\"Datafiles\"]");
20 addOption( sOutBase, 'o', "Output base filename [defaults to classname]"); 21 addOption( sOutBase, 'o', "Output base filename [defaults to classname]");
21 addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64"); 22 addOption( sOutDir, 'd', "Output directory [defaults to current dir]");
23 addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64, hex");
22 setNonOption( slot(this, &Options::addInput) ); 24 setNonOption( slot(this, &Options::addInput) );
23 addHelpOption(); 25 addHelpOption();
24 26
@@ -26,6 +28,8 @@ public:
26 28
27 if( !sOutBase.isSet() ) 29 if( !sOutBase.isSet() )
28 sOutBase = sClass.toLower(); 30 sOutBase = sClass.toLower();
31 if( sOutDir.isSet() )
32 sOutDir += "/";
29 } 33 }
30 34
31 virtual ~Options() 35 virtual ~Options()
@@ -46,6 +50,7 @@ public:
46 50
47 Bu::String sClass; 51 Bu::String sClass;
48 Bu::String sOutBase; 52 Bu::String sOutBase;
53 Bu::String sOutDir;
49 Bu::StringList slInput; 54 Bu::StringList slInput;
50 Bu::StringList slFilter; 55 Bu::StringList slFilter;
51}; 56};
@@ -54,8 +59,10 @@ int main( int argc, char *argv[] )
54{ 59{
55 Options opt( argc, argv ); 60 Options opt( argc, argv );
56 61
57 File fHdrOut( opt.sOutBase + ".h", File::WriteNew ); 62 File fHdrOut( opt.sOutDir + opt.sOutBase + ".h", File::WriteNew );
58 File fSrcOut( opt.sOutBase + ".cpp", File::WriteNew ); 63 File fSrcOut( opt.sOutDir + opt.sOutBase + ".cpp", File::WriteNew );
64
65 Bu::Hash<char, bool> hFilters;
59 66
60 Formatter fHdr( fHdrOut ); 67 Formatter fHdr( fHdrOut );
61 Formatter fSrc( fSrcOut ); 68 Formatter fSrc( fSrcOut );
@@ -83,6 +90,7 @@ int main( int argc, char *argv[] )
83 << "#include <bu/deflate.h>" << fSrc.nl 90 << "#include <bu/deflate.h>" << fSrc.nl
84 << "#include <bu/bzip2.h>" << fSrc.nl 91 << "#include <bu/bzip2.h>" << fSrc.nl
85 << "#include <bu/base64.h>" << fSrc.nl 92 << "#include <bu/base64.h>" << fSrc.nl
93 << "#include <bu/hex.h>" << fSrc.nl
86 << "#include <bu/strfilter.h>" << fSrc.nl 94 << "#include <bu/strfilter.h>" << fSrc.nl
87 << "#include <bu/staticmembuf.h>" << fSrc.nl << fSrc.nl 95 << "#include <bu/staticmembuf.h>" << fSrc.nl << fSrc.nl
88 << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl; 96 << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl;
@@ -104,16 +112,25 @@ int main( int argc, char *argv[] )
104 { 112 {
105 sDat = encodeStr<Deflate>( sDat ); 113 sDat = encodeStr<Deflate>( sDat );
106 sFltDesc.prepend("d"); 114 sFltDesc.prepend("d");
115 hFilters.insert('d', true );
107 } 116 }
108 else if( *f == "bzip2" ) 117 else if( *f == "bzip2" )
109 { 118 {
110 sDat = encodeStr<BZip2>( sDat ); 119 sDat = encodeStr<BZip2>( sDat );
111 sFltDesc.prepend("b"); 120 sFltDesc.prepend("b");
121 hFilters.insert('b', true );
112 } 122 }
113 else if( *f == "base64" ) 123 else if( *f == "base64" )
114 { 124 {
115 sDat = encodeStr<Base64>( sDat ); 125 sDat = encodeStr<Base64>( sDat );
116 sFltDesc.prepend("6"); 126 sFltDesc.prepend("6");
127 hFilters.insert('6', true );
128 }
129 else if( *f == "hex" )
130 {
131 sDat = encodeStr<Hex>( sDat );
132 sFltDesc.prepend("h");
133 hFilters.insert('h', true );
117 } 134 }
118 else 135 else
119 { 136 {
@@ -152,17 +169,26 @@ int main( int argc, char *argv[] )
152 fSrc << "Bu::StreamStack *" << opt.sClass << "::open( const Bu::String &sName )" << fSrc.nl 169 fSrc << "Bu::StreamStack *" << opt.sClass << "::open( const Bu::String &sName )" << fSrc.nl
153 << "{" << fSrc.nl 170 << "{" << fSrc.nl
154 << "\tconst File &f = getFile( sName );" << fSrc.nl 171 << "\tconst File &f = getFile( sName );" << fSrc.nl
155 << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl 172 << "\tBu::StreamStack *s = new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl;
156 << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl 173
157 << "\t{" << fSrc.nl 174 if( !hFilters.isEmpty() )
158 << "\t\tswitch( *t )" << fSrc.nl 175 {
159 << "\t\t{" << fSrc.nl 176 fSrc << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl
160 << "\t\t\tcase 'd': s->pushFilter<Bu::Deflate>(); break;" << fSrc.nl 177 << "\t{" << fSrc.nl
161 << "\t\t\tcase 'b': s->pushFilter<Bu::BZip2>(); break;" << fSrc.nl 178 << "\t\tswitch( *t )" << fSrc.nl
162 << "\t\t\tcase '6': s->pushFilter<Bu::Base64>(); break;" << fSrc.nl 179 << "\t\t{" << fSrc.nl;
163 << "\t\t}" << fSrc.nl 180 if( hFilters.has('d') )
164 << "\t}" << fSrc.nl 181 fSrc << "\t\t\tcase 'd': s->pushFilter<Bu::Deflate>(); break;" << fSrc.nl;
165 << "\treturn s;" << fSrc.nl 182 if( hFilters.has('b') )
183 fSrc << "\t\t\tcase 'b': s->pushFilter<Bu::BZip2>(); break;" << fSrc.nl;
184 if( hFilters.has('6') )
185 fSrc << "\t\t\tcase '6': s->pushFilter<Bu::Base64>(); break;" << fSrc.nl;
186 if( hFilters.has('h') )
187 fSrc << "\t\t\tcase 'h': s->pushFilter<Bu::Hex>(); break;" << fSrc.nl;
188 fSrc << "\t\t}" << fSrc.nl
189 << "\t}" << fSrc.nl;
190 }
191 fSrc << "\treturn s;" << fSrc.nl
166 << "}" << fSrc.nl << fSrc.nl; 192 << "}" << fSrc.nl << fSrc.nl;
167 193
168 fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl 194 fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl