From b6f57560fb7fae00f0854ca19158bd5512e5405b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 1 Oct 2008 21:48:33 +0000 Subject: This commit is sure to break things. This should be a very, very minor change. What changed API-Wise: - I deleted a constructor in Bu::File that shouldn't have been used anyway. - I changed it from using fopen style mode strings to using libbu++ style mode flags. Check the docs for the complete list, but basically instead of "wb" you do Bu::File::Write, and so on, you can or any of the libbu++ flags together. There is no binary/text mode, it just writes whatever you tell it to verbatim (binary mode). Lots of extras are supported. Nothing else should have changed (except now the file stream is unbuffered, like all the other streams). Sorry if this breaks anything, if it's too annoying, use the last revision for a while longer. --- src/file.cpp | 111 +++++++++++++++++++++++++++++-------------------- src/file.h | 27 ++++++++---- src/nids.cpp | 15 ++++--- src/tests/archive.cpp | 2 +- src/tests/archive2.cpp | 2 +- src/tests/bzip2.cpp | 4 +- src/tests/nids.cpp | 11 +++-- src/tests/taf.cpp | 8 ++-- src/unit/file.cpp | 8 ++-- src/unit/taf.cpp | 4 +- 10 files changed, 116 insertions(+), 76 deletions(-) diff --git a/src/file.cpp b/src/file.cpp index 3419216..5049bbb 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -10,30 +10,24 @@ #include #include #include +#include namespace Bu { subExceptionDef( FileException ) } -Bu::File::File( const char *sName, const char *sFlags ) +Bu::File::File( const Bu::FString &sName, int iFlags ) : + fd( -1 ) { - fh = fopen( sName, sFlags ); - if( fh == NULL ) + fd = ::open( sName.getStr(), getPosixFlags( iFlags ) ); + if( fd < 0 ) { - throw Bu::FileException( errno, "%s: %s", strerror(errno), sName ); + throw Bu::FileException( errno, "%s: %s", + strerror(errno), sName.getStr() ); } } -Bu::File::File( const Bu::FString &sName, const char *sFlags ) +Bu::File::File( int fd ) : + fd( fd ) { - fh = fopen( sName.getStr(), sFlags ); - if( fh == NULL ) - { - throw Bu::FileException( errno, "%s: %s", strerror(errno), sName.getStr() ); - } -} - -Bu::File::File( int fd, const char *sFlags ) -{ - fh = fdopen( fd, sFlags ); } Bu::File::~File() @@ -43,69 +37,68 @@ Bu::File::~File() void Bu::File::close() { - if( fh ) + if( fd >= 0 ) { - fclose( fh ); - fh = NULL; + if( ::close( fd ) ) + { + throw Bu::FileException( errno, "%s", + strerror(errno) ); + } + fd = -1; } } size_t Bu::File::read( void *pBuf, size_t nBytes ) { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - int nAmnt = fread( pBuf, 1, nBytes, fh ); - - //if( nAmnt == 0 ) - // throw FileException("End of file."); - - return nAmnt; + return ::read( fd, pBuf, nBytes ); } size_t Bu::File::write( const void *pBuf, size_t nBytes ) { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - return fwrite( pBuf, 1, nBytes, fh ); + return ::write( fd, pBuf, nBytes ); } long Bu::File::tell() { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - return ftell( fh ); + return lseek( fd, 0, SEEK_CUR ); } void Bu::File::seek( long offset ) { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - fseek( fh, offset, SEEK_CUR ); + lseek( fd, offset, SEEK_CUR ); } void Bu::File::setPos( long pos ) { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - fseek( fh, pos, SEEK_SET ); + lseek( fd, pos, SEEK_SET ); } void Bu::File::setPosEnd( long pos ) { - if( !fh ) + if( fd < 0 ) throw FileException("File not open."); - fseek( fh, pos, SEEK_END ); + lseek( fd, pos, SEEK_END ); } bool Bu::File::isEOS() { - return feof( fh ); + return false; } bool Bu::File::canRead() @@ -145,13 +138,13 @@ void Bu::File::setBlocking( bool bBlocking ) #else if( bBlocking ) fcntl( - fileno( fh ), - F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )&(~O_NONBLOCK) + fd, + F_SETFL, fcntl( fd, F_GETFL, 0 )&(~O_NONBLOCK) ); else fcntl( - fileno( fh ), - F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )|O_NONBLOCK + fd, + F_SETFL, fcntl( fd, F_GETFL, 0 )|O_NONBLOCK ); #endif } @@ -159,22 +152,52 @@ void Bu::File::setBlocking( bool bBlocking ) #ifndef WIN32 void Bu::File::truncate( long nSize ) { - ftruncate( fileno( fh ), nSize ); + ftruncate( fd, nSize ); } void Bu::File::chmod( mode_t t ) { - fchmod( fileno( fh ), t ); + fchmod( fd, t ); } #endif void Bu::File::flush() { - fflush( fh ); + // There is no flushing with direct I/O... + //fflush( fh ); } bool Bu::File::isOpen() { - return (fh != NULL); + return (fd > -1); +} + +int Bu::File::getPosixFlags( int iFlags ) +{ + int iRet = 0; + switch( (iFlags&ReadWrite) ) + { + // According to posix, O_RDWR is not necesarilly O_RDONLY|O_WRONLY, so + // lets be proper and use the right value in the right place. + case Read: iRet = O_RDONLY; break; + case Write: iRet = O_WRONLY; break; + case ReadWrite: iRet = O_RDWR; break; + default: + throw FileException( + "You must specify Read, Write, or both when opening a file."); + } + + if( (iFlags&Create) ) + iRet |= O_CREAT; + if( (iFlags&Append) ) + iRet |= O_APPEND; + if( (iFlags&Truncate) ) + iRet |= O_TRUNC; + if( (iFlags&NonBlock) ) + iRet |= O_NONBLOCK; + if( (iFlags&Exclusive) ) + iRet |= O_EXCL; + + return iRet; } diff --git a/src/file.h b/src/file.h index e48e6f1..40419b0 100644 --- a/src/file.h +++ b/src/file.h @@ -10,7 +10,6 @@ #include #include -#include #include "bu/stream.h" #include "bu/fstring.h" @@ -27,9 +26,8 @@ namespace Bu class File : public Bu::Stream { public: - File( const char *sName, const char *sFlags ); - File( const Bu::FString &sName, const char *sFlags ); - File( int fd, const char *sFlags ); + File( const Bu::FString &sName, int iFlags ); + File( int fd ); virtual ~File(); virtual void close(); @@ -56,6 +54,19 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + enum { + // Flags + Read = 0x01, //< Open file for reading + Write = 0x02, //< Open file for writing + Create = 0x04, //< Create file if it doesn't exist + Truncate = 0x08, //< Truncate file if it does exist + Append = 0x10, //< Always append on every write + NonBlock = 0x20, //< Open file in non-blocking mode + Exclusive = 0x44, //< Create file, if it exists then fail + + // Helpful mixes + ReadWrite = 0x03 //< Open for reading and writing + }; /** * Create a temp file and return its handle *@param sName (Bu::FString) Give in the form: "/tmp/tmpfileXXXXXXXX" @@ -65,11 +76,11 @@ namespace Bu *@returns (Bu::File) A file object representing your temp file. */ #ifndef WIN32 - inline static Bu::File tempFile( Bu::FString &sName, const char *sFlags ) + inline static Bu::File tempFile( Bu::FString &sName, int /*iFlags*/ ) { int afh_d = mkstemp( sName.getStr() ); - return Bu::File( afh_d, sFlags ); + return Bu::File( afh_d ); } /** @@ -87,8 +98,10 @@ namespace Bu #endif private: - FILE *fh; + int getPosixFlags( int iFlags ); + private: + int fd; }; } diff --git a/src/nids.cpp b/src/nids.cpp index d0cb843..a6596a2 100644 --- a/src/nids.cpp +++ b/src/nids.cpp @@ -3,7 +3,7 @@ #include "bu/nidsstream.h" #include -#define NIDS_MAGIC_CODE "\xFF\xC3\x99\xBD" +#define NIDS_MAGIC_CODE ((unsigned char *)"\xFF\xC3\x99\xBD") namespace Bu { @@ -36,8 +36,10 @@ Bu::Nids::Nids( Bu::Stream &sStore ) : sStore.isOpen()?"yes":"no" ); printf("sizeof(Block) = %db\n", sizeof(Block) ); - - + printf("Magic: %02X%02X%02X%02X\n", + NIDS_MAGIC_CODE[0], NIDS_MAGIC_CODE[1], + NIDS_MAGIC_CODE[2], NIDS_MAGIC_CODE[3] + ); } Bu::Nids::~Nids() @@ -46,13 +48,16 @@ Bu::Nids::~Nids() void Bu::Nids::initialize() { - char buf[4]; - sStore.read( buf, 4 ); + unsigned char buf[4]; + if( sStore.read( buf, 4 ) < 4 ) + throw NidsException("Input stream appears to be empty."); if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) ) { throw NidsException( "Stream does not appear to be a valid NIDS format."); } + + } void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp index 698d37c..131d4de 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -12,7 +12,7 @@ using namespace Bu; int main() { - File f("test.dat", "wb"); + File f("test.dat", File::Write ); Archive ar( f, Archive::save ); std::string s("Hello there"); diff --git a/src/tests/archive2.cpp b/src/tests/archive2.cpp index 6d3c2c1..ab02d04 100644 --- a/src/tests/archive2.cpp +++ b/src/tests/archive2.cpp @@ -82,7 +82,7 @@ void write() { C *c = new C; - Bu::File f( "test.archive", "wb"); + Bu::File f( "test.archive", Bu::File::Write ); Bu::Archive ar( f, Bu::Archive::save ); ar << c; } diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp index de7c034..77dc064 100644 --- a/src/tests/bzip2.cpp +++ b/src/tests/bzip2.cpp @@ -19,10 +19,10 @@ int main( int argc, char *argv[] ) char buf[1024]; size_t nRead; - Bu::File f( argv[0], "wb" ); + Bu::File f( argv[0], Bu::File::Write ); Bu::BZip2 bz2( f ); - Bu::File fin( argv[1], "rb"); + Bu::File fin( argv[1], Bu::File::Read ); for(;;) { diff --git a/src/tests/nids.cpp b/src/tests/nids.cpp index 4856883..f50fde5 100644 --- a/src/tests/nids.cpp +++ b/src/tests/nids.cpp @@ -10,17 +10,16 @@ int main( int argc, char *argv[] ) return 1; } - Bu::File fOut( argv[1], "wb+"); + Bu::File fOut( argv[1], Bu::File::ReadWrite ); Bu::Nids n( fOut ); // n.initialize( 120, 5 ); Bu::NidsStream s = n.openStream( n.createStream() ); -/* - Bu::FString sBuf( 350 ); - memset( sBuf.getStr(), 'a', 350 ); - s.write( sBuf ); - */ + +// Bu::FString sBuf( 350 ); +// memset( sBuf.getStr(), 'a', 350 ); +// s.write( sBuf ); return 0; } diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp index 281b9c4..859ecfc 100644 --- a/src/tests/taf.cpp +++ b/src/tests/taf.cpp @@ -13,13 +13,13 @@ int main( int argc, char *argv[] ) { if( argc == 1 ) { - Bu::File f("test.taf", "rb"); + Bu::File f("test.taf", Bu::File::Read ); Bu::TafReader tr( f ); Bu::TafGroup *pGroup = tr.readGroup(); { - Bu::File fo("out.taf", "wb"); + Bu::File fo("out.taf", Bu::File::Write ); Bu::TafWriter tw( fo ); tw.writeGroup( pGroup ); } @@ -28,13 +28,13 @@ int main( int argc, char *argv[] ) } else if( argc == 3 ) { - Bu::File f( argv[1], "rb"); + Bu::File f( argv[1], Bu::File::Read ); Bu::TafReader tr( f ); Bu::TafGroup *pGroup = tr.readGroup(); { - Bu::File fo( argv[2], "wb"); + Bu::File fo( argv[2], Bu::File::Write ); Bu::TafWriter tw( fo ); tw.writeGroup( pGroup ); } diff --git a/src/unit/file.cpp b/src/unit/file.cpp index a22239d..cc19fac 100644 --- a/src/unit/file.cpp +++ b/src/unit/file.cpp @@ -31,7 +31,7 @@ public: // void writeFull() { - Bu::File sf("testfile1", "wb"); + Bu::File sf("testfile1", Bu::File::Write ); for( int c = 0; c < 256; c++ ) { unsigned char ch = (unsigned char)c; @@ -49,7 +49,7 @@ public: void readBlocks() { - Bu::File sf("testfile1", "rb"); + Bu::File sf("testfile1", Bu::File::Read ); unsigned char buf[50]; size_t total = 0; for(;;) @@ -74,7 +74,7 @@ public: { try { - Bu::File sf("doesn'texist", "rb"); + Bu::File sf("doesn'texist", Bu::File::Read ); unitFailed("No exception thrown"); } catch( Bu::FileException &e ) @@ -85,7 +85,7 @@ public: void readError2() { - Bu::File sf("testfile1", "rb"); + Bu::File sf("testfile1", Bu::File::Read ); char buf[256]; int r = sf.read( buf, 256 ); unitTest( r == 256 ); diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp index 4e22ab1..0a97eba 100644 --- a/src/unit/taf.cpp +++ b/src/unit/taf.cpp @@ -29,14 +29,14 @@ public: { #define FN_TMP ("/tmp/tmpXXXXXXXX") Bu::FString sFnTmp(FN_TMP); - Bu::File fOut = Bu::File::tempFile( sFnTmp, "wb" ); + Bu::File fOut = Bu::File::tempFile( sFnTmp, Bu::File::Write ); const char *data = "{test: name=\"Bob\"}" ; fOut.write(data,strlen(data)); fOut.close(); - Bu::File fIn(sFnTmp.getStr(), "rb"); + Bu::File fIn(sFnTmp.getStr(), Bu::File::Read ); Bu::TafReader tr(fIn); Bu::TafGroup *tn = tr.readGroup(); -- cgit v1.2.3