From c884da672645231b5ec47706c886381dab1b391a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 05:09:12 +0000 Subject: The file stream is imported and works, as does our first test, and the new tweaks to archive. The && operator is now a template function, and as such requires no special handling. It could be worth it to check this out for other types, yet dangerous, since it would let you archive anything, even a class, without writing the proper functions for it...we shall see what happens... --- src/archive.cpp | 27 ++++++++++++++-- src/archive.h | 12 ++++--- src/old/sfile.cpp | 74 ------------------------------------------ src/old/sfile.h | 29 ----------------- src/sfile.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/sfile.h | 36 +++++++++++++++++++++ src/tests/archive.cpp | 11 ++++++- 7 files changed, 169 insertions(+), 110 deletions(-) delete mode 100644 src/old/sfile.cpp delete mode 100644 src/old/sfile.h create mode 100644 src/sfile.cpp create mode 100644 src/sfile.h (limited to 'src') diff --git a/src/archive.cpp b/src/archive.cpp index 5f5145c..be06c0e 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -1,13 +1,36 @@ #include "archive.h" -Bu::Archive::Archive(bool bLoading): - bLoading(bLoading) +Bu::Archive::Archive( Stream &rStream, bool bLoading ) : + bLoading( bLoading ), + rStream( rStream ) { } + Bu::Archive::~Archive() { } +void Bu::Archive::write( const void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.write( (const char *)pData, nSize ); +} + +void Bu::Archive::read( void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.read( (char *)pData, nSize ); +} + +void Bu::Archive::close() +{ + rStream.close(); +} + bool Bu::Archive::isLoading() { return bLoading; diff --git a/src/archive.h b/src/archive.h index 7de9220..26e430b 100644 --- a/src/archive.h +++ b/src/archive.h @@ -4,6 +4,7 @@ #include #include #include "archable.h" +#include "stream.h" namespace Bu { @@ -20,12 +21,12 @@ namespace Bu save = false }; - Archive( bool bLoading ); + Archive( Stream &rStream, bool bLoading ); virtual ~Archive(); - virtual void close()=0; + virtual void close(); - virtual void write(const void *, int32_t)=0; - virtual void read(void *, int32_t)=0; + virtual void write(const void *, int32_t); + virtual void read(void *, int32_t); virtual Archive &operator<<(bool); virtual Archive &operator<<(int8_t); @@ -67,6 +68,9 @@ namespace Bu virtual Archive &operator&&(float &); virtual Archive &operator&&(double &); virtual Archive &operator&&(long double &); + + private: + Stream &rStream; }; Archive &operator<<(Archive &, class Bu::Archable &); diff --git a/src/old/sfile.cpp b/src/old/sfile.cpp deleted file mode 100644 index f1de03c..0000000 --- a/src/old/sfile.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "sfile.h" -#include "exceptions.h" - -SFile::SFile( const char *sName, const char *sFlags ) -{ - fh = fopen( sName, sFlags ); -} - -SFile::~SFile() -{ -} - -void SFile::close() -{ - if( fh ) - { - fclose( fh ); - fh = NULL; - } -} - -size_t SFile::read( char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fread( pBuf, 1, nBytes, fh ); -} - -size_t SFile::write( const char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fwrite( pBuf, 1, nBytes, fh ); -} - -long SFile::tell() -{ - if( !fh ) - throw FileException("File not open."); - - return ftell( fh ); -} - -void SFile::seek( long offset ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, offset, SEEK_CUR ); -} - -void SFile::setPos( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_SET ); -} - -void SFile::setPosEnd( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_END ); -} - -bool SFile::isEOS() -{ - return feof( fh ); -} - diff --git a/src/old/sfile.h b/src/old/sfile.h deleted file mode 100644 index b51e5bc..0000000 --- a/src/old/sfile.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SFILE_H -#define SFILE_H - -#include - -#include "stream.h" - -class SFile : public Stream -{ -public: - SFile( const char *sName, const char *sFlags ); - virtual ~SFile(); - - virtual void close(); - virtual size_t read( char *pBuf, size_t nBytes ); - virtual size_t write( const char *pBuf, size_t nBytes ); - - virtual long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); - virtual bool isEOS(); - -private: - FILE *fh; - -}; - -#endif diff --git a/src/sfile.cpp b/src/sfile.cpp new file mode 100644 index 0000000..d7c5c83 --- /dev/null +++ b/src/sfile.cpp @@ -0,0 +1,90 @@ +#include "sfile.h" +#include "exceptions.h" + +Bu::SFile::SFile( const char *sName, const char *sFlags ) +{ + fh = fopen( sName, sFlags ); +} + +Bu::SFile::~SFile() +{ + close(); +} + +void Bu::SFile::close() +{ + if( fh ) + { + fclose( fh ); + fh = NULL; + } +} + +size_t Bu::SFile::read( char *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fread( pBuf, 1, nBytes, fh ); +} + +size_t Bu::SFile::write( const char *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fwrite( pBuf, 1, nBytes, fh ); +} + +long Bu::SFile::tell() +{ + if( !fh ) + throw FileException("File not open."); + + return ftell( fh ); +} + +void Bu::SFile::seek( long offset ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, offset, SEEK_CUR ); +} + +void Bu::SFile::setPos( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_SET ); +} + +void Bu::SFile::setPosEnd( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_END ); +} + +bool Bu::SFile::isEOS() +{ + return feof( fh ); +} + +bool Bu::SFile::canRead() +{ + return true; +} + +bool Bu::SFile::canWrite() +{ + return true; +} + +bool Bu::SFile::canSeek() +{ + return true; +} + diff --git a/src/sfile.h b/src/sfile.h new file mode 100644 index 0000000..304f6b7 --- /dev/null +++ b/src/sfile.h @@ -0,0 +1,36 @@ +#ifndef SFILE_H +#define SFILE_H + +#include + +#include "stream.h" + +namespace Bu +{ + class SFile : public Bu::Stream + { + public: + SFile( const char *sName, const char *sFlags ); + virtual ~SFile(); + + virtual void close(); + virtual size_t read( char *pBuf, size_t nBytes ); + virtual size_t write( const char *pBuf, size_t nBytes ); + + virtual long tell(); + virtual void seek( long offset ); + virtual void setPos( long pos ); + virtual void setPosEnd( long pos ); + virtual bool isEOS(); + + virtual bool canRead(); + virtual bool canWrite(); + virtual bool canSeek(); + + private: + FILE *fh; + + }; +} + +#endif diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp index fb0d97c..5b7e285 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -1,7 +1,16 @@ #include "archive.h" +#include "sfile.h" + +using namespace Bu; int main() { - //Archive + SFile f("test.dat", "wb"); + Archive ar( f, Archive::save ); + + std::string s("Hello there"); + ar << s; + + return 0; } -- cgit v1.2.3