diff options
Diffstat (limited to '')
-rw-r--r-- | src/staticmembuf.cpp | 135 | ||||
-rw-r--r-- | src/staticmembuf.h | 65 | ||||
-rw-r--r-- | src/tools/bin2cpp.cpp | 49 |
3 files changed, 225 insertions, 24 deletions
diff --git a/src/staticmembuf.cpp b/src/staticmembuf.cpp new file mode 100644 index 0000000..74fae31 --- /dev/null +++ b/src/staticmembuf.cpp | |||
@@ -0,0 +1,135 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/staticmembuf.h" | ||
9 | |||
10 | using namespace Bu; | ||
11 | |||
12 | Bu::StaticMemBuf::StaticMemBuf( const void *pData, size iSize ) : | ||
13 | pData( pData ), | ||
14 | iSize( iSize ), | ||
15 | nPos( 0 ) | ||
16 | { | ||
17 | } | ||
18 | |||
19 | Bu::StaticMemBuf::~StaticMemBuf() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | void Bu::StaticMemBuf::close() | ||
24 | { | ||
25 | } | ||
26 | |||
27 | size Bu::StaticMemBuf::read( void *pBuf, size nBytes ) | ||
28 | { | ||
29 | if( iSize-nPos < nBytes ) | ||
30 | nBytes = iSize-nPos; | ||
31 | |||
32 | memcpy( pBuf, ((char *)pData)+nPos, nBytes ); | ||
33 | nPos += nBytes; | ||
34 | |||
35 | return nBytes; | ||
36 | } | ||
37 | |||
38 | size Bu::StaticMemBuf::write( const void *, size ) | ||
39 | { | ||
40 | return -1; | ||
41 | } | ||
42 | |||
43 | size Bu::StaticMemBuf::tell() | ||
44 | { | ||
45 | return nPos; | ||
46 | } | ||
47 | |||
48 | void Bu::StaticMemBuf::seek( size offset ) | ||
49 | { | ||
50 | nPos += offset; | ||
51 | if( nPos < 0 ) nPos = 0; | ||
52 | else if( nPos > iSize ) nPos = iSize; | ||
53 | } | ||
54 | |||
55 | void Bu::StaticMemBuf::setPos( size pos ) | ||
56 | { | ||
57 | nPos = pos; | ||
58 | if( nPos < 0 ) nPos = 0; | ||
59 | else if( nPos > iSize ) nPos = iSize; | ||
60 | } | ||
61 | |||
62 | void Bu::StaticMemBuf::setPosEnd( size pos ) | ||
63 | { | ||
64 | nPos = iSize-pos; | ||
65 | if( nPos < 0 ) nPos = 0; | ||
66 | else if( nPos > iSize ) nPos = iSize; | ||
67 | } | ||
68 | |||
69 | bool Bu::StaticMemBuf::isEos() | ||
70 | { | ||
71 | return (nPos == iSize); | ||
72 | } | ||
73 | |||
74 | bool Bu::StaticMemBuf::isOpen() | ||
75 | { | ||
76 | return true; | ||
77 | } | ||
78 | |||
79 | void Bu::StaticMemBuf::flush() | ||
80 | { | ||
81 | } | ||
82 | |||
83 | bool Bu::StaticMemBuf::canRead() | ||
84 | { | ||
85 | return !isEos(); | ||
86 | } | ||
87 | |||
88 | bool Bu::StaticMemBuf::canWrite() | ||
89 | { | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | bool Bu::StaticMemBuf::isReadable() | ||
94 | { | ||
95 | return true; | ||
96 | } | ||
97 | |||
98 | bool Bu::StaticMemBuf::isWritable() | ||
99 | { | ||
100 | return false; | ||
101 | } | ||
102 | |||
103 | bool Bu::StaticMemBuf::isSeekable() | ||
104 | { | ||
105 | return true; | ||
106 | } | ||
107 | |||
108 | bool Bu::StaticMemBuf::isBlocking() | ||
109 | { | ||
110 | return true; | ||
111 | } | ||
112 | |||
113 | void Bu::StaticMemBuf::setBlocking( bool ) | ||
114 | { | ||
115 | } | ||
116 | |||
117 | void Bu::StaticMemBuf::setSize( size ) | ||
118 | { | ||
119 | } | ||
120 | |||
121 | Bu::size Bu::StaticMemBuf::getSize() const | ||
122 | { | ||
123 | return iSize; | ||
124 | } | ||
125 | |||
126 | Bu::size Bu::StaticMemBuf::getBlockSize() const | ||
127 | { | ||
128 | return iSize; | ||
129 | } | ||
130 | |||
131 | Bu::String Bu::StaticMemBuf::getLocation() const | ||
132 | { | ||
133 | return ""; | ||
134 | } | ||
135 | |||
diff --git a/src/staticmembuf.h b/src/staticmembuf.h new file mode 100644 index 0000000..f168de3 --- /dev/null +++ b/src/staticmembuf.h | |||
@@ -0,0 +1,65 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_STATIC_MEM_BUF_H | ||
9 | #define BU_STATIC_MEM_BUF_H | ||
10 | |||
11 | #include <stdint.h> | ||
12 | |||
13 | #include "bu/config.h" | ||
14 | #include "bu/stream.h" | ||
15 | |||
16 | namespace Bu | ||
17 | { | ||
18 | /** | ||
19 | * An immutable, read-only memory buffer. Construct this buffer around a | ||
20 | * block of raw memory, provide the length of the block, and you can read | ||
21 | * from that block via this class as though it were a normal stream. | ||
22 | * | ||
23 | * Use this class instead of MemBuf when you have a string already, and | ||
24 | * don't need to change it. MemBuf will make a copy of your string for | ||
25 | * it's own use (often) and this will not (ever). | ||
26 | *@ingroup Streams | ||
27 | */ | ||
28 | class StaticMemBuf : public Stream | ||
29 | { | ||
30 | public: | ||
31 | StaticMemBuf( const void *pData, size iSize ); | ||
32 | virtual ~StaticMemBuf(); | ||
33 | |||
34 | virtual void close(); | ||
35 | virtual size read( void *pBuf, size iBytes ); | ||
36 | |||
37 | virtual size write( const void *pBuf, size iBytes ); | ||
38 | using Stream::write; | ||
39 | virtual size tell(); | ||
40 | virtual void seek( size offset ); | ||
41 | virtual void setPos( size pos ); | ||
42 | virtual void setPosEnd( size pos ); | ||
43 | virtual bool isEos(); | ||
44 | virtual bool isOpen(); | ||
45 | virtual void flush(); | ||
46 | virtual bool canRead(); | ||
47 | virtual bool canWrite(); | ||
48 | virtual bool isReadable(); | ||
49 | virtual bool isWritable(); | ||
50 | virtual bool isSeekable(); | ||
51 | virtual bool isBlocking(); | ||
52 | virtual void setBlocking( bool bBlocking=true ); | ||
53 | virtual void setSize( size iSize ); | ||
54 | virtual size getSize() const; | ||
55 | virtual size getBlockSize() const; | ||
56 | virtual Bu::String getLocation() const; | ||
57 | |||
58 | private: | ||
59 | const void *pData; | ||
60 | size iSize; | ||
61 | size nPos; | ||
62 | }; | ||
63 | } | ||
64 | |||
65 | #endif | ||
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 |