From 10747b4fb6ff74fa83384859b09ba695378aea60 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 9 Feb 2012 05:30:38 +0000 Subject: Added StaticMemBuf and used it in bin2cpp. You can also set the name of the class that bin2cpp generates for you. --- src/staticmembuf.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/staticmembuf.h | 65 ++++++++++++++++++++++++ src/tools/bin2cpp.cpp | 49 +++++++++--------- 3 files changed, 225 insertions(+), 24 deletions(-) create mode 100644 src/staticmembuf.cpp create mode 100644 src/staticmembuf.h (limited to 'src') 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 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/staticmembuf.h" + +using namespace Bu; + +Bu::StaticMemBuf::StaticMemBuf( const void *pData, size iSize ) : + pData( pData ), + iSize( iSize ), + nPos( 0 ) +{ +} + +Bu::StaticMemBuf::~StaticMemBuf() +{ +} + +void Bu::StaticMemBuf::close() +{ +} + +size Bu::StaticMemBuf::read( void *pBuf, size nBytes ) +{ + if( iSize-nPos < nBytes ) + nBytes = iSize-nPos; + + memcpy( pBuf, ((char *)pData)+nPos, nBytes ); + nPos += nBytes; + + return nBytes; +} + +size Bu::StaticMemBuf::write( const void *, size ) +{ + return -1; +} + +size Bu::StaticMemBuf::tell() +{ + return nPos; +} + +void Bu::StaticMemBuf::seek( size offset ) +{ + nPos += offset; + if( nPos < 0 ) nPos = 0; + else if( nPos > iSize ) nPos = iSize; +} + +void Bu::StaticMemBuf::setPos( size pos ) +{ + nPos = pos; + if( nPos < 0 ) nPos = 0; + else if( nPos > iSize ) nPos = iSize; +} + +void Bu::StaticMemBuf::setPosEnd( size pos ) +{ + nPos = iSize-pos; + if( nPos < 0 ) nPos = 0; + else if( nPos > iSize ) nPos = iSize; +} + +bool Bu::StaticMemBuf::isEos() +{ + return (nPos == iSize); +} + +bool Bu::StaticMemBuf::isOpen() +{ + return true; +} + +void Bu::StaticMemBuf::flush() +{ +} + +bool Bu::StaticMemBuf::canRead() +{ + return !isEos(); +} + +bool Bu::StaticMemBuf::canWrite() +{ + return false; +} + +bool Bu::StaticMemBuf::isReadable() +{ + return true; +} + +bool Bu::StaticMemBuf::isWritable() +{ + return false; +} + +bool Bu::StaticMemBuf::isSeekable() +{ + return true; +} + +bool Bu::StaticMemBuf::isBlocking() +{ + return true; +} + +void Bu::StaticMemBuf::setBlocking( bool ) +{ +} + +void Bu::StaticMemBuf::setSize( size ) +{ +} + +Bu::size Bu::StaticMemBuf::getSize() const +{ + return iSize; +} + +Bu::size Bu::StaticMemBuf::getBlockSize() const +{ + return iSize; +} + +Bu::String Bu::StaticMemBuf::getLocation() const +{ + return ""; +} + 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 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_STATIC_MEM_BUF_H +#define BU_STATIC_MEM_BUF_H + +#include + +#include "bu/config.h" +#include "bu/stream.h" + +namespace Bu +{ + /** + * An immutable, read-only memory buffer. Construct this buffer around a + * block of raw memory, provide the length of the block, and you can read + * from that block via this class as though it were a normal stream. + * + * Use this class instead of MemBuf when you have a string already, and + * don't need to change it. MemBuf will make a copy of your string for + * it's own use (often) and this will not (ever). + *@ingroup Streams + */ + class StaticMemBuf : public Stream + { + public: + StaticMemBuf( const void *pData, size iSize ); + virtual ~StaticMemBuf(); + + virtual void close(); + virtual size read( void *pBuf, size iBytes ); + + virtual size write( const void *pBuf, size iBytes ); + using Stream::write; + virtual size tell(); + virtual void seek( size offset ); + virtual void setPos( size pos ); + virtual void setPosEnd( size pos ); + virtual bool isEos(); + virtual bool isOpen(); + virtual void flush(); + virtual bool canRead(); + virtual bool canWrite(); + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); + virtual bool isBlocking(); + virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( size iSize ); + virtual size getSize() const; + virtual size getBlockSize() const; + virtual Bu::String getLocation() const; + + private: + const void *pData; + size iSize; + size nPos; + }; +} + +#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 { public: Options( int argc, char *argv[] ) : - sOutBase("bin") + sClass("Datafiles") { - addOption( sOutBase, 'o', "Output base filename [default=\"bin\"]"); + 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"); setNonOption( slot(this, &Options::addInput) ); addHelpOption(); parse( argc, argv ); + + if( !sOutBase.isSet() ) + sOutBase = sClass.toLower(); } virtual ~Options() @@ -36,11 +40,11 @@ public: int addInput( Bu::StrArray aArgs ) { - sio << "Input: " << aArgs[0] << sio.nl; slInput.append( aArgs[0] ); return 0; } + Bu::String sClass; Bu::String sOutBase; Bu::StringList slInput; Bu::StringList slFilter; @@ -55,12 +59,12 @@ int main( int argc, char *argv[] ) Formatter fHdr( fHdrOut ); Formatter fSrc( fSrcOut ); - fHdr << "#ifndef BIN2CPP_DATAFILE_H" << fHdr.nl - << "#define BIN2CPP_DATAFILE_H" << fHdr.nl << fHdr.nl + fHdr << "#ifndef BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl + << "#define BIN2CPP_" << opt.sClass.toUpper() << "_H" << fHdr.nl << fHdr.nl << "#include " << fHdr.nl << "#include " << fHdr.nl << fHdr.nl - << "class Datafiles" << fHdr.nl + << "class " << opt.sClass << fHdr.nl << "{" << fHdr.nl << "public:" << fHdr.nl << "\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[] ) << "#include " << fSrc.nl << "#include " << fSrc.nl << "#include " << fSrc.nl - << "#include " << fSrc.nl << fSrc.nl - << "const Datafiles::File Datafiles::aFile[] = {" << fSrc.nl; + << "#include " << fSrc.nl << fSrc.nl + << "const " << opt.sClass << "::File " << opt.sClass << "::aFile[] = {" << fSrc.nl; for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) { @@ -128,7 +132,7 @@ int main( int argc, char *argv[] ) } fSrc << "};" << fSrc.nl << fSrc.nl; - fSrc << "const Datafiles::File &Datafiles::getFile( const Bu::String &sName )" + fSrc << "const " << opt.sClass << "::File &" << opt.sClass << "::getFile( const Bu::String &sName )" << fSrc.nl << "{" << fSrc.nl << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl @@ -145,10 +149,10 @@ int main( int argc, char *argv[] ) << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl << "}" << fSrc.nl << fSrc.nl; - fSrc << "Bu::StreamStack *Datafiles::open( const Bu::String &sName )" << fSrc.nl + 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::MemBuf( Bu::String( f.data, f.iSize ) ) );" << 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 @@ -161,29 +165,26 @@ int main( int argc, char *argv[] ) << "\treturn s;" << fSrc.nl << "}" << fSrc.nl << fSrc.nl; - fSrc << "Bu::StreamStack *Datafiles::openRaw( const Bu::String &sName )" << fSrc.nl + fSrc << "Bu::StreamStack *" << opt.sClass << "::openRaw( const Bu::String &sName )" << fSrc.nl << "{" << fSrc.nl << "\tconst File &f = getFile( sName );" << fSrc.nl - << "\treturn new Bu::StreamStack( new Bu::MemBuf( Bu::String( f.data, f.iSize ) ) );" << fSrc.nl + << "\treturn new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl << "}" << fSrc.nl << fSrc.nl; - fSrc << "Bu::String Datafiles::getString( const Bu::String &sName )" << fSrc.nl + fSrc << "Bu::String " << opt.sClass << "::getString( const Bu::String &sName )" << fSrc.nl << "{" << fSrc.nl - << "\tconst File &f = getFile( sName );" << fSrc.nl - << "\tBu::String s( f.data, f.iSize );" << fSrc.nl - << "\tfor( const char *t = f.flt; *t; t++ )" << fSrc.nl + << "\tBu::StreamStack *ss = open( sName );" << fSrc.nl + << "\tBu::String s;" << fSrc.nl + << "\tchar buf[1024];" << fSrc.nl + << "\twhile( !ss->isEos() )" << fSrc.nl << "\t{" << fSrc.nl - << "\t\tswitch( *t )" << fSrc.nl - << "\t\t{" << fSrc.nl - << "\t\t\tcase 'd': s = Bu::decodeStr( s ); break;" << fSrc.nl - << "\t\t\tcase 'b': s = Bu::decodeStr( s ); break;" << fSrc.nl - << "\t\t\tcase '6': s = Bu::decodeStr( s ); break;" << fSrc.nl - << "\t\t}" << fSrc.nl + << "\t\ts.append( buf, ss->read( buf, 1024 ) );" << fSrc.nl << "\t}" << fSrc.nl + << "\tdelete ss;" << fSrc.nl << "\treturn s;" << fSrc.nl << "}" << fSrc.nl << fSrc.nl; - fSrc << "Bu::String Datafiles::getStringRaw( const Bu::String &sName )" << fSrc.nl + fSrc << "Bu::String " << opt.sClass << "::getStringRaw( const Bu::String &sName )" << fSrc.nl << "{" << fSrc.nl << "\tconst File &f = getFile( sName );" << fSrc.nl << "\treturn Bu::String( f.data, f.iSize );" << fSrc.nl -- cgit v1.2.3