From 17df4c2b9616c29865b0d893cc797d4938a660a2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 24 Sep 2008 00:19:12 +0000 Subject: Wholly crap. Added the Fifo, fixed a bunch of bugs, made things more standard, now I have a huge list of new functions to add. Also, we discovered that if we add -W it produces more warnings, warnings about things that we'd like to know about. I have a lot of work to go fixing that... --- build.conf | 2 +- src/exceptions.cpp | 1 + src/exceptions.h | 1 + src/fifo.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fifo.h | 69 +++++++++++++++++++++++++ src/file.cpp | 12 ++++- src/process.cpp | 11 +++- src/process.h | 1 + src/util.h | 18 +++++++ 9 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 src/fifo.cpp create mode 100644 src/fifo.h diff --git a/build.conf b/build.conf index 31d33a8..898d6d4 100644 --- a/build.conf +++ b/build.conf @@ -9,7 +9,7 @@ default action: check group "lnhdrs", check "libbu++.a" "tests" action: check group "lnhdrs", check group "tests" "all" action: check group "lnhdrs", check targets() -set "CXXFLAGS" += "-ggdb -Wall" +set "CXXFLAGS" += "-ggdb -W -Wall" #set "CXXFLAGS" += "-O2 -Wall" # set "CXXFLAGS" += "-pg" diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 24c1a93..5d6deeb 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -13,6 +13,7 @@ namespace Bu subExceptionDef( XmlException ) subExceptionDef( TafException ) subExceptionDef( FileException ) + subExceptionDef( FifoException ) subExceptionDef( SocketException ) subExceptionDef( ConnectionException ) subExceptionDef( PluginException ) diff --git a/src/exceptions.h b/src/exceptions.h index cef682f..91e0e6d 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -16,6 +16,7 @@ namespace Bu subExceptionDecl( XmlException ) subExceptionDecl( TafException ) subExceptionDecl( FileException ) + subExceptionDecl( FifoException ) subExceptionDecl( SocketException ) subExceptionDecl( ConnectionException ) subExceptionDecl( PluginException ) diff --git a/src/fifo.cpp b/src/fifo.cpp new file mode 100644 index 0000000..2321cb6 --- /dev/null +++ b/src/fifo.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2007-2008 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 "fifo.h" +#include "exceptions.h" +#include +#include +#include +#include + +Bu::Fifo::Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc ) : + iFlags( iFlags ), + iIn( -1 ), + iOut( -1 ) +{ + if( iFlags&Create ) + { + if( mkfifo( sName.getStr(), mAcc ) ) + { + throw FifoException("Error creating fifo: %s\n", strerror( errno ) ); + } + } + if( iFlags&Read ) + { + iIn = ::open( + sName.getStr(), + O_RDONLY|((iFlags&NonBlock)?O_NONBLOCK:0) + ); + } + if( iFlags&Write ) + { + iOut = ::open( + sName.getStr(), + O_WRONLY + ); + } +} + +Bu::Fifo::~Fifo() +{ + close(); +} + +void Bu::Fifo::close() +{ + if( iIn > -1 ) + { + ::close( iIn ); + iIn = -1; + } + if( iOut > -1 ) + { + ::close( iOut ); + iOut = -1; + } +} + +size_t Bu::Fifo::read( void *pBuf, size_t nBytes ) +{ + if( iIn < 0 ) + throw FifoException("Fifo not open for reading."); + + return TEMP_FAILURE_RETRY( ::read( iIn, pBuf, nBytes ) ); +} + +size_t Bu::Fifo::write( const void *pBuf, size_t nBytes ) +{ + if( iOut < 0 ) + throw FifoException("Fifo not open for writing."); + + return TEMP_FAILURE_RETRY( ::write( iOut, pBuf, nBytes ) ); +} + +long Bu::Fifo::tell() +{ + return -1; +} + +void Bu::Fifo::seek( long offset ) +{ +} + +void Bu::Fifo::setPos( long pos ) +{ +} + +void Bu::Fifo::setPosEnd( long pos ) +{ +} + +bool Bu::Fifo::isEOS() +{ + return false; +} + +bool Bu::Fifo::canRead() +{ + return (iIn>-1); +} + +bool Bu::Fifo::canWrite() +{ + return (iOut>-1); +} + +bool Bu::Fifo::isReadable() +{ + return (iIn>-1); +} + +bool Bu::Fifo::isWritable() +{ + return (iOut>-1); +} + +bool Bu::Fifo::isSeekable() +{ + return false; +} + +bool Bu::Fifo::isBlocking() +{ + return ((fcntl( iIn, F_GETFL, 0 )&O_NONBLOCK) == O_NONBLOCK); +} + +void Bu::Fifo::setBlocking( bool bBlocking ) +{ + if( bBlocking ) + fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )&(~O_NONBLOCK) ); + else + fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )|O_NONBLOCK ); +} + +void Bu::Fifo::flush() +{ +} + +bool Bu::Fifo::isOpen() +{ + return (iIn > -1) || (iOut > -1); +} + diff --git a/src/fifo.h b/src/fifo.h new file mode 100644 index 0000000..4989839 --- /dev/null +++ b/src/fifo.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2007-2008 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_FIFO_H +#define BU_FIFO_H + +#include +#include +#include + +#include "bu/stream.h" +#include "bu/fstring.h" + +namespace Bu +{ + /** + * A fifo stream. + *@ingroup Streams + */ + class Fifo : public Bu::Stream + { + public: + Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc=-1 ); + virtual ~Fifo(); + + virtual void close(); + virtual size_t read( void *pBuf, size_t nBytes ); + virtual size_t write( const void *pBuf, size_t nBytes ); + using Stream::write; + + virtual long tell(); + virtual void seek( long offset ); + virtual void setPos( long pos ); + virtual void setPosEnd( long 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 ); + + enum { + Read = 0x01, + Write = 0x02, + Create = 0x04, + Delete = 0x08, + NonBlock = 0x10 + }; + + private: + int iFlags; + int iIn; + int iOut; + }; +} + +#endif diff --git a/src/file.cpp b/src/file.cpp index 3896005..83d76c5 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -10,6 +10,7 @@ #include #include #include +#include Bu::File::File( const char *sName, const char *sFlags ) { @@ -138,7 +139,16 @@ bool Bu::File::isBlocking() void Bu::File::setBlocking( bool bBlocking ) { - return; + if( bBlocking ) + fcntl( + fileno( fh ), + F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )&(~O_NONBLOCK) + ); + else + fcntl( + fileno( fh ), + F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )|O_NONBLOCK + ); } #ifndef WIN32 diff --git a/src/process.cpp b/src/process.cpp index e71b782..5781600 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -12,6 +12,7 @@ #include #include #include +#include Bu::Process::Process( const char *sName, char *const argv[] ) { @@ -69,6 +70,7 @@ void Bu::Process::gexec( const char *sName, char *const argv[] ) dup2( iaStdOut[1], 1 ); // dup2( iaStdErr[1], 2 ); execvp( sName, argv ); + throw Bu::ExceptionBase("Hey, execvp failed!"); } ::close( iaStdIn[0] ); ::close( iaStdOut[1] ); @@ -81,6 +83,8 @@ void Bu::Process::close() size_t Bu::Process::read( void *pBuf, size_t nBytes ) { + return TEMP_FAILURE_RETRY( ::read( iStdOut, pBuf, nBytes ) ); + /* size_t iTotal = 0; for(;;) { @@ -91,6 +95,7 @@ size_t Bu::Process::read( void *pBuf, size_t nBytes ) if( iTotal == nBytes ) return iTotal; } + */ /* size_t iTotal = 0; fd_set rfs; @@ -124,7 +129,7 @@ size_t Bu::Process::readErr( void *pBuf, size_t nBytes ) size_t Bu::Process::write( const void *pBuf, size_t nBytes ) { - return ::write( iStdIn, pBuf, nBytes ); + return TEMP_FAILURE_RETRY( ::write( iStdIn, pBuf, nBytes ) ); } long Bu::Process::tell() @@ -190,5 +195,9 @@ bool Bu::Process::isBlocking() void Bu::Process::setBlocking( bool bBlocking ) { + if( bBlocking ) + fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )&(~O_NONBLOCK) ); + else + fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK ); } diff --git a/src/process.h b/src/process.h index 27e2a32..a63fbf5 100644 --- a/src/process.h +++ b/src/process.h @@ -32,6 +32,7 @@ namespace Bu virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t readErr( void *pBuf, size_t nBytes ); virtual size_t write( const void *pBuf, size_t nBytes ); + using Stream::write; virtual long tell(); virtual void seek( long offset ); diff --git a/src/util.h b/src/util.h index b55ceee..efbfb26 100644 --- a/src/util.h +++ b/src/util.h @@ -27,18 +27,36 @@ namespace Bu b = tmp; } + template + const item &min( const item &a, const item &b ) + { + return a item &min( item &a, item &b ) { return a + const item &max( const item &a, const item &b ) + { + return a>b?a:b; + } + template item &max( item &a, item &b ) { return a>b?a:b; } + template + const item &mid( const item &a, const item &b, const item &c ) + { + return min( max( a, b ), c ); + } + template item &mid( item &a, item &b, item &c ) { -- cgit v1.2.3