From 514721c24c7212c084ad2530e8239ff121097818 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 6 Apr 2009 19:13:51 +0000 Subject: Ok, I rearranged some things, we have a tools dir now, those build by default. Also I added a bunch of classes that I've been tinkering with that are almost ready for use, so I figured I may as well throw them in here. --- src/tests/fastcgi.cpp | 81 +++++++++++++++++ src/tests/nidstool.cpp | 241 ------------------------------------------------- src/tests/url.cpp | 45 ++++----- 3 files changed, 105 insertions(+), 262 deletions(-) create mode 100644 src/tests/fastcgi.cpp delete mode 100644 src/tests/nidstool.cpp (limited to 'src/tests') diff --git a/src/tests/fastcgi.cpp b/src/tests/fastcgi.cpp new file mode 100644 index 0000000..53dd68a --- /dev/null +++ b/src/tests/fastcgi.cpp @@ -0,0 +1,81 @@ +#include "bu/fastcgi.h" + +class Cgi : public Bu::FastCgi +{ +public: + Cgi() : + Bu::FastCgi::FastCgi() + { + } + + Cgi( int iPort ) : + Bu::FastCgi::FastCgi( iPort ) + { + } + + virtual ~Cgi() + { + } + + virtual int request( const StrHash &hParams, + const Bu::FString &sStdIn, Bu::Stream &sStdOut, + Bu::Stream &sStdErr ) + { + Bu::FString sOut("Content-Type: text/html\r\n\r\n"); + sOut += "

Environment:

"; + char buf[2048]; + sOut += "

Cwd:

"; + sOut += "

Stdin:

"; + sOut.formatAppend("%d bytes
", sStdIn.getSize() );
+		Bu::FString sL, sR;
+		for( Bu::FString::const_iterator i = sStdIn.begin();
+			i; i++ )
+		{
+			sL.formatAppend("%02X ",
+				(unsigned int)((unsigned char)*i) );
+			if( *i < 27 )
+				sR += ". ";
+			else
+				sR.formatAppend("&#%d; ",
+					(unsigned int)((unsigned char)*i) );
+			if( sL.getSize()/3 == 8 )
+			{
+				sOut += sL + " | " + sR + "\n";
+				sL = sR = "";
+			}
+		}
+		if( sL != "" )
+		{
+			while( sL.getSize()/3 < 8 )
+				sL += "   ";
+			sOut += sL + " | " + sR + "\n";
+		}
+		sOut += "

";
+		sOut += sStdIn;
+		sOut += "
"; + sOut += "


"; + sOut += ""; + + sStdOut.write( sOut ); + + return 0; + } +}; + +int main() +{ + Cgi c( 8789 ); + + c.run(); + + return 0; +} + diff --git a/src/tests/nidstool.cpp b/src/tests/nidstool.cpp deleted file mode 100644 index d1465ce..0000000 --- a/src/tests/nidstool.cpp +++ /dev/null @@ -1,241 +0,0 @@ -#include "bu/file.h" -#include "bu/nids.h" -#include "bu/nidsstream.h" -#include "bu/paramproc.h" - -#include - -typedef struct Block -{ - uint32_t uFirstBlock; - uint32_t uNextBlock; - uint32_t uBytesUsed; -} Block; - -class Param : public Bu::ParamProc -{ -public: - Param( int argc, char *argv[] ) - { - addHelpBanner("nidstool - Do stuff with nids files.\n\n"); - addParam("info", 'i', mkproc(Param::procInfo), - "Print some info about the file."); - addParam("dump", 'd', mkproc(Param::procDump), - "Dump a stream to a file."); - addParam("analyze", 'a', mkproc(Param::procAnalyze), - "Analyze a nids file."); - addParam("copy", 'c', mkproc(Param::procCopy), - "Copy a nids file, changing settings."); - addParam("help", 'h', mkproc(Bu::ParamProc::help), "This help."); - process( argc, argv ); - } - - virtual ~Param() - { - } - - void printInfo( Bu::Nids &n ) - { - printf("File info:\n"); - printf(" Header overhead: %db\n", n.getBlockStart() ); - printf(" Block size: %db\n", n.getBlockSize() ); - printf(" Block count: %d\n", n.getNumBlocks() ); - printf(" Blocks used: %d (%d%%)\n", n.getNumUsedBlocks(), - n.getNumUsedBlocks()*100/n.getNumBlocks() ); - printf(" Block overhead: %db\n", n.getBlockOverhead() ); - printf(" Block storage: %db (%d%%)\n", - n.getBlockSize()-n.getBlockOverhead(), - (n.getBlockSize()-n.getBlockOverhead())*100/n.getBlockSize() ); - } - - int procInfo( int argc, char *argv[] ) - { - if( argc < 1 ) - { - printf("You must provide a file name.\n"); - exit( 1 ); - } - - Bu::File fIn( argv[0], Bu::File::Read ); - Bu::Nids n( fIn ); - n.initialize(); - - printInfo( n ); - - if( argc >= 2 ) - { - uint32_t uStream = strtoul( argv[1], NULL, 0 ); - uint32_t uBlock = uStream; - - Block b; - - for(;;) - { - fIn.setPos( n.getBlockStart()+n.getBlockSize()*uBlock ); - fIn.read( &b, sizeof(Block) ); - printf("Stream %u: block %u, next %u, %ub used.\n", - uStream, uBlock, b.uNextBlock, b.uBytesUsed - ); - if( b.uNextBlock == 0xFFFFFFFFUL ) - break; - uBlock = b.uNextBlock; - } - printf("Stream End.\n"); - - return 2; - } - - return 1; - } - - int procDump( int argc, char *argv[] ) - { - if( argc < 3 ) - { - printf("You must provide a nids file, a stream id, and an output " - "file.\n"); - exit( 1 ); - } - - Bu::File fIn( argv[0], Bu::File::Read ); - Bu::Nids n( fIn ); - n.initialize(); - - int iStream = strtol( argv[1], NULL, 0 ); - Bu::NidsStream sIn = n.openStream( iStream ); - - Bu::File fOut( argv[2], Bu::File::Write|Bu::File::Create ); - int iTotal = 0; - char buf[100]; - for(;;) - { - int iRead = sIn.read( buf, 100 ); - iTotal += fOut.write( buf, iRead ); - if( iRead < 100 ) - break; - } - - printf("Wrote %db from stream %d in %s to %s.\n", - iTotal, iStream, argv[0], argv[2] ); - return 3; - } - - int procAnalyze( int argc, char *argv[] ) - { - if( argc < 1 ) - { - printf("You must provide a file name.\n"); - exit( 1 ); - } - - Bu::File fIn( argv[0], Bu::File::Read ); - Bu::Nids n( fIn ); - n.initialize(); - - printInfo( n ); - - int iStreamCnt = 0; - int iStreamTotal = 0; - int iOneBlock = 0; - uint32_t iLargest = 0; - uint32_t iSmallest = 0; - int iWaste = 0; - int iUsable = n.getBlockSize()-n.getBlockOverhead(); - Block b; - for( int j = 0; j < n.getNumBlocks(); j++ ) - { - fIn.setPos( n.getBlockStart()+n.getBlockSize()*j ); - fIn.read( &b, sizeof(Block) ); - if( b.uFirstBlock != (uint32_t)j ) - continue; - - iStreamCnt++; - iStreamTotal += b.uBytesUsed; - - if( b.uNextBlock == 0xFFFFFFFFUL ) - { - iOneBlock++; - iWaste += iUsable - b.uBytesUsed; - } - else - { - iWaste += iUsable - (b.uBytesUsed%iUsable); - } - - if( j == 0 ) - { - iSmallest = iLargest = b.uBytesUsed; - } - else - { - if( iLargest < b.uBytesUsed ) - iLargest = b.uBytesUsed; - if( iSmallest > b.uBytesUsed ) - iSmallest = b.uBytesUsed; - } - } - printf("Steam analysis:\n"); - printf(" Stream count: %d\n", iStreamCnt ); - printf(" Stream size: %db/%db/%db (min/avr/max)\n", - iSmallest, iStreamTotal/iStreamCnt, iLargest ); - printf(" One-block streams: %d (%d%%)\n", - iOneBlock, iOneBlock*100/iStreamCnt ); - printf(" Total wasted space: %db (%d%%)\n", - iWaste, iWaste*100/iStreamTotal ); - printf(" Avr blocks-per-stream: %f%%\n", - (float)n.getNumBlocks()/(float)iStreamCnt ); - - return 1; - } - - int procCopy( int argc, char *argv[] ) - { - if( argc < 3 ) - { - printf("You must provide source stream, blocksize, destination.\n"); - exit( 1 ); - } - - Bu::File fIn( argv[0], Bu::File::Read ); - Bu::Nids nIn( fIn ); - nIn.initialize(); - - Bu::File fOut( argv[2], Bu::File::Read|Bu::File::Write|Bu::File::Create| - Bu::File::Truncate ); - Bu::Nids nOut( fOut ); - nOut.initialize( strtol( argv[1], 0, NULL ) ); - - Block b; - for( int j = 0; j < nIn.getNumBlocks(); j++ ) - { - fIn.setPos( nIn.getBlockStart()+nIn.getBlockSize()*j ); - fIn.read( &b, sizeof(Block) ); - if( b.uFirstBlock != (uint32_t)j ) - continue; - - Bu::NidsStream sIn = nIn.openStream( j ); - int iNew = nOut.createStream(); - Bu::NidsStream sOut = nOut.openStream( iNew ); - - char buf[1024]; - for(;;) - { - int iRead = sIn.read( buf, 1024 ); - sOut.write( buf, iRead ); - if( iRead < 1024 ) - break; - } - } - - return 3; - } -}; - - -int main( int argc, char *argv[] ) -{ - Param p( argc, argv ); - - return 0; -} - diff --git a/src/tests/url.cpp b/src/tests/url.cpp index c9af676..4dc8c46 100644 --- a/src/tests/url.cpp +++ b/src/tests/url.cpp @@ -4,28 +4,31 @@ int main( int argc, char *argv[] ) { - printf("encodede: %s\n", Bu::Url::encode( argv[1] ).getStr() ); - printf("decodede: %s\n", Bu::Url::decode( argv[1] ).getStr() ); - Bu::Url u( argv[1] ); - - printf("Protocol: %s\n", u.getProtocol().getStr() ); - printf("User: %s\n", u.getUser().getStr() ); - printf("Pass: %s\n", u.getPass().getStr() ); - printf("Host: %s\n", u.getHost().getStr() ); - printf("Path: %s\n", u.getPath().getStr() ); - try - { - printf("Port: %d\n", u.getPort() ); - } catch( Bu::ExceptionBase &e ) + for( argc--, argv++; argc >= 0; argc--, argv++ ) { - printf("Port: not set.\n"); - } - printf("Parameters:\n"); - for( Bu::Url::ParamList::const_iterator i = u.getParamBegin(); i; i++ ) - { - printf(" \"%s\" = \"%s\"\n", - (*i).sName.getStr(), (*i).sValue.getStr() - ); + printf("encodede: %s\n", Bu::Url::encode( *argv ).getStr() ); + printf("decodede: %s\n", Bu::Url::decode( *argv ).getStr() ); + Bu::Url u( *argv ); + + printf("Protocol: %s\n", u.getProtocol().getStr() ); + printf("User: %s\n", u.getUser().getStr() ); + printf("Pass: %s\n", u.getPass().getStr() ); + printf("Host: %s\n", u.getHost().getStr() ); + printf("Path: %s\n", u.getPath().getStr() ); + try + { + printf("Port: %d\n", u.getPort() ); + } catch( Bu::ExceptionBase &e ) + { + printf("Port: not set.\n"); + } + printf("Parameters:\n"); + for( Bu::Url::ParamList::const_iterator i = u.getParamBegin(); i; i++ ) + { + printf(" \"%s\" = \"%s\"\n", + (*i).sName.getStr(), (*i).sValue.getStr() + ); + } } return 0; -- cgit v1.2.3