diff options
Diffstat (limited to 'src/file.cpp')
| -rw-r--r-- | src/file.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/file.cpp b/src/file.cpp new file mode 100644 index 0000000..5de5f6c --- /dev/null +++ b/src/file.cpp | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | #include "file.h" | ||
| 2 | #include "exceptions.h" | ||
| 3 | #include <errno.h> | ||
| 4 | |||
| 5 | Bu::File::File( const char *sName, const char *sFlags ) | ||
| 6 | { | ||
| 7 | fh = fopen( sName, sFlags ); | ||
| 8 | if( fh == NULL ) | ||
| 9 | { | ||
| 10 | throw Bu::FileException( errno, strerror(errno) ); | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | Bu::File::~File() | ||
| 15 | { | ||
| 16 | close(); | ||
| 17 | } | ||
| 18 | |||
| 19 | void Bu::File::close() | ||
| 20 | { | ||
| 21 | if( fh ) | ||
| 22 | { | ||
| 23 | fclose( fh ); | ||
| 24 | fh = NULL; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | size_t Bu::File::read( void *pBuf, size_t nBytes ) | ||
| 29 | { | ||
| 30 | if( !fh ) | ||
| 31 | throw FileException("File not open."); | ||
| 32 | |||
| 33 | int nAmnt = fread( pBuf, 1, nBytes, fh ); | ||
| 34 | |||
| 35 | if( nAmnt == 0 ) | ||
| 36 | throw FileException("End of file."); | ||
| 37 | |||
| 38 | return nAmnt; | ||
| 39 | } | ||
| 40 | |||
| 41 | size_t Bu::File::write( const void *pBuf, size_t nBytes ) | ||
| 42 | { | ||
| 43 | if( !fh ) | ||
| 44 | throw FileException("File not open."); | ||
| 45 | |||
| 46 | return fwrite( pBuf, 1, nBytes, fh ); | ||
| 47 | } | ||
| 48 | |||
| 49 | long Bu::File::tell() | ||
| 50 | { | ||
| 51 | if( !fh ) | ||
| 52 | throw FileException("File not open."); | ||
| 53 | |||
| 54 | return ftell( fh ); | ||
| 55 | } | ||
| 56 | |||
| 57 | void Bu::File::seek( long offset ) | ||
| 58 | { | ||
| 59 | if( !fh ) | ||
| 60 | throw FileException("File not open."); | ||
| 61 | |||
| 62 | fseek( fh, offset, SEEK_CUR ); | ||
| 63 | } | ||
| 64 | |||
| 65 | void Bu::File::setPos( long pos ) | ||
| 66 | { | ||
| 67 | if( !fh ) | ||
| 68 | throw FileException("File not open."); | ||
| 69 | |||
| 70 | fseek( fh, pos, SEEK_SET ); | ||
| 71 | } | ||
| 72 | |||
| 73 | void Bu::File::setPosEnd( long pos ) | ||
| 74 | { | ||
| 75 | if( !fh ) | ||
| 76 | throw FileException("File not open."); | ||
| 77 | |||
| 78 | fseek( fh, pos, SEEK_END ); | ||
| 79 | } | ||
| 80 | |||
| 81 | bool Bu::File::isEOS() | ||
| 82 | { | ||
| 83 | return feof( fh ); | ||
| 84 | } | ||
| 85 | |||
| 86 | bool Bu::File::canRead() | ||
| 87 | { | ||
| 88 | return true; | ||
| 89 | } | ||
| 90 | |||
| 91 | bool Bu::File::canWrite() | ||
| 92 | { | ||
| 93 | return true; | ||
| 94 | } | ||
| 95 | |||
| 96 | bool Bu::File::canSeek() | ||
| 97 | { | ||
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
