diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 05:09:12 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 05:09:12 +0000 |
commit | c884da672645231b5ec47706c886381dab1b391a (patch) | |
tree | f9c0a0c4fb9006c2c1aa10c8c65de2f6e42894de | |
parent | da89e6d30e57bd6dbb10b4d36b093ce9bbf5c666 (diff) | |
download | libbu++-c884da672645231b5ec47706c886381dab1b391a.tar.gz libbu++-c884da672645231b5ec47706c886381dab1b391a.tar.bz2 libbu++-c884da672645231b5ec47706c886381dab1b391a.tar.xz libbu++-c884da672645231b5ec47706c886381dab1b391a.zip |
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...
Diffstat (limited to '')
-rw-r--r-- | src/archive.cpp | 27 | ||||
-rw-r--r-- | src/archive.h | 12 | ||||
-rw-r--r-- | src/old/sfile.h | 29 | ||||
-rw-r--r-- | src/sfile.cpp (renamed from src/old/sfile.cpp) | 36 | ||||
-rw-r--r-- | src/sfile.h | 36 | ||||
-rw-r--r-- | src/tests/archive.cpp | 11 |
6 files changed, 105 insertions, 46 deletions
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 @@ | |||
1 | #include "archive.h" | 1 | #include "archive.h" |
2 | 2 | ||
3 | Bu::Archive::Archive(bool bLoading): | 3 | Bu::Archive::Archive( Stream &rStream, bool bLoading ) : |
4 | bLoading(bLoading) | 4 | bLoading( bLoading ), |
5 | rStream( rStream ) | ||
5 | { | 6 | { |
6 | } | 7 | } |
8 | |||
7 | Bu::Archive::~Archive() | 9 | Bu::Archive::~Archive() |
8 | { | 10 | { |
9 | } | 11 | } |
10 | 12 | ||
13 | void Bu::Archive::write( const void *pData, int32_t nSize ) | ||
14 | { | ||
15 | if( nSize == 0 || pData == NULL ) | ||
16 | return; | ||
17 | |||
18 | rStream.write( (const char *)pData, nSize ); | ||
19 | } | ||
20 | |||
21 | void Bu::Archive::read( void *pData, int32_t nSize ) | ||
22 | { | ||
23 | if( nSize == 0 || pData == NULL ) | ||
24 | return; | ||
25 | |||
26 | rStream.read( (char *)pData, nSize ); | ||
27 | } | ||
28 | |||
29 | void Bu::Archive::close() | ||
30 | { | ||
31 | rStream.close(); | ||
32 | } | ||
33 | |||
11 | bool Bu::Archive::isLoading() | 34 | bool Bu::Archive::isLoading() |
12 | { | 35 | { |
13 | return bLoading; | 36 | 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 @@ | |||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | #include <string> | 5 | #include <string> |
6 | #include "archable.h" | 6 | #include "archable.h" |
7 | #include "stream.h" | ||
7 | 8 | ||
8 | namespace Bu | 9 | namespace Bu |
9 | { | 10 | { |
@@ -20,12 +21,12 @@ namespace Bu | |||
20 | save = false | 21 | save = false |
21 | }; | 22 | }; |
22 | 23 | ||
23 | Archive( bool bLoading ); | 24 | Archive( Stream &rStream, bool bLoading ); |
24 | virtual ~Archive(); | 25 | virtual ~Archive(); |
25 | virtual void close()=0; | 26 | virtual void close(); |
26 | 27 | ||
27 | virtual void write(const void *, int32_t)=0; | 28 | virtual void write(const void *, int32_t); |
28 | virtual void read(void *, int32_t)=0; | 29 | virtual void read(void *, int32_t); |
29 | 30 | ||
30 | virtual Archive &operator<<(bool); | 31 | virtual Archive &operator<<(bool); |
31 | virtual Archive &operator<<(int8_t); | 32 | virtual Archive &operator<<(int8_t); |
@@ -67,6 +68,9 @@ namespace Bu | |||
67 | virtual Archive &operator&&(float &); | 68 | virtual Archive &operator&&(float &); |
68 | virtual Archive &operator&&(double &); | 69 | virtual Archive &operator&&(double &); |
69 | virtual Archive &operator&&(long double &); | 70 | virtual Archive &operator&&(long double &); |
71 | |||
72 | private: | ||
73 | Stream &rStream; | ||
70 | }; | 74 | }; |
71 | 75 | ||
72 | Archive &operator<<(Archive &, class Bu::Archable &); | 76 | Archive &operator<<(Archive &, class Bu::Archable &); |
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 @@ | |||
1 | #ifndef SFILE_H | ||
2 | #define SFILE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | |||
6 | #include "stream.h" | ||
7 | |||
8 | class SFile : public Stream | ||
9 | { | ||
10 | public: | ||
11 | SFile( const char *sName, const char *sFlags ); | ||
12 | virtual ~SFile(); | ||
13 | |||
14 | virtual void close(); | ||
15 | virtual size_t read( char *pBuf, size_t nBytes ); | ||
16 | virtual size_t write( const char *pBuf, size_t nBytes ); | ||
17 | |||
18 | virtual long tell(); | ||
19 | virtual void seek( long offset ); | ||
20 | virtual void setPos( long pos ); | ||
21 | virtual void setPosEnd( long pos ); | ||
22 | virtual bool isEOS(); | ||
23 | |||
24 | private: | ||
25 | FILE *fh; | ||
26 | |||
27 | }; | ||
28 | |||
29 | #endif | ||
diff --git a/src/old/sfile.cpp b/src/sfile.cpp index f1de03c..d7c5c83 100644 --- a/src/old/sfile.cpp +++ b/src/sfile.cpp | |||
@@ -1,16 +1,17 @@ | |||
1 | #include "sfile.h" | 1 | #include "sfile.h" |
2 | #include "exceptions.h" | 2 | #include "exceptions.h" |
3 | 3 | ||
4 | SFile::SFile( const char *sName, const char *sFlags ) | 4 | Bu::SFile::SFile( const char *sName, const char *sFlags ) |
5 | { | 5 | { |
6 | fh = fopen( sName, sFlags ); | 6 | fh = fopen( sName, sFlags ); |
7 | } | 7 | } |
8 | 8 | ||
9 | SFile::~SFile() | 9 | Bu::SFile::~SFile() |
10 | { | 10 | { |
11 | close(); | ||
11 | } | 12 | } |
12 | 13 | ||
13 | void SFile::close() | 14 | void Bu::SFile::close() |
14 | { | 15 | { |
15 | if( fh ) | 16 | if( fh ) |
16 | { | 17 | { |
@@ -19,7 +20,7 @@ void SFile::close() | |||
19 | } | 20 | } |
20 | } | 21 | } |
21 | 22 | ||
22 | size_t SFile::read( char *pBuf, size_t nBytes ) | 23 | size_t Bu::SFile::read( char *pBuf, size_t nBytes ) |
23 | { | 24 | { |
24 | if( !fh ) | 25 | if( !fh ) |
25 | throw FileException("File not open."); | 26 | throw FileException("File not open."); |
@@ -27,7 +28,7 @@ size_t SFile::read( char *pBuf, size_t nBytes ) | |||
27 | return fread( pBuf, 1, nBytes, fh ); | 28 | return fread( pBuf, 1, nBytes, fh ); |
28 | } | 29 | } |
29 | 30 | ||
30 | size_t SFile::write( const char *pBuf, size_t nBytes ) | 31 | size_t Bu::SFile::write( const char *pBuf, size_t nBytes ) |
31 | { | 32 | { |
32 | if( !fh ) | 33 | if( !fh ) |
33 | throw FileException("File not open."); | 34 | throw FileException("File not open."); |
@@ -35,7 +36,7 @@ size_t SFile::write( const char *pBuf, size_t nBytes ) | |||
35 | return fwrite( pBuf, 1, nBytes, fh ); | 36 | return fwrite( pBuf, 1, nBytes, fh ); |
36 | } | 37 | } |
37 | 38 | ||
38 | long SFile::tell() | 39 | long Bu::SFile::tell() |
39 | { | 40 | { |
40 | if( !fh ) | 41 | if( !fh ) |
41 | throw FileException("File not open."); | 42 | throw FileException("File not open."); |
@@ -43,7 +44,7 @@ long SFile::tell() | |||
43 | return ftell( fh ); | 44 | return ftell( fh ); |
44 | } | 45 | } |
45 | 46 | ||
46 | void SFile::seek( long offset ) | 47 | void Bu::SFile::seek( long offset ) |
47 | { | 48 | { |
48 | if( !fh ) | 49 | if( !fh ) |
49 | throw FileException("File not open."); | 50 | throw FileException("File not open."); |
@@ -51,7 +52,7 @@ void SFile::seek( long offset ) | |||
51 | fseek( fh, offset, SEEK_CUR ); | 52 | fseek( fh, offset, SEEK_CUR ); |
52 | } | 53 | } |
53 | 54 | ||
54 | void SFile::setPos( long pos ) | 55 | void Bu::SFile::setPos( long pos ) |
55 | { | 56 | { |
56 | if( !fh ) | 57 | if( !fh ) |
57 | throw FileException("File not open."); | 58 | throw FileException("File not open."); |
@@ -59,7 +60,7 @@ void SFile::setPos( long pos ) | |||
59 | fseek( fh, pos, SEEK_SET ); | 60 | fseek( fh, pos, SEEK_SET ); |
60 | } | 61 | } |
61 | 62 | ||
62 | void SFile::setPosEnd( long pos ) | 63 | void Bu::SFile::setPosEnd( long pos ) |
63 | { | 64 | { |
64 | if( !fh ) | 65 | if( !fh ) |
65 | throw FileException("File not open."); | 66 | throw FileException("File not open."); |
@@ -67,8 +68,23 @@ void SFile::setPosEnd( long pos ) | |||
67 | fseek( fh, pos, SEEK_END ); | 68 | fseek( fh, pos, SEEK_END ); |
68 | } | 69 | } |
69 | 70 | ||
70 | bool SFile::isEOS() | 71 | bool Bu::SFile::isEOS() |
71 | { | 72 | { |
72 | return feof( fh ); | 73 | return feof( fh ); |
73 | } | 74 | } |
74 | 75 | ||
76 | bool Bu::SFile::canRead() | ||
77 | { | ||
78 | return true; | ||
79 | } | ||
80 | |||
81 | bool Bu::SFile::canWrite() | ||
82 | { | ||
83 | return true; | ||
84 | } | ||
85 | |||
86 | bool Bu::SFile::canSeek() | ||
87 | { | ||
88 | return true; | ||
89 | } | ||
90 | |||
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 @@ | |||
1 | #ifndef SFILE_H | ||
2 | #define SFILE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | |||
6 | #include "stream.h" | ||
7 | |||
8 | namespace Bu | ||
9 | { | ||
10 | class SFile : public Bu::Stream | ||
11 | { | ||
12 | public: | ||
13 | SFile( const char *sName, const char *sFlags ); | ||
14 | virtual ~SFile(); | ||
15 | |||
16 | virtual void close(); | ||
17 | virtual size_t read( char *pBuf, size_t nBytes ); | ||
18 | virtual size_t write( const char *pBuf, size_t nBytes ); | ||
19 | |||
20 | virtual long tell(); | ||
21 | virtual void seek( long offset ); | ||
22 | virtual void setPos( long pos ); | ||
23 | virtual void setPosEnd( long pos ); | ||
24 | virtual bool isEOS(); | ||
25 | |||
26 | virtual bool canRead(); | ||
27 | virtual bool canWrite(); | ||
28 | virtual bool canSeek(); | ||
29 | |||
30 | private: | ||
31 | FILE *fh; | ||
32 | |||
33 | }; | ||
34 | } | ||
35 | |||
36 | #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 @@ | |||
1 | #include "archive.h" | 1 | #include "archive.h" |
2 | #include "sfile.h" | ||
3 | |||
4 | using namespace Bu; | ||
2 | 5 | ||
3 | int main() | 6 | int main() |
4 | { | 7 | { |
5 | //Archive | 8 | SFile f("test.dat", "wb"); |
9 | Archive ar( f, Archive::save ); | ||
10 | |||
11 | std::string s("Hello there"); | ||
12 | ar << s; | ||
13 | |||
14 | return 0; | ||
6 | } | 15 | } |
7 | 16 | ||