From ec05778d5718a7912e506764d443a78d6a6179e3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 5 Nov 2012 22:41:51 +0000 Subject: Converted tabs to spaces with tabconv. --- src/tools/bin2cpp.cpp | 414 +++++++++++----------- src/tools/mkunit.cpp | 962 +++++++++++++++++++++++++------------------------- src/tools/myriad.cpp | 438 +++++++++++------------ src/tools/viewcsv.cpp | 868 ++++++++++++++++++++++----------------------- 4 files changed, 1341 insertions(+), 1341 deletions(-) (limited to 'src/tools') diff --git a/src/tools/bin2cpp.cpp b/src/tools/bin2cpp.cpp index 0b822f0..4214b34 100644 --- a/src/tools/bin2cpp.cpp +++ b/src/tools/bin2cpp.cpp @@ -16,244 +16,244 @@ using namespace Bu; class Options : public OptParser { public: - Options( int argc, char *argv[] ) : - sClass("Datafiles") - { - addHelpBanner("bin2cpp - convert files into executable-embeddable C++ code.\n"); - addHelpBanner("Each file in the input is loaded, filtered according to your options, and written as stack allocated, static variables in a generated class. You can then access the files as though they were on disk through that class."); - addHelpBanner("\nUsage: bin2cpp [options] [input1] [input2] [...] [inputN]"); - addHelpBanner( " Or: bin2cpp -s \n"); - addOption( sClass, 'c', "Class name [default=\"Datafiles\"]"); - addOption( sOutBase, 'o', "Output base filename [defaults to classname]"); - addOption( sOutDir, 'd', "Output directory [defaults to current dir]"); - addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, lzma, base64, hex"); - addOption( sSpecFile, 's', "Use the specified spec file instead of providing options on the command line. If you use this option all others are ignored."); - setNonOption( slot(this, &Options::addInput) ); - addHelpOption(); + Options( int argc, char *argv[] ) : + sClass("Datafiles") + { + addHelpBanner("bin2cpp - convert files into executable-embeddable C++ code.\n"); + addHelpBanner("Each file in the input is loaded, filtered according to your options, and written as stack allocated, static variables in a generated class. You can then access the files as though they were on disk through that class."); + addHelpBanner("\nUsage: bin2cpp [options] [input1] [input2] [...] [inputN]"); + addHelpBanner( " Or: bin2cpp -s \n"); + addOption( sClass, 'c', "Class name [default=\"Datafiles\"]"); + addOption( sOutBase, 'o', "Output base filename [defaults to classname]"); + addOption( sOutDir, 'd', "Output directory [defaults to current dir]"); + addOption( slot(this, &Options::addFilter), 'f', "Add filter: deflate, bzip2, lzma, base64, hex"); + addOption( sSpecFile, 's', "Use the specified spec file instead of providing options on the command line. If you use this option all others are ignored."); + setNonOption( slot(this, &Options::addInput) ); + addHelpOption(); - parse( argc, argv ); + parse( argc, argv ); - if( !sOutBase.isSet() ) - sOutBase = sClass.toLower(); - if( sOutDir.isSet() ) - sOutDir += "/"; - } + if( !sOutBase.isSet() ) + sOutBase = sClass.toLower(); + if( sOutDir.isSet() ) + sOutDir += "/"; + } - virtual ~Options() - { - } + virtual ~Options() + { + } - int addFilter( Bu::StrArray aArgs ) - { - slFilter.append( aArgs[1] ); - return 1; - } + int addFilter( Bu::StrArray aArgs ) + { + slFilter.append( aArgs[1] ); + return 1; + } - int addInput( Bu::StrArray aArgs ) - { - slInput.append( aArgs[0] ); - return 0; - } + int addInput( Bu::StrArray aArgs ) + { + slInput.append( aArgs[0] ); + return 0; + } - Bu::String sClass; - Bu::String sOutBase; - Bu::String sOutDir; - Bu::StringList slInput; - Bu::StringList slFilter; - Bu::String sSpecFile; + Bu::String sClass; + Bu::String sOutBase; + Bu::String sOutDir; + Bu::StringList slInput; + Bu::StringList slFilter; + Bu::String sSpecFile; }; int main( int argc, char *argv[] ) { - Options opt( argc, argv ); + Options opt( argc, argv ); - if( !opt.sSpecFile.isEmpty() ) - { - Bu::File fTaf( opt.sSpecFile, Bu::File::Read ); - Bu::TafReader rTaf( fTaf ); - Bu::TafGroup *pRoot = rTaf.readGroup(); + if( !opt.sSpecFile.isEmpty() ) + { + Bu::File fTaf( opt.sSpecFile, Bu::File::Read ); + Bu::TafReader rTaf( fTaf ); + Bu::TafGroup *pRoot = rTaf.readGroup(); - if( pRoot == NULL || pRoot->getName() != "bin2cpp" ) - { - sio << "Specfied spec file does not appear to be a bin2cpp taf " - "specifications file." << sio.nl; - return 5; - } + if( pRoot == NULL || pRoot->getName() != "bin2cpp" ) + { + sio << "Specfied spec file does not appear to be a bin2cpp taf " + "specifications file." << sio.nl; + return 5; + } - opt.sOutBase = opt.sClass = pRoot->getProperty("class"); - if( pRoot->hasProperty("output") ) - opt.sOutBase = pRoot->getProperty("output"); - opt.sOutDir = pRoot->getProperty("dir", ".") + "/"; + opt.sOutBase = opt.sClass = pRoot->getProperty("class"); + if( pRoot->hasProperty("output") ) + opt.sOutBase = pRoot->getProperty("output"); + opt.sOutDir = pRoot->getProperty("dir", ".") + "/"; - delete pRoot; - } + delete pRoot; + } - File fHdrOut( opt.sOutDir + opt.sOutBase + ".h", File::WriteNew ); - File fSrcOut( opt.sOutDir + 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; + Bu::Hash hFilters; - Formatter fHdr( fHdrOut ); - Formatter fSrc( fSrcOut ); - 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 " << opt.sClass << fHdr.nl - << "{" << fHdr.nl - << "public:" << fHdr.nl - << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl - << "\tstatic const File &getFile( const Bu::String &sName );" << fHdr.nl - << "\tstatic Bu::StreamStack *open( const Bu::String &sName );" << fHdr.nl - << "\tstatic Bu::StreamStack *openRaw( const Bu::String &sName );" << fHdr.nl - << "\tstatic Bu::String getString( const Bu::String &sName );" << fHdr.nl - << "\tstatic Bu::String getStringRaw( const Bu::String &sName );" << fHdr.nl - << fHdr.nl; - fHdr << "public:" << fHdr.nl - << "\tstatic const File aFile[];" << fHdr.nl - << "};" << fHdr.nl << fHdr.nl; - fHdr << "#endif"; + Formatter fHdr( fHdrOut ); + Formatter fSrc( fSrcOut ); + 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 " << opt.sClass << fHdr.nl + << "{" << fHdr.nl + << "public:" << fHdr.nl + << "\tclass File { public: int iSize; const char *data; const char *flt; };" << fHdr.nl << fHdr.nl + << "\tstatic const File &getFile( const Bu::String &sName );" << fHdr.nl + << "\tstatic Bu::StreamStack *open( const Bu::String &sName );" << fHdr.nl + << "\tstatic Bu::StreamStack *openRaw( const Bu::String &sName );" << fHdr.nl + << "\tstatic Bu::String getString( const Bu::String &sName );" << fHdr.nl + << "\tstatic Bu::String getStringRaw( const Bu::String &sName );" << fHdr.nl + << fHdr.nl; + fHdr << "public:" << fHdr.nl + << "\tstatic const File aFile[];" << fHdr.nl + << "};" << fHdr.nl << fHdr.nl; + fHdr << "#endif"; - fSrc << "#include \"" << opt.sOutBase << ".h\"" << fSrc.nl - << "#include " << fSrc.nl - << "#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; + fSrc << "#include \"" << opt.sOutBase << ".h\"" << fSrc.nl + << "#include " << fSrc.nl + << "#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; - for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) - { - File fIn( *i, File::Read ); - Bu::String sDat; - char buf[1024]; - while( !fIn.isEos() ) - { - sDat.append( buf, fIn.read( buf, 1024 ) ); - } + for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) + { + File fIn( *i, File::Read ); + Bu::String sDat; + char buf[1024]; + while( !fIn.isEos() ) + { + sDat.append( buf, fIn.read( buf, 1024 ) ); + } - Bu::String sFltDesc; - for( Bu::StringList::iterator f = opt.slFilter.begin(); f; f++ ) - { - if( *f == "deflate" ) - { - 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 == "lzma" ) - { - sDat = encodeStr( sDat ); - sFltDesc.prepend("l"); - hFilters.insert('l', 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 - { - sio << "No known filter named " << *f << sio.nl; - return 1; - } - } - - fSrc << " {" << sDat.getSize() << ", \""; + Bu::String sFltDesc; + for( Bu::StringList::iterator f = opt.slFilter.begin(); f; f++ ) + { + if( *f == "deflate" ) + { + 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 == "lzma" ) + { + sDat = encodeStr( sDat ); + sFltDesc.prepend("l"); + hFilters.insert('l', 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 + { + sio << "No known filter named " << *f << sio.nl; + return 1; + } + } + + fSrc << " {" << sDat.getSize() << ", \""; for( Bu::String::iterator j = sDat.begin(); j; j++ ) { fSrc << "\\x" << Fmt::hex() << (unsigned char)*j; } fSrc << "\", \"" << sFltDesc << "\"}," << fSrc.nl; - } - fSrc << "};" << fSrc.nl << fSrc.nl; + } + fSrc << "};" << fSrc.nl << fSrc.nl; - fSrc << "const " << opt.sClass << "::File &" << opt.sClass << "::getFile( const Bu::String &sName )" - << fSrc.nl - << "{" << fSrc.nl - << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl - << "\t{" << fSrc.nl; + fSrc << "const " << opt.sClass << "::File &" << opt.sClass << "::getFile( const Bu::String &sName )" + << fSrc.nl + << "{" << fSrc.nl + << "\tswitch( Bu::__calcHashCode( sName ) )" << fSrc.nl + << "\t{" << fSrc.nl; - int idx = 0; - for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) - { - fSrc << "\t\tcase " << Bu::__calcHashCode( *i ) << "UL:" << fSrc.nl - << "\t\t\treturn aFile[" << idx << "];" << fSrc.nl; - idx++; - } - fSrc << "\t}" << fSrc.nl - << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl - << "}" << fSrc.nl << 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::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl; + int idx = 0; + for( Bu::StringList::iterator i = opt.slInput.begin(); i; i++ ) + { + fSrc << "\t\tcase " << Bu::__calcHashCode( *i ) << "UL:" << fSrc.nl + << "\t\t\treturn aFile[" << idx << "];" << fSrc.nl; + idx++; + } + fSrc << "\t}" << fSrc.nl + << "\tthrow Bu::ExceptionBase(\"No file matching \\\"%s\\\" found.\", sName.getStr() );" << fSrc.nl + << "}" << fSrc.nl << 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::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('l') ) - fSrc << "\t\t\tcase 'l': 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; + 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('l') ) + fSrc << "\t\t\tcase 'l': 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 - << "{" << fSrc.nl - << "\tconst File &f = getFile( sName );" << fSrc.nl - << "\treturn new Bu::StreamStack( new Bu::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl - << "}" << fSrc.nl << 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::StaticMemBuf( f.data, f.iSize ) );" << fSrc.nl + << "}" << fSrc.nl << fSrc.nl; - fSrc << "Bu::String " << opt.sClass << "::getString( const Bu::String &sName )" << fSrc.nl - << "{" << 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\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 " << 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 - << "}" << fSrc.nl << fSrc.nl; + fSrc << "Bu::String " << opt.sClass << "::getString( const Bu::String &sName )" << fSrc.nl + << "{" << 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\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 " << 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 + << "}" << fSrc.nl << fSrc.nl; - return 0; + return 0; } diff --git a/src/tools/mkunit.cpp b/src/tools/mkunit.cpp index d634feb..96deb6b 100644 --- a/src/tools/mkunit.cpp +++ b/src/tools/mkunit.cpp @@ -17,236 +17,236 @@ using namespace Bu; class Test { public: - Test() : - bExpectPass( true ) - { - } + Test() : + bExpectPass( true ) + { + } - Bu::String sName; - bool bExpectPass; + Bu::String sName; + bool bExpectPass; }; typedef Bu::List TestList; class Suite { public: - Bu::String sName; - TestList lTest; + Bu::String sName; + TestList lTest; }; //typedef Bu::List SuiteList; enum TokType { - tokFluff, - tokSuite, - tokTest, - tokChar, - tokBlock, - tokEof + tokFluff, + tokSuite, + tokTest, + tokChar, + tokBlock, + tokEof }; Bu::Formatter &operator<<( Bu::Formatter &f, TokType t ) { - switch( t ) - { - case tokFluff: return f << "tokFluff"; - case tokSuite: return f << "tokSuite"; - case tokTest: return f << "tokTest"; - case tokChar: return f << "tokChar"; - case tokBlock: return f << "tokBlock"; - case tokEof: return f << "tokEof"; - } - - return f; + switch( t ) + { + case tokFluff: return f << "tokFluff"; + case tokSuite: return f << "tokSuite"; + case tokTest: return f << "tokTest"; + case tokChar: return f << "tokChar"; + case tokBlock: return f << "tokBlock"; + case tokEof: return f << "tokEof"; + } + + return f; } Bu::Formatter &operator<<( Bu::Formatter &f, const Test &t ) { - return f << "{" << t.sName << ", bExpectPass=" << t.bExpectPass << "}"; + return f << "{" << t.sName << ", bExpectPass=" << t.bExpectPass << "}"; } Bu::Formatter &operator<<( Bu::Formatter &f, const Suite &s ) { - return f << "Suite[" << s.sName << "] = " << s.lTest << f.nl; + return f << "Suite[" << s.sName << "] = " << s.lTest << f.nl; } class Parser { public: - Parser( const Bu::String &sFile ) : - sIn( sFile ), - fIn( sFile, File::Read ), - bIn( fIn ), - cBuf( 0 ), - bAvail( false ), - eMode( mRoot ), - iLine( 1 ), - iChar( 0 ), - iDepth( 0 ) - { - } - - char nextChar() - { - if( bAvail ) - return cBuf; - - if( bIn.read( &cBuf, 1 ) < 1 ) - throw Bu::ExceptionBase("End of stream"); - bAvail = true; - - if( cBuf == '\n' ) - { - iLine++; - iChar = 0; - } - else - iChar++; - - return cBuf; - } - - TokType nextToken( Variant &v, Bu::String &sWsOut, int &iLineStart, - int &iCharStart ) - { - Bu::String sTok, sWs; - - char buf; - try - { - buf = nextChar(); - } - catch(...) - { - return tokEof; - } - - for(;;) - { - if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' ) - { - sWs += buf; - bAvail = false; - } - else - break; - - try - { - buf = nextChar(); - } - catch(...) - { - sWsOut = sWs; - return tokEof; - } - } - - sWsOut = sWs; - - iLineStart = iLine; - iCharStart = iChar; - bool bInStr = false; - bool bDblStr; - - for(;;) - { - switch( eMode ) - { - case mRoot: - if( buf == ' ' || buf == '\t' || buf == '\n' - || buf == '\r' ) - { - if( sTok == "suite" ) - return tokSuite; - else - { - v = sTok; - return tokFluff; - } - } - else if( buf == '(' || buf == ')' || buf == '{' - || buf == '}' || buf == ';' ) - { - if( sTok.getSize() == 0 ) - { - bAvail = false; - v = buf; - return tokChar; - } - else - { - v = sTok; - return tokFluff; - } - } - else - { - sTok += buf; - bAvail = false; - } - break; - - case mSuite: - if( buf == ' ' || buf == '\t' || buf == '\n' - || buf == '\r' ) - { - if( sTok == "test" ) - return tokTest; - else - { - v = sTok; - return tokFluff; - } - } - else if( buf == '(' || buf == ')' - || buf == '}' || buf == ';' ) - { - if( sTok.getSize() == 0 ) - { - bAvail = false; - v = buf; - return tokChar; - } - else - { - v = sTok; - return tokFluff; - } - } - else if( buf == '{' ) - { - if( sTok.getSize() > 0 ) - { - v = sTok; - return tokFluff; - } - else - { - sTok += buf; - bAvail = false; - eMode = mBlock; - iDepth = 1; - } - } - else - { - sTok += buf; - bAvail = false; - } - break; - - case mBlock: - if( bInStr ) - { - if( buf == '\\' ) - { - sTok += buf; - bAvail = false; - sTok += nextChar(); - bAvail = false; - } - else if( bDblStr == true && buf == '\"' ) + Parser( const Bu::String &sFile ) : + sIn( sFile ), + fIn( sFile, File::Read ), + bIn( fIn ), + cBuf( 0 ), + bAvail( false ), + eMode( mRoot ), + iLine( 1 ), + iChar( 0 ), + iDepth( 0 ) + { + } + + char nextChar() + { + if( bAvail ) + return cBuf; + + if( bIn.read( &cBuf, 1 ) < 1 ) + throw Bu::ExceptionBase("End of stream"); + bAvail = true; + + if( cBuf == '\n' ) + { + iLine++; + iChar = 0; + } + else + iChar++; + + return cBuf; + } + + TokType nextToken( Variant &v, Bu::String &sWsOut, int &iLineStart, + int &iCharStart ) + { + Bu::String sTok, sWs; + + char buf; + try + { + buf = nextChar(); + } + catch(...) + { + return tokEof; + } + + for(;;) + { + if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' ) + { + sWs += buf; + bAvail = false; + } + else + break; + + try + { + buf = nextChar(); + } + catch(...) + { + sWsOut = sWs; + return tokEof; + } + } + + sWsOut = sWs; + + iLineStart = iLine; + iCharStart = iChar; + bool bInStr = false; + bool bDblStr; + + for(;;) + { + switch( eMode ) + { + case mRoot: + if( buf == ' ' || buf == '\t' || buf == '\n' + || buf == '\r' ) + { + if( sTok == "suite" ) + return tokSuite; + else + { + v = sTok; + return tokFluff; + } + } + else if( buf == '(' || buf == ')' || buf == '{' + || buf == '}' || buf == ';' ) + { + if( sTok.getSize() == 0 ) + { + bAvail = false; + v = buf; + return tokChar; + } + else + { + v = sTok; + return tokFluff; + } + } + else + { + sTok += buf; + bAvail = false; + } + break; + + case mSuite: + if( buf == ' ' || buf == '\t' || buf == '\n' + || buf == '\r' ) + { + if( sTok == "test" ) + return tokTest; + else + { + v = sTok; + return tokFluff; + } + } + else if( buf == '(' || buf == ')' + || buf == '}' || buf == ';' ) + { + if( sTok.getSize() == 0 ) + { + bAvail = false; + v = buf; + return tokChar; + } + else + { + v = sTok; + return tokFluff; + } + } + else if( buf == '{' ) + { + if( sTok.getSize() > 0 ) + { + v = sTok; + return tokFluff; + } + else + { + sTok += buf; + bAvail = false; + eMode = mBlock; + iDepth = 1; + } + } + else + { + sTok += buf; + bAvail = false; + } + break; + + case mBlock: + if( bInStr ) + { + if( buf == '\\' ) + { + sTok += buf; + bAvail = false; + sTok += nextChar(); + bAvail = false; + } + else if( bDblStr == true && buf == '\"' ) { sTok += buf; bAvail = false; @@ -267,295 +267,295 @@ public: else { if( buf == '\"' ) - { - bInStr = true; - bDblStr = true; - sTok += buf; - bAvail = false; - } - else if( buf == '\'' ) - { - bInStr = true; - bDblStr = false; - sTok += buf; - bAvail = false; - } - else if( buf == '}' ) - { - sTok += buf; - bAvail = false; - iDepth--; - if( iDepth == 0 ) - { - v = sTok; - eMode = mSuite; - return tokBlock; - } - } - else if( buf == '{' ) - { - sTok += buf; - bAvail = false; - iDepth++; - } - else - { - sTok += buf; - bAvail = false; - } - } - break; - } - - buf = nextChar(); - } - } - - void firstPass() - { - Variant v; - Bu::String sWs; - int iL, iC; - for(;;) - { - TokType t = nextToken( v, sWs, iL, iC ); - if( t == tokEof ) - return; - switch( eMode ) - { - case mRoot: - if( t == tokSuite ) - { - if( nextToken( v, sWs, iL, iC ) != tokFluff ) - throw Bu::ExceptionBase("%d:%d: Expected string " - "following suite.", iL, iC ); - s.sName = v.get(); - if( nextToken( v, sWs, iL, iC ) != tokChar || - v.get() != '{' ) - throw Bu::ExceptionBase("%d:%d: Expected {, got " - "'%s'", iL, iC, v.toString().getStr() ); - eMode = mSuite; - } - break; - - case mSuite: - switch( t ) - { - case tokFluff: - break; - - case tokBlock: - break; - - case tokTest: - { - if( nextToken( v, sWs, iL, iC ) != tokFluff ) - throw Bu::ExceptionBase("%d:%d: Expected " - "string following test.", iL, iC ); - Test t; - t.sName = v.get(); - if( nextToken( v, sWs, iL, iC ) != tokBlock ) - throw Bu::ExceptionBase("%d:%d: Expected " - "{...} block.", - iL, iC ); - s.lTest.append( t ); - } - break; - - case tokChar: - if( v.get() == '}' ) - { - eMode = mRoot; - } - else - { - } - break; - - default: - sio << iL << ":" << iC << ": Unexpected " - << t << " found." << sio.nl; - return; - break; - } - break; - - default: - sio << "???" << sio.nl; - break; - } - } - } - - void secondPass( const Bu::String &sOut ) - { - File fOut( sOut, File::WriteNew ); - Formatter f( fOut ); - fIn.setPos( 0 ); - bIn.stop(); - bIn.start(); - bAvail = false; - eMode = mRoot; - iLine = 1; - iChar = 0; - bool bHasIncluded = false; - - Bu::String sWs; - Variant v; - int iL, iC; - for(;;) - { - TokType t = nextToken( v, sWs, iL, iC ); - switch( eMode ) - { - case mRoot: - if( t == tokSuite ) - { - fOut.write( sWs ); - if( nextToken( v, sWs, iL, iC ) != tokFluff ) - throw Bu::ExceptionBase("%d:%d: Expected string " - "following suite.", iL, iC ); - s.sName = v.get(); - if( nextToken( v, sWs, iL, iC ) != tokChar || - v.get() != '{' ) - throw Bu::ExceptionBase("%d:%d: Expected {", - iL, iC ); - eMode = mSuite; - - if( bHasIncluded == false ) - { - fOut.write("#include \n"); - bHasIncluded = true; - } - - Bu::String sClass = "_UnitSuite_" + s.sName; - f << "class " << sClass - << " : public Bu::UnitSuite" << f.nl - << "{" << f.nl << "public:" << f.nl - << "\t" << sClass << "()" << f.nl - << "\t{" << f.nl - << "\t\tsetName(\"" << s.sName << "\");" << f.nl; - for( TestList::iterator i = s.lTest.begin(); i; i++ ) - { - f << "\t\tadd( static_cast(" - "&" << sClass << "::" << (*i).sName << "), \"" + { + bInStr = true; + bDblStr = true; + sTok += buf; + bAvail = false; + } + else if( buf == '\'' ) + { + bInStr = true; + bDblStr = false; + sTok += buf; + bAvail = false; + } + else if( buf == '}' ) + { + sTok += buf; + bAvail = false; + iDepth--; + if( iDepth == 0 ) + { + v = sTok; + eMode = mSuite; + return tokBlock; + } + } + else if( buf == '{' ) + { + sTok += buf; + bAvail = false; + iDepth++; + } + else + { + sTok += buf; + bAvail = false; + } + } + break; + } + + buf = nextChar(); + } + } + + void firstPass() + { + Variant v; + Bu::String sWs; + int iL, iC; + for(;;) + { + TokType t = nextToken( v, sWs, iL, iC ); + if( t == tokEof ) + return; + switch( eMode ) + { + case mRoot: + if( t == tokSuite ) + { + if( nextToken( v, sWs, iL, iC ) != tokFluff ) + throw Bu::ExceptionBase("%d:%d: Expected string " + "following suite.", iL, iC ); + s.sName = v.get(); + if( nextToken( v, sWs, iL, iC ) != tokChar || + v.get() != '{' ) + throw Bu::ExceptionBase("%d:%d: Expected {, got " + "'%s'", iL, iC, v.toString().getStr() ); + eMode = mSuite; + } + break; + + case mSuite: + switch( t ) + { + case tokFluff: + break; + + case tokBlock: + break; + + case tokTest: + { + if( nextToken( v, sWs, iL, iC ) != tokFluff ) + throw Bu::ExceptionBase("%d:%d: Expected " + "string following test.", iL, iC ); + Test t; + t.sName = v.get(); + if( nextToken( v, sWs, iL, iC ) != tokBlock ) + throw Bu::ExceptionBase("%d:%d: Expected " + "{...} block.", + iL, iC ); + s.lTest.append( t ); + } + break; + + case tokChar: + if( v.get() == '}' ) + { + eMode = mRoot; + } + else + { + } + break; + + default: + sio << iL << ":" << iC << ": Unexpected " + << t << " found." << sio.nl; + return; + break; + } + break; + + default: + sio << "???" << sio.nl; + break; + } + } + } + + void secondPass( const Bu::String &sOut ) + { + File fOut( sOut, File::WriteNew ); + Formatter f( fOut ); + fIn.setPos( 0 ); + bIn.stop(); + bIn.start(); + bAvail = false; + eMode = mRoot; + iLine = 1; + iChar = 0; + bool bHasIncluded = false; + + Bu::String sWs; + Variant v; + int iL, iC; + for(;;) + { + TokType t = nextToken( v, sWs, iL, iC ); + switch( eMode ) + { + case mRoot: + if( t == tokSuite ) + { + fOut.write( sWs ); + if( nextToken( v, sWs, iL, iC ) != tokFluff ) + throw Bu::ExceptionBase("%d:%d: Expected string " + "following suite.", iL, iC ); + s.sName = v.get(); + if( nextToken( v, sWs, iL, iC ) != tokChar || + v.get() != '{' ) + throw Bu::ExceptionBase("%d:%d: Expected {", + iL, iC ); + eMode = mSuite; + + if( bHasIncluded == false ) + { + fOut.write("#include \n"); + bHasIncluded = true; + } + + Bu::String sClass = "_UnitSuite_" + s.sName; + f << "class " << sClass + << " : public Bu::UnitSuite" << f.nl + << "{" << f.nl << "public:" << f.nl + << "\t" << sClass << "()" << f.nl + << "\t{" << f.nl + << "\t\tsetName(\"" << s.sName << "\");" << f.nl; + for( TestList::iterator i = s.lTest.begin(); i; i++ ) + { + f << "\t\tadd( static_cast(" + "&" << sClass << "::" << (*i).sName << "), \"" << (*i).sName << "\", Bu::UnitSuite::" - "expectPass );" << f.nl; - } - f << "\t}" << f.nl << f.nl - << "\tvirtual ~" << sClass << "() { }" << f.nl - << f.nl; - } - else if( t == tokEof ) - { - Bu::String sClass = "_UnitSuite_" + s.sName; - f << sWs << f.nl << "int main( int argc, char *argv[] )" - << f.nl << "{" << f.nl << "\treturn " << sClass - << "().run( argc, argv );" << f.nl << "}" << f.nl; - } - else - { - fOut.write( sWs ); - f << v; - } - break; - - case mSuite: - switch( t ) - { - case tokFluff: - fOut.write( sWs ); - fOut.write( v.get() ); - break; - - case tokTest: - { - fOut.write( sWs ); - if( nextToken( v, sWs, iL, iC ) != tokFluff ) - throw Bu::ExceptionBase("%d:%d: Expected " - "string following test.", iL, iC ); - Test t; - t.sName = v.get(); - if( nextToken( v, sWs, iL, iC ) != tokBlock ) - throw Bu::ExceptionBase("%d:%d: Expected " - "{...} block.", - iL, iC ); - - f << "\tvoid " << t.sName << "()" - << f.nl << "#line " << iL - << " \"" << sIn << "\"" << f.nl - << v << f.nl; - } - break; - - case tokChar: - if( v.get() == '}' ) - { - f << "};" << f.nl << f.nl; - eMode = mRoot; - } - else - { - char buf = v.get(); - fOut.write( sWs ); - fOut.write( &buf, 1 ); - } - break; - - case tokBlock: - fOut.write( sWs ); - f << f.nl << "#line " << iL << " \"" << sIn + "expectPass );" << f.nl; + } + f << "\t}" << f.nl << f.nl + << "\tvirtual ~" << sClass << "() { }" << f.nl + << f.nl; + } + else if( t == tokEof ) + { + Bu::String sClass = "_UnitSuite_" + s.sName; + f << sWs << f.nl << "int main( int argc, char *argv[] )" + << f.nl << "{" << f.nl << "\treturn " << sClass + << "().run( argc, argv );" << f.nl << "}" << f.nl; + } + else + { + fOut.write( sWs ); + f << v; + } + break; + + case mSuite: + switch( t ) + { + case tokFluff: + fOut.write( sWs ); + fOut.write( v.get() ); + break; + + case tokTest: + { + fOut.write( sWs ); + if( nextToken( v, sWs, iL, iC ) != tokFluff ) + throw Bu::ExceptionBase("%d:%d: Expected " + "string following test.", iL, iC ); + Test t; + t.sName = v.get(); + if( nextToken( v, sWs, iL, iC ) != tokBlock ) + throw Bu::ExceptionBase("%d:%d: Expected " + "{...} block.", + iL, iC ); + + f << "\tvoid " << t.sName << "()" + << f.nl << "#line " << iL + << " \"" << sIn << "\"" << f.nl + << v << f.nl; + } + break; + + case tokChar: + if( v.get() == '}' ) + { + f << "};" << f.nl << f.nl; + eMode = mRoot; + } + else + { + char buf = v.get(); + fOut.write( sWs ); + fOut.write( &buf, 1 ); + } + break; + + case tokBlock: + fOut.write( sWs ); + f << f.nl << "#line " << iL << " \"" << sIn << "\"" << f.nl; - fOut.write( v.get() ); - - break; - - default: - sio << iL << ":" << iC << ": Unexpected " - << t << " found." << sio.nl; - return; - break; - } - break; - - default: - sio << "???" << sio.nl; - break; - } - if( t == tokEof ) - return; - } - } + fOut.write( v.get() ); + + break; + + default: + sio << iL << ":" << iC << ": Unexpected " + << t << " found." << sio.nl; + return; + break; + } + break; + + default: + sio << "???" << sio.nl; + break; + } + if( t == tokEof ) + return; + } + } private: - Bu::String sIn; - File fIn; - Buffer bIn; - char cBuf; - bool bAvail; - enum Mode - { - mRoot, - mSuite, - mBlock - }; - Mode eMode; - int iLine, iChar; - int iDepth; - Suite s; + Bu::String sIn; + File fIn; + Buffer bIn; + char cBuf; + bool bAvail; + enum Mode + { + mRoot, + mSuite, + mBlock + }; + Mode eMode; + int iLine, iChar; + int iDepth; + Suite s; }; int main( int argc, char *argv[] ) { - if( argc < 3 ) - { - sio << "Too few parameters." << sio.nl; - return 0; - } - Parser p( argv[1] ); + if( argc < 3 ) + { + sio << "Too few parameters." << sio.nl; + return 0; + } + Parser p( argv[1] ); - p.firstPass(); + p.firstPass(); - p.secondPass( argv[2] ); + p.secondPass( argv[2] ); } diff --git a/src/tools/myriad.cpp b/src/tools/myriad.cpp index 7cab628..8a288b0 100644 --- a/src/tools/myriad.cpp +++ b/src/tools/myriad.cpp @@ -17,237 +17,237 @@ using namespace Bu; enum Mode { - modeCreate, - modeInfo, - modeStreamNew, - modeStreamDump, - modeStreamPut, - modeStreamGet, - - modeNone + modeCreate, + modeInfo, + modeStreamNew, + modeStreamDump, + modeStreamPut, + modeStreamGet, + + modeNone }; class Options : public OptParser { public: - Options( int argc, char *argv[] ) : - eMode( modeNone ), - iBlockSize( 64 ), - iPreallocate( 0 ), - iStream( 0 ) - { - addHelpBanner("Mode of operation:"); - addOption( eMode, 'c', "create", - "Create a new Myriad file." ); - addOption( eMode, 'i', "info", - "Display some info about a Myriad file." ); - addOption( eMode, 'n', "new", - "Create a new sub-stream in a Myriad file."); - addOption( eMode, 'd', "dump", - "Display a hexdump of a stream from a Myriad file."); - addOption( eMode, "get", - "Get a file out of a Myriad stream (use --dst)."); - addOption( eMode, "put", - "Put a file into a Myriad stream (usr --src)."); - addHelpOption(); - - addHelpBanner("\nGeneral options:"); - addOption( iBlockSize, 'b', "block-size", "Set the block size." ); - addOption( iPreallocate, 'p', "preallocate", - "Number of blocks to preallocate." ); - addOption( sFile, 'f', "file", "Set the Myriad filename." ); - addOption( iStream, 's', "stream", "Substream to work with."); - addOption( sSrc, "src", "Source file for copying into a Myriad file."); - addOption( sDst, "dst", - "Destination file for copying out of a Myriad file."); - - setOverride( "create", modeCreate ); - setOverride( "info", modeInfo ); - setOverride( "new", modeStreamNew ); - setOverride( "dump", modeStreamDump ); - setOverride( "put", modeStreamPut ); - setOverride( "get", modeStreamGet ); - - parse( argc, argv ); - } - - Mode eMode; - int iBlockSize; - int iPreallocate; - int iStream; - Bu::String sFile; - Bu::String sSrc; - Bu::String sDst; + Options( int argc, char *argv[] ) : + eMode( modeNone ), + iBlockSize( 64 ), + iPreallocate( 0 ), + iStream( 0 ) + { + addHelpBanner("Mode of operation:"); + addOption( eMode, 'c', "create", + "Create a new Myriad file." ); + addOption( eMode, 'i', "info", + "Display some info about a Myriad file." ); + addOption( eMode, 'n', "new", + "Create a new sub-stream in a Myriad file."); + addOption( eMode, 'd', "dump", + "Display a hexdump of a stream from a Myriad file."); + addOption( eMode, "get", + "Get a file out of a Myriad stream (use --dst)."); + addOption( eMode, "put", + "Put a file into a Myriad stream (usr --src)."); + addHelpOption(); + + addHelpBanner("\nGeneral options:"); + addOption( iBlockSize, 'b', "block-size", "Set the block size." ); + addOption( iPreallocate, 'p', "preallocate", + "Number of blocks to preallocate." ); + addOption( sFile, 'f', "file", "Set the Myriad filename." ); + addOption( iStream, 's', "stream", "Substream to work with."); + addOption( sSrc, "src", "Source file for copying into a Myriad file."); + addOption( sDst, "dst", + "Destination file for copying out of a Myriad file."); + + setOverride( "create", modeCreate ); + setOverride( "info", modeInfo ); + setOverride( "new", modeStreamNew ); + setOverride( "dump", modeStreamDump ); + setOverride( "put", modeStreamPut ); + setOverride( "get", modeStreamGet ); + + parse( argc, argv ); + } + + Mode eMode; + int iBlockSize; + int iPreallocate; + int iStream; + Bu::String sFile; + Bu::String sSrc; + Bu::String sDst; }; Bu::Formatter &operator>>( Bu::Formatter &f, Mode & /*e*/ ) { - sio << "Uh oh, the formatter was called..." << sio.nl; - return f; + sio << "Uh oh, the formatter was called..." << sio.nl; + return f; } int main( int argc, char *argv[] ) { - Options opts( argc, argv ); - - switch( opts.eMode ) - { - case modeCreate: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file to create." << sio.nl; - return 0; - } - else - { - File fOut( opts.sFile, File::WriteNew|File::Read ); - Myriad m( fOut, opts.iBlockSize, opts.iPreallocate ); - } - break; - - case modeInfo: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file to display info about." << sio.nl; - return 0; - } - else - { - File fIn( opts.sFile, File::Read ); - Myriad m( fIn ); - sio << "Myriad info:" << sio.nl - << " Block size: " << m.getBlockSize() << sio.nl - << " Block count: " << m.getNumBlocks() << sio.nl - << " Blocks used: " << m.getNumUsedBlocks() << " (" - << m.getNumUsedBlocks()*100/m.getNumBlocks() << "%)" - << sio.nl - << " Stream count: " << m.getNumStreams() << sio.nl - << " Used space: " << m.getTotalUsedBytes() << sio.nl - << " Unused space: " << m.getTotalUnusedBytes() << sio.nl - << " % of files: " << (double)(m.getNumBlocks()*m.getBlockSize())/(double)(m.getTotalUsedBytes() + m.getTotalUnusedBytes( 4096 ))*100.0 << sio.nl; -/* Bu::Array aStreams = m.getStreamIds(); - sio << " Stream info:" << sio.nl; - for( Bu::Array::iterator i = aStreams.begin(); i; i++ ) - { - sio << " " << Fmt(4) << *i << ") " - << m.getStreamSize( *i ) << "b" << sio.nl; - } */ - } - break; - - case modeStreamNew: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file manipulate." << sio.nl; - return 0; - } - else - { - File fOut( opts.sFile, File::Write|File::Read ); - Myriad m( fOut ); - m.createStream( opts.iPreallocate ); - } - break; - - case modeStreamDump: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file to manipulate." << sio.nl; - return 0; - } - else - { - File fOut( opts.sFile, File::Read ); - Myriad m( fOut ); - MyriadStream s = m.openStream( opts.iStream ); - sio << "Stream " << opts.iStream << ":" << sio.nl; - char buf[8]; - int iPos = 0; - while( !s.isEos() ) - { - size_t sAmnt = s.read( buf, 8 ); - sio << Fmt(5) << iPos << ": "; - iPos += sAmnt; - for( size_t j = 0; j < sAmnt; j++ ) - { - sio << Fmt::hex(2) << (int)((unsigned char)buf[j]) - << " "; - } - for( size_t j = sAmnt; j < 8; j++ ) - { - sio << "-- "; - } - sio << "| "; - for( size_t j = 0; j < sAmnt; j++ ) - { - if( buf[j] >= 32 && buf[j] <= 126 ) - sio << buf[j] << " "; - else - sio << " "; - } - sio << sio.nl; - } - sio << "Position: " << s.tell() << ", isEos()=" << s.isEos() - << sio.nl; - } - break; - - case modeStreamPut: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file manipulate." << sio.nl; - return 0; - } - else if( !opts.sSrc.isSet() ) - { - sio << "Please specify a source file to read." << sio.nl; - } - else - { - File fOut( opts.sFile, File::Write|File::Read ); - Myriad m( fOut ); - MyriadStream sOut = m.openStream( - m.createStream( opts.iPreallocate ) - ); - File fIn( opts.sSrc, File::Read ); - char buf[1024]; - while( !fIn.isEos() ) - { - sOut.write( buf, fIn.read( buf, 1024 ) ); - } - } - break; - - case modeStreamGet: - if( !opts.sFile.isSet() ) - { - sio << "Please specify a file manipulate." << sio.nl; - return 0; - } - else if( !opts.sDst.isSet() ) - { - sio << "Please specify a destination file to write." << sio.nl; - } - else - { - File fIn( opts.sFile, File::Write|File::Read ); - Myriad m( fIn ); - MyriadStream sIn = m.openStream( opts.iStream ); - File fOut( opts.sDst, File::Write|File::Create|File::Truncate ); - char buf[1024]; - while( !sIn.isEos() ) - { - fOut.write( buf, sIn.read( buf, 1024 ) ); - } - } - break; - - case modeNone: - sio << "Please select a mode, for more info, try --help." - << sio.nl << sio.nl; - break; - } - - return 0; + Options opts( argc, argv ); + + switch( opts.eMode ) + { + case modeCreate: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file to create." << sio.nl; + return 0; + } + else + { + File fOut( opts.sFile, File::WriteNew|File::Read ); + Myriad m( fOut, opts.iBlockSize, opts.iPreallocate ); + } + break; + + case modeInfo: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file to display info about." << sio.nl; + return 0; + } + else + { + File fIn( opts.sFile, File::Read ); + Myriad m( fIn ); + sio << "Myriad info:" << sio.nl + << " Block size: " << m.getBlockSize() << sio.nl + << " Block count: " << m.getNumBlocks() << sio.nl + << " Blocks used: " << m.getNumUsedBlocks() << " (" + << m.getNumUsedBlocks()*100/m.getNumBlocks() << "%)" + << sio.nl + << " Stream count: " << m.getNumStreams() << sio.nl + << " Used space: " << m.getTotalUsedBytes() << sio.nl + << " Unused space: " << m.getTotalUnusedBytes() << sio.nl + << " % of files: " << (double)(m.getNumBlocks()*m.getBlockSize())/(double)(m.getTotalUsedBytes() + m.getTotalUnusedBytes( 4096 ))*100.0 << sio.nl; +/* Bu::Array aStreams = m.getStreamIds(); + sio << " Stream info:" << sio.nl; + for( Bu::Array::iterator i = aStreams.begin(); i; i++ ) + { + sio << " " << Fmt(4) << *i << ") " + << m.getStreamSize( *i ) << "b" << sio.nl; + } */ + } + break; + + case modeStreamNew: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file manipulate." << sio.nl; + return 0; + } + else + { + File fOut( opts.sFile, File::Write|File::Read ); + Myriad m( fOut ); + m.createStream( opts.iPreallocate ); + } + break; + + case modeStreamDump: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file to manipulate." << sio.nl; + return 0; + } + else + { + File fOut( opts.sFile, File::Read ); + Myriad m( fOut ); + MyriadStream s = m.openStream( opts.iStream ); + sio << "Stream " << opts.iStream << ":" << sio.nl; + char buf[8]; + int iPos = 0; + while( !s.isEos() ) + { + size_t sAmnt = s.read( buf, 8 ); + sio << Fmt(5) << iPos << ": "; + iPos += sAmnt; + for( size_t j = 0; j < sAmnt; j++ ) + { + sio << Fmt::hex(2) << (int)((unsigned char)buf[j]) + << " "; + } + for( size_t j = sAmnt; j < 8; j++ ) + { + sio << "-- "; + } + sio << "| "; + for( size_t j = 0; j < sAmnt; j++ ) + { + if( buf[j] >= 32 && buf[j] <= 126 ) + sio << buf[j] << " "; + else + sio << " "; + } + sio << sio.nl; + } + sio << "Position: " << s.tell() << ", isEos()=" << s.isEos() + << sio.nl; + } + break; + + case modeStreamPut: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file manipulate." << sio.nl; + return 0; + } + else if( !opts.sSrc.isSet() ) + { + sio << "Please specify a source file to read." << sio.nl; + } + else + { + File fOut( opts.sFile, File::Write|File::Read ); + Myriad m( fOut ); + MyriadStream sOut = m.openStream( + m.createStream( opts.iPreallocate ) + ); + File fIn( opts.sSrc, File::Read ); + char buf[1024]; + while( !fIn.isEos() ) + { + sOut.write( buf, fIn.read( buf, 1024 ) ); + } + } + break; + + case modeStreamGet: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file manipulate." << sio.nl; + return 0; + } + else if( !opts.sDst.isSet() ) + { + sio << "Please specify a destination file to write." << sio.nl; + } + else + { + File fIn( opts.sFile, File::Write|File::Read ); + Myriad m( fIn ); + MyriadStream sIn = m.openStream( opts.iStream ); + File fOut( opts.sDst, File::Write|File::Create|File::Truncate ); + char buf[1024]; + while( !sIn.isEos() ) + { + fOut.write( buf, sIn.read( buf, 1024 ) ); + } + } + break; + + case modeNone: + sio << "Please select a mode, for more info, try --help." + << sio.nl << sio.nl; + break; + } + + return 0; } diff --git a/src/tools/viewcsv.cpp b/src/tools/viewcsv.cpp index 5009ea8..328cafc 100644 --- a/src/tools/viewcsv.cpp +++ b/src/tools/viewcsv.cpp @@ -20,33 +20,33 @@ using namespace Bu; class Options : public Bu::OptParser { public: - Options( int argc, char *argv[] ) : - bHeader( true ) - { - addOption( bHeader, "no-header", - "Don't use the first line as a header row. This behaviour can " - "also be toggled while running with 'h'." ); - setNonOption( slot( this, &Options::onNonOption ) ); - addHelpOption(); - - setOverride( "no-header", "true" ); - parse( argc, argv ); - } - - virtual ~Options() - { - } - - int onNonOption( StrArray aParams ) - { - //sio << aParams << sio.nl; - sFileIn = aParams[0]; - - return 0; - } - - Bu::String sFileIn; - bool bHeader; + Options( int argc, char *argv[] ) : + bHeader( true ) + { + addOption( bHeader, "no-header", + "Don't use the first line as a header row. This behaviour can " + "also be toggled while running with 'h'." ); + setNonOption( slot( this, &Options::onNonOption ) ); + addHelpOption(); + + setOverride( "no-header", "true" ); + parse( argc, argv ); + } + + virtual ~Options() + { + } + + int onNonOption( StrArray aParams ) + { + //sio << aParams << sio.nl; + sFileIn = aParams[0]; + + return 0; + } + + Bu::String sFileIn; + bool bHeader; }; typedef Bu::Array StrGrid; @@ -54,421 +54,421 @@ typedef Bu::Array IntArray; class CsvDoc { public: - CsvDoc() : - iMaxCols( 0 ) - { - } - - virtual ~CsvDoc() - { - } - - void addRow( StrArray aStr ) - { - sgData.append( aStr ); - if( iMaxCols < aStr.getSize() ) - iMaxCols = aStr.getSize(); - while( aWidths.getSize() < iMaxCols ) - { - aWidths.append( 0 ); - } - for( int j = 0; j < aStr.getSize(); j++ ) - { - if( aWidths[j] < aStr[j].getSize() ) - aWidths[j] = aStr[j].getSize(); - } - } - - int iMaxCols; - StrGrid sgData; - IntArray aWidths; + CsvDoc() : + iMaxCols( 0 ) + { + } + + virtual ~CsvDoc() + { + } + + void addRow( StrArray aStr ) + { + sgData.append( aStr ); + if( iMaxCols < aStr.getSize() ) + iMaxCols = aStr.getSize(); + while( aWidths.getSize() < iMaxCols ) + { + aWidths.append( 0 ); + } + for( int j = 0; j < aStr.getSize(); j++ ) + { + if( aWidths[j] < aStr[j].getSize() ) + aWidths[j] = aStr[j].getSize(); + } + } + + int iMaxCols; + StrGrid sgData; + IntArray aWidths; }; class CsvView { public: - CsvView( CsvDoc &doc ) : - doc( doc ), - iXOff( 0 ), - iYOff( 0 ), - bHeaderRow( false ) - { - } - - virtual ~CsvView() - { - } - - void render() - { - erase(); - int maxx, maxy; - getmaxyx( stdscr, maxy, maxx ); - - int iRows = buMin( (int)doc.sgData.getSize(), maxy-((bHeaderRow)?(4):(3)) ); - int iCols = buMin( doc.iMaxCols, (int)maxx-1 ); - - int iHdrHeight = 1; - if( bHeaderRow ) - iHdrHeight++; - - // Draw the headers - for( int iRow = 0; iRow < iRows; iRow++ ) - { - if( iRow+iYOff >= doc.sgData.getSize() ) - break; - char buf[6]; - snprintf( buf, 6, "%5d", iRow+iYOff ); - mvaddnstr( iRow+iHdrHeight+1, 0, buf, 5 ); - mvaddch( iRow+iHdrHeight+1, 6, ACS_VLINE ); - } - int iXPos = 6; - try - { - for( int iCol = 0; iCol < iCols; iCol++ ) - { - if( iXPos >= maxx ) - break; - int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); - char buf[6]; - snprintf( buf, 6, "%d", iCol+iXOff ); - mvaddch( 0, iXPos, ACS_VLINE ); - mvaddch( iHdrHeight, iXPos, ACS_PLUS ); - mvaddnstr( 0, iXPos+1, buf, 5 ); - if( bHeaderRow ) - { - mvaddnstr( - 1, iXPos+1, doc.sgData[0][iCol+iXOff].getStr(), iWidth - ); - mvaddch( 1, iXPos, ACS_VLINE ); - } - for( int j = 0; j < iWidth; j++ ) - { - mvaddch( iHdrHeight, iXPos+j+1, ACS_HLINE ); - } - iXPos += iWidth+1; - } - } - catch(...) { } - for( int j = 0; j < 6; j++ ) - { - mvaddch( iHdrHeight, j, ACS_HLINE ); - } - - // Draw some data - for( int iRow = 0; iRow < iRows; iRow++ ) - { - try - { - int iXPos = 6; - for( int iCol = 0; iCol < iCols; iCol++ ) - { - if( iXPos >= maxx ) - break; - int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); - mvaddch( iRow+iHdrHeight+1, iXPos, ACS_VLINE ); - mvaddnstr( iRow+iHdrHeight+1, iXPos+1, - doc.sgData[iRow+iYOff][iCol+iXOff].getStr(), iWidth ); - iXPos += iWidth+1; - } - } catch(...) { } - } - - attron( A_REVERSE ); - for( int j = 0; j < maxx; j++ ) - { - mvaddch( maxy-1, j, ' ' ); - } - mvaddstr( maxy-1, 1, "q) quit h) toggle header row" ); - char buf[30]; - int iWidth = sprintf( buf, "[%dx%ld]", - doc.iMaxCols, doc.sgData.getSize() - ); - mvaddstr( maxy-1, maxx-iWidth-1, buf ); - attroff( A_REVERSE ); - } - - void move( int iX, int iY ) - { - iXOff += iX; - iYOff += iY; - if( iXOff < 0 ) - iXOff = 0; - - if( bHeaderRow ) - { - if( iYOff < 1 ) - iYOff = 1; - } - else - { - if( iYOff < 0 ) - iYOff = 0; - } - - if( iYOff >= doc.sgData.getSize() ) - iYOff = doc.sgData.getSize()-1; - - if( iXOff >= doc.iMaxCols ) - iXOff = doc.iMaxCols-1; - } - - void pageDown() - { - int maxx, maxy; - getmaxyx( stdscr, maxy, maxx ); - move( 0, maxy-((bHeaderRow)?(4):(3)) ); - } - - void pageUp() - { - int maxx, maxy; - getmaxyx( stdscr, maxy, maxx ); - move( 0, -(maxy-((bHeaderRow)?(4):(3))) ); - } - - void home() - { - iYOff = 0; - if( bHeaderRow ) iYOff++; - } - - void end() - { - iYOff = doc.sgData.getSize()-1; - } - - void setHeaderRow( bool bOn ) - { - if( bHeaderRow == bOn ) - return; - - bHeaderRow = bOn; - move( 0, ((bOn)?(1):(-1)) ); - } - - void toggleHeaderRow() - { - setHeaderRow( !bHeaderRow ); - } - - Bu::String prompt( const Bu::String &sPrompt ) - { - int maxx, maxy; - Bu::String sStr; - - RegEx re( sPrompt ); - - curs_set( 1 ); - for(;;) - { - getmaxyx( stdscr, maxy, maxx ); - for( int j = 0; j < maxx; j++ ) - { - mvaddch( maxy-1, j, ' ' ); - } - mvaddstr( maxy-1, 0, sPrompt.getStr() ); - - mvaddstr( maxy-1, sPrompt.getSize(), sStr.getStr() ); - - int iCh = getch(); - switch( iCh ) - { - case '\n': - case '\r': - case KEY_ENTER: - curs_set( 0 ); - return sStr; - break; - - case KEY_BACKSPACE: - if( sStr.getSize() > 0 ) - sStr.resize( sStr.getSize()-1 ); - break; - - default: - if( iCh < 127 ) - sStr += (char)iCh; - break; - } - } - } - - void resetCaret() - { - sysCaret.reset(); - } - - void findNext( const Bu::String &sTerm ) - { - RegEx re( sTerm ); - - int y = sysCaret.iRow; - if( y < 0 ) - y = 0; - int x = sysCaret.iCol+1; - for( ; y < doc.sgData.getSize(); y++ ) - { - StrArray &aRow = doc.sgData[y]; - for( ; x < aRow.getSize(); x++ ) - { - if( re.execute( aRow[x] ) ) //aRow[x].find( sTerm ) ) - { - sysCaret.iRow = y; - sysCaret.iCol = x; - scrollToCaret(); - return; - } - } - x = 0; - } - } - - void scrollToCaret() - { - iXOff = sysCaret.iCol; - iYOff = sysCaret.iRow; - } - - CsvDoc &doc; - int iXOff; - int iYOff; - bool bHeaderRow; - - class Caret - { - public: - Caret() : - iRow( -1 ), - iCol( -1 ) - { - } - - virtual ~Caret() - { - } - - - void reset() - { - iRow = iCol = -1; - } - - bool isSet() - { - if( iRow < 0 || iCol < 0 ) - return false; - return true; - } - - int iRow; - int iCol; - }; - - Caret sysCaret; + CsvView( CsvDoc &doc ) : + doc( doc ), + iXOff( 0 ), + iYOff( 0 ), + bHeaderRow( false ) + { + } + + virtual ~CsvView() + { + } + + void render() + { + erase(); + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + + int iRows = buMin( (int)doc.sgData.getSize(), maxy-((bHeaderRow)?(4):(3)) ); + int iCols = buMin( doc.iMaxCols, (int)maxx-1 ); + + int iHdrHeight = 1; + if( bHeaderRow ) + iHdrHeight++; + + // Draw the headers + for( int iRow = 0; iRow < iRows; iRow++ ) + { + if( iRow+iYOff >= doc.sgData.getSize() ) + break; + char buf[6]; + snprintf( buf, 6, "%5d", iRow+iYOff ); + mvaddnstr( iRow+iHdrHeight+1, 0, buf, 5 ); + mvaddch( iRow+iHdrHeight+1, 6, ACS_VLINE ); + } + int iXPos = 6; + try + { + for( int iCol = 0; iCol < iCols; iCol++ ) + { + if( iXPos >= maxx ) + break; + int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); + char buf[6]; + snprintf( buf, 6, "%d", iCol+iXOff ); + mvaddch( 0, iXPos, ACS_VLINE ); + mvaddch( iHdrHeight, iXPos, ACS_PLUS ); + mvaddnstr( 0, iXPos+1, buf, 5 ); + if( bHeaderRow ) + { + mvaddnstr( + 1, iXPos+1, doc.sgData[0][iCol+iXOff].getStr(), iWidth + ); + mvaddch( 1, iXPos, ACS_VLINE ); + } + for( int j = 0; j < iWidth; j++ ) + { + mvaddch( iHdrHeight, iXPos+j+1, ACS_HLINE ); + } + iXPos += iWidth+1; + } + } + catch(...) { } + for( int j = 0; j < 6; j++ ) + { + mvaddch( iHdrHeight, j, ACS_HLINE ); + } + + // Draw some data + for( int iRow = 0; iRow < iRows; iRow++ ) + { + try + { + int iXPos = 6; + for( int iCol = 0; iCol < iCols; iCol++ ) + { + if( iXPos >= maxx ) + break; + int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); + mvaddch( iRow+iHdrHeight+1, iXPos, ACS_VLINE ); + mvaddnstr( iRow+iHdrHeight+1, iXPos+1, + doc.sgData[iRow+iYOff][iCol+iXOff].getStr(), iWidth ); + iXPos += iWidth+1; + } + } catch(...) { } + } + + attron( A_REVERSE ); + for( int j = 0; j < maxx; j++ ) + { + mvaddch( maxy-1, j, ' ' ); + } + mvaddstr( maxy-1, 1, "q) quit h) toggle header row" ); + char buf[30]; + int iWidth = sprintf( buf, "[%dx%ld]", + doc.iMaxCols, doc.sgData.getSize() + ); + mvaddstr( maxy-1, maxx-iWidth-1, buf ); + attroff( A_REVERSE ); + } + + void move( int iX, int iY ) + { + iXOff += iX; + iYOff += iY; + if( iXOff < 0 ) + iXOff = 0; + + if( bHeaderRow ) + { + if( iYOff < 1 ) + iYOff = 1; + } + else + { + if( iYOff < 0 ) + iYOff = 0; + } + + if( iYOff >= doc.sgData.getSize() ) + iYOff = doc.sgData.getSize()-1; + + if( iXOff >= doc.iMaxCols ) + iXOff = doc.iMaxCols-1; + } + + void pageDown() + { + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + move( 0, maxy-((bHeaderRow)?(4):(3)) ); + } + + void pageUp() + { + int maxx, maxy; + getmaxyx( stdscr, maxy, maxx ); + move( 0, -(maxy-((bHeaderRow)?(4):(3))) ); + } + + void home() + { + iYOff = 0; + if( bHeaderRow ) iYOff++; + } + + void end() + { + iYOff = doc.sgData.getSize()-1; + } + + void setHeaderRow( bool bOn ) + { + if( bHeaderRow == bOn ) + return; + + bHeaderRow = bOn; + move( 0, ((bOn)?(1):(-1)) ); + } + + void toggleHeaderRow() + { + setHeaderRow( !bHeaderRow ); + } + + Bu::String prompt( const Bu::String &sPrompt ) + { + int maxx, maxy; + Bu::String sStr; + + RegEx re( sPrompt ); + + curs_set( 1 ); + for(;;) + { + getmaxyx( stdscr, maxy, maxx ); + for( int j = 0; j < maxx; j++ ) + { + mvaddch( maxy-1, j, ' ' ); + } + mvaddstr( maxy-1, 0, sPrompt.getStr() ); + + mvaddstr( maxy-1, sPrompt.getSize(), sStr.getStr() ); + + int iCh = getch(); + switch( iCh ) + { + case '\n': + case '\r': + case KEY_ENTER: + curs_set( 0 ); + return sStr; + break; + + case KEY_BACKSPACE: + if( sStr.getSize() > 0 ) + sStr.resize( sStr.getSize()-1 ); + break; + + default: + if( iCh < 127 ) + sStr += (char)iCh; + break; + } + } + } + + void resetCaret() + { + sysCaret.reset(); + } + + void findNext( const Bu::String &sTerm ) + { + RegEx re( sTerm ); + + int y = sysCaret.iRow; + if( y < 0 ) + y = 0; + int x = sysCaret.iCol+1; + for( ; y < doc.sgData.getSize(); y++ ) + { + StrArray &aRow = doc.sgData[y]; + for( ; x < aRow.getSize(); x++ ) + { + if( re.execute( aRow[x] ) ) //aRow[x].find( sTerm ) ) + { + sysCaret.iRow = y; + sysCaret.iCol = x; + scrollToCaret(); + return; + } + } + x = 0; + } + } + + void scrollToCaret() + { + iXOff = sysCaret.iCol; + iYOff = sysCaret.iRow; + } + + CsvDoc &doc; + int iXOff; + int iYOff; + bool bHeaderRow; + + class Caret + { + public: + Caret() : + iRow( -1 ), + iCol( -1 ) + { + } + + virtual ~Caret() + { + } + + + void reset() + { + iRow = iCol = -1; + } + + bool isSet() + { + if( iRow < 0 || iCol < 0 ) + return false; + return true; + } + + int iRow; + int iCol; + }; + + Caret sysCaret; }; int main( int argc, char *argv[] ) { - Options opt( argc, argv ); - - if( !opt.sFileIn.isSet() ) - { - sio << "No file specified." << sio.nl; - return 1; - } - - CsvDoc doc; - { - File fIn( opt.sFileIn, File::Read ); - NewLine nlIn( fIn ); - Buffer bIn( nlIn ); - CsvReader cr( bIn ); - - while( !fIn.isEos() ) - { - StrArray sa = cr.readLine(); - if( fIn.isEos() ) - break; - doc.addRow( sa ); - } - } - - initscr(); - cbreak(); - noecho(); - nonl(); - intrflush( stdscr, FALSE ); - keypad( stdscr, TRUE ); - curs_set( 0 ); - - CsvView view( doc ); - view.setHeaderRow( opt.bHeader ); - - Bu::String sSearchTerm; - - bool bRun = true; - do - { - view.render(); - - int ch = getch(); - switch( ch ) - { - case 'q': - bRun = false; - break; - - case KEY_DOWN: - view.move( 0, 1 ); - break; - - case KEY_UP: - view.move( 0, -1 ); - break; - - case KEY_LEFT: - view.move( -1, 0 ); - break; - - case KEY_RIGHT: - view.move( 1, 0 ); - break; - - case KEY_NPAGE: - view.pageDown(); - break; - - case KEY_PPAGE: - view.pageUp(); - break; - - case KEY_HOME: - view.home(); - break; - - case KEY_END: - view.end(); - break; - - case '/': - sSearchTerm = view.prompt("find: "); - view.resetCaret(); - view.findNext( sSearchTerm ); - break; - - case 'n': - view.findNext( sSearchTerm ); - break; - - case 'h': - view.toggleHeaderRow(); - break; - } - } while( bRun ); - - endwin(); - - return 0; + Options opt( argc, argv ); + + if( !opt.sFileIn.isSet() ) + { + sio << "No file specified." << sio.nl; + return 1; + } + + CsvDoc doc; + { + File fIn( opt.sFileIn, File::Read ); + NewLine nlIn( fIn ); + Buffer bIn( nlIn ); + CsvReader cr( bIn ); + + while( !fIn.isEos() ) + { + StrArray sa = cr.readLine(); + if( fIn.isEos() ) + break; + doc.addRow( sa ); + } + } + + initscr(); + cbreak(); + noecho(); + nonl(); + intrflush( stdscr, FALSE ); + keypad( stdscr, TRUE ); + curs_set( 0 ); + + CsvView view( doc ); + view.setHeaderRow( opt.bHeader ); + + Bu::String sSearchTerm; + + bool bRun = true; + do + { + view.render(); + + int ch = getch(); + switch( ch ) + { + case 'q': + bRun = false; + break; + + case KEY_DOWN: + view.move( 0, 1 ); + break; + + case KEY_UP: + view.move( 0, -1 ); + break; + + case KEY_LEFT: + view.move( -1, 0 ); + break; + + case KEY_RIGHT: + view.move( 1, 0 ); + break; + + case KEY_NPAGE: + view.pageDown(); + break; + + case KEY_PPAGE: + view.pageUp(); + break; + + case KEY_HOME: + view.home(); + break; + + case KEY_END: + view.end(); + break; + + case '/': + sSearchTerm = view.prompt("find: "); + view.resetCaret(); + view.findNext( sSearchTerm ); + break; + + case 'n': + view.findNext( sSearchTerm ); + break; + + case 'h': + view.toggleHeaderRow(); + break; + } + } while( bRun ); + + endwin(); + + return 0; } -- cgit v1.2.3