diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-02-09 03:55:59 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-02-09 03:55:59 +0000 |
commit | 5a6476514c075743ca13d566bd306b2d71d1952a (patch) | |
tree | d995359aa43c593992be6ec11147a20e2932e732 /src | |
parent | 4b1ab50fb061c5abe6727d031b7fec72bfa97c7d (diff) | |
download | libbu++-5a6476514c075743ca13d566bd306b2d71d1952a.tar.gz libbu++-5a6476514c075743ca13d566bd306b2d71d1952a.tar.bz2 libbu++-5a6476514c075743ca13d566bd306b2d71d1952a.tar.xz libbu++-5a6476514c075743ca13d566bd306b2d71d1952a.zip |
bin2cpp added. It's nearly done. I want to add a StaticMemBuf class to
libbu++ and then use that to minimize memory usage in the bin2cpp generated
classes. That should go really quickly.
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/bin2cpp.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/tools/bin2cpp.cpp b/src/tools/bin2cpp.cpp new file mode 100644 index 0000000..4a0befa --- /dev/null +++ b/src/tools/bin2cpp.cpp | |||
@@ -0,0 +1,194 @@ | |||
1 | #include <bu/sio.h> | ||
2 | #include <bu/optparser.h> | ||
3 | #include <bu/file.h> | ||
4 | #include <bu/deflate.h> | ||
5 | #include <bu/bzip2.h> | ||
6 | #include <bu/base64.h> | ||
7 | #include <bu/streamstack.h> | ||
8 | |||
9 | #include <bu/strfilter.h> | ||
10 | |||
11 | using namespace Bu; | ||
12 | |||
13 | class Options : public OptParser | ||
14 | { | ||
15 | public: | ||
16 | Options( int argc, char *argv[] ) : | ||
17 | sOutBase("bin") | ||
18 | { | ||
19 | addOption( sOutBase, 'o', "Output base filename [default=\"bin\"]"); | ||
20 | addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, base64"); | ||
21 | setNonOption( slot(this, &Options::addInput) ); | ||
22 | addHelpOption(); | ||
23 | |||
24 | parse( argc, argv ); | ||
25 | } | ||
26 | |||
27 | virtual ~Options() | ||
28 | { | ||
29 | } | ||
30 | |||
31 | int addFilter( Bu::StrArray aArgs ) | ||
32 | { | ||
33 | slFilter.append( aArgs[1] ); | ||
34 | return 1; | ||
35 | } | ||
36 | |||
37 | int addInput( Bu::StrArray aArgs ) | ||
38 | { | ||
39 | sio << "Input: " << aArgs[0] << sio.nl; | ||
40 | slInput.append( aArgs[0] ); | ||
41 | return 0; | ||
42 | } | ||
43 | |||
44 | Bu::String sOutBase; | ||
45 | Bu::StringList slInput; | ||
46 | Bu::StringList slFilter; | ||
47 | }; | ||
48 | |||
49 | int main( int argc, char *argv[] ) | ||
50 | { | ||
51 | Options opt( argc, argv ); | ||
52 | |||
53 | File fHdrOut( opt.sOutBase + ".h", File::WriteNew ); | ||
54 | File fSrcOut( opt.sOutBase + ".cpp", File::WriteNew ); | ||
55 | |||
56 | Formatter fHdr( fHdrOut ); | ||
57 | Formatter fSrc( fSrcOut ); | ||
58 | fHdr << "#ifndef BIN2CPP_DATAFILE_H" << fHdr.nl | ||
59 | << "#define BIN2CPP_DATAFILE_H" << fHdr.nl << fHdr.nl | ||
60 | << "#include <bu/string.h>" << fHdr.nl | ||
61 | << "#include <bu/streamstack.h>" << fHdr.nl | ||
62 | << fHdr.nl | ||
63 | << "class Datafiles" << fHdr.nl | ||
64 | << "{" << fHdr.nl | ||
65 | << "public:" << fHdr.nl | ||
66 | << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl | ||
67 | << "\tstatic const File &getFile( const Bu::String &sName );" << fHdr.nl | ||
68 | << "\tstatic Bu::StreamStack *open( const Bu::String &sName );" << fHdr.nl | ||
69 | << "\tstatic Bu::StreamStack *openRaw( const Bu::String &sName );" << fHdr.nl | ||
70 | << "\tstatic Bu::String getString( const Bu::String &sName );" << fHdr.nl | ||
71 | << "\tstatic Bu::String getStringRaw( const Bu::String &sName );" << fHdr.nl | ||
72 | << fHdr.nl; | ||
73 | fHdr << "public:" << fHdr.nl | ||
74 | << "\tstatic const File aFile[];" << fHdr.nl | ||
75 | << "};" << fHdr.nl << fHdr.nl; | ||
76 | fHdr << "#endif"; | ||
77 | |||
78 | fSrc << "#include \"" << opt.sOutBase << ".h\"" << fSrc.nl | ||
79 | << "#include <bu/deflate.h>" << fSrc.nl | ||
80 | << "#include <bu/bzip2.h>" << fSrc.nl | ||
81 | << "#include <bu/base64.h>" << fSrc.nl | ||
82 | << "#include <bu/strfilter.h>" << fSrc.nl | ||
83 | << "#include <bu/membuf.h>" << fSrc.nl << fSrc.nl | ||
84 | << "const Datafiles::File Datafiles::aFile[] = {" << fSrc.nl; | ||
85 | |||
86 | for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) | ||
87 | { | ||
88 | File fIn( *i, File::Read ); | ||
89 | Bu::String sDat; | ||
90 | char buf[1024]; | ||
91 | while( !fIn.isEos() ) | ||
92 | { | ||
93 | sDat.append( buf, fIn.read( buf, 1024 ) ); | ||
94 | } | ||
95 | |||
96 | Bu::String sFltDesc; | ||
97 | for( Bu::StringList::iterator f = opt.slFilter.begin(); f; f++ ) | ||
98 | { | ||
99 | if( *f == "deflate" ) | ||
100 | { | ||
101 | sDat = encodeStr<Deflate>( sDat ); | ||
102 | sFltDesc.prepend("d"); | ||
103 | } | ||
104 | else if( *f == "bzip2" ) | ||
105 | { | ||
106 | sDat = encodeStr<BZip2>( sDat ); | ||
107 | sFltDesc.prepend("b"); | ||
108 | } | ||
109 | else if( *f == "base64" ) | ||
110 | { | ||
111 | sDat = encodeStr<Base64>( sDat ); | ||
112 | sFltDesc.prepend("6"); | ||
113 | } | ||
114 | else | ||
115 | { | ||
116 | sio << "No known filter named " << *f << sio.nl; | ||
117 | return 1; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | fSrc << " {" << sDat.getSize() << ", \""; | ||
122 | |||
123 | for( Bu::String::iterator j = sDat.begin(); j; j++ ) | ||
124 | { | ||
125 | fSrc << "\\x" << Fmt::hex() << (unsigned char)*j; | ||
126 | } | ||
127 | fSrc << "\", \"" << sFltDesc << "\"}," << fSrc.nl; | ||
128 | } | ||
129 | fSrc << "};" << fSrc.nl << fSrc.nl; | ||
130 | |||
131 | fSrc << "const Datafiles::File &Datafiles::getFile( const Bu::String &sName )" | ||
132 | << fSrc.nl | ||
133 | << "{" << fSrc.nl | ||
134 | << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl | ||
135 | << "\t{" << fSrc.nl; | ||
136 | |||
137 | int idx = 0; | ||
138 | for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) | ||
139 | { | ||
140 | fSrc << "\t\tcase " << Bu::__calcHashCode( *i ) << "UL:" << fSrc.nl | ||
141 | << "\t\t\treturn aFile[" << idx << "];" << fSrc.nl; | ||
142 | idx++; | ||
143 | } | ||
144 | fSrc << "\t}" << fSrc.nl | ||
145 | << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl | ||
146 | << "}" << fSrc.nl << fSrc.nl; | ||
147 | |||
148 | fSrc << "Bu::StreamStack *Datafiles::open( const Bu::String &sName )" << fSrc.nl | ||
149 | << "{" << fSrc.nl | ||
150 | << "\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 | ||
152 | << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl | ||
153 | << "\t{" << fSrc.nl | ||
154 | << "\t\tswitch( *t )" << fSrc.nl | ||
155 | << "\t\t{" << fSrc.nl | ||
156 | << "\t\t\tcase 'd': s->pushFilter<Bu::Deflate>(); break;" << fSrc.nl | ||
157 | << "\t\t\tcase 'b': s->pushFilter<Bu::BZip2>(); break;" << fSrc.nl | ||
158 | << "\t\t\tcase '6': s->pushFilter<Bu::Base64>(); break;" << fSrc.nl | ||
159 | << "\t\t}" << fSrc.nl | ||
160 | << "\t}" << fSrc.nl | ||
161 | << "\treturn s;" << fSrc.nl | ||
162 | << "}" << fSrc.nl << fSrc.nl; | ||
163 | |||
164 | fSrc << "Bu::StreamStack *Datafiles::openRaw( const Bu::String &sName )" << fSrc.nl | ||
165 | << "{" << fSrc.nl | ||
166 | << "\tconst File &f = getFile( sName );" << fSrc.nl | ||
167 | << "\treturn new Bu::StreamStack( new Bu::MemBuf( Bu::String( f.data, f.iSize ) ) );" << fSrc.nl | ||
168 | << "}" << fSrc.nl << fSrc.nl; | ||
169 | |||
170 | fSrc << "Bu::String Datafiles::getString( const Bu::String &sName )" << fSrc.nl | ||
171 | << "{" << fSrc.nl | ||
172 | << "\tconst File &f = getFile( sName );" << fSrc.nl | ||
173 | << "\tBu::String s( f.data, f.iSize );" << fSrc.nl | ||
174 | << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl | ||
175 | << "\t{" << fSrc.nl | ||
176 | << "\t\tswitch( *t )" << 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 | ||
183 | << "\treturn s;" << fSrc.nl | ||
184 | << "}" << fSrc.nl << fSrc.nl; | ||
185 | |||
186 | fSrc << "Bu::String Datafiles::getStringRaw( const Bu::String &sName )" << fSrc.nl | ||
187 | << "{" << fSrc.nl | ||
188 | << "\tconst File &f = getFile( sName );" << fSrc.nl | ||
189 | << "\treturn Bu::String( f.data, f.iSize );" << fSrc.nl | ||
190 | << "}" << fSrc.nl << fSrc.nl; | ||
191 | |||
192 | return 0; | ||
193 | } | ||
194 | |||