aboutsummaryrefslogtreecommitdiff
path: root/src/file.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-10-01 21:48:33 +0000
committerMike Buland <eichlan@xagasoft.com>2008-10-01 21:48:33 +0000
commitb6f57560fb7fae00f0854ca19158bd5512e5405b (patch)
treeaa3c7012ee2b1ee9b97b1ba136217cbf6a84d587 /src/file.cpp
parentd872f7e07c5367f251cf5ebb70a03916251f5306 (diff)
downloadlibbu++-b6f57560fb7fae00f0854ca19158bd5512e5405b.tar.gz
libbu++-b6f57560fb7fae00f0854ca19158bd5512e5405b.tar.bz2
libbu++-b6f57560fb7fae00f0854ca19158bd5512e5405b.tar.xz
libbu++-b6f57560fb7fae00f0854ca19158bd5512e5405b.zip
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.
Diffstat (limited to 'src/file.cpp')
-rw-r--r--src/file.cpp111
1 files changed, 67 insertions, 44 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 @@
10#include <sys/types.h> 10#include <sys/types.h>
11#include <sys/stat.h> 11#include <sys/stat.h>
12#include <fcntl.h> 12#include <fcntl.h>
13#include <unistd.h>
13 14
14namespace Bu { subExceptionDef( FileException ) } 15namespace Bu { subExceptionDef( FileException ) }
15 16
16Bu::File::File( const char *sName, const char *sFlags ) 17Bu::File::File( const Bu::FString &sName, int iFlags ) :
18 fd( -1 )
17{ 19{
18 fh = fopen( sName, sFlags ); 20 fd = ::open( sName.getStr(), getPosixFlags( iFlags ) );
19 if( fh == NULL ) 21 if( fd < 0 )
20 { 22 {
21 throw Bu::FileException( errno, "%s: %s", strerror(errno), sName ); 23 throw Bu::FileException( errno, "%s: %s",
24 strerror(errno), sName.getStr() );
22 } 25 }
23} 26}
24 27
25Bu::File::File( const Bu::FString &sName, const char *sFlags ) 28Bu::File::File( int fd ) :
29 fd( fd )
26{ 30{
27 fh = fopen( sName.getStr(), sFlags );
28 if( fh == NULL )
29 {
30 throw Bu::FileException( errno, "%s: %s", strerror(errno), sName.getStr() );
31 }
32}
33
34Bu::File::File( int fd, const char *sFlags )
35{
36 fh = fdopen( fd, sFlags );
37} 31}
38 32
39Bu::File::~File() 33Bu::File::~File()
@@ -43,69 +37,68 @@ Bu::File::~File()
43 37
44void Bu::File::close() 38void Bu::File::close()
45{ 39{
46 if( fh ) 40 if( fd >= 0 )
47 { 41 {
48 fclose( fh ); 42 if( ::close( fd ) )
49 fh = NULL; 43 {
44 throw Bu::FileException( errno, "%s",
45 strerror(errno) );
46 }
47 fd = -1;
50 } 48 }
51} 49}
52 50
53size_t Bu::File::read( void *pBuf, size_t nBytes ) 51size_t Bu::File::read( void *pBuf, size_t nBytes )
54{ 52{
55 if( !fh ) 53 if( fd < 0 )
56 throw FileException("File not open."); 54 throw FileException("File not open.");
57 55
58 int nAmnt = fread( pBuf, 1, nBytes, fh ); 56 return ::read( fd, pBuf, nBytes );
59
60 //if( nAmnt == 0 )
61 // throw FileException("End of file.");
62
63 return nAmnt;
64} 57}
65 58
66size_t Bu::File::write( const void *pBuf, size_t nBytes ) 59size_t Bu::File::write( const void *pBuf, size_t nBytes )
67{ 60{
68 if( !fh ) 61 if( fd < 0 )
69 throw FileException("File not open."); 62 throw FileException("File not open.");
70 63
71 return fwrite( pBuf, 1, nBytes, fh ); 64 return ::write( fd, pBuf, nBytes );
72} 65}
73 66
74long Bu::File::tell() 67long Bu::File::tell()
75{ 68{
76 if( !fh ) 69 if( fd < 0 )
77 throw FileException("File not open."); 70 throw FileException("File not open.");
78 71
79 return ftell( fh ); 72 return lseek( fd, 0, SEEK_CUR );
80} 73}
81 74
82void Bu::File::seek( long offset ) 75void Bu::File::seek( long offset )
83{ 76{
84 if( !fh ) 77 if( fd < 0 )
85 throw FileException("File not open."); 78 throw FileException("File not open.");
86 79
87 fseek( fh, offset, SEEK_CUR ); 80 lseek( fd, offset, SEEK_CUR );
88} 81}
89 82
90void Bu::File::setPos( long pos ) 83void Bu::File::setPos( long pos )
91{ 84{
92 if( !fh ) 85 if( fd < 0 )
93 throw FileException("File not open."); 86 throw FileException("File not open.");
94 87
95 fseek( fh, pos, SEEK_SET ); 88 lseek( fd, pos, SEEK_SET );
96} 89}
97 90
98void Bu::File::setPosEnd( long pos ) 91void Bu::File::setPosEnd( long pos )
99{ 92{
100 if( !fh ) 93 if( fd < 0 )
101 throw FileException("File not open."); 94 throw FileException("File not open.");
102 95
103 fseek( fh, pos, SEEK_END ); 96 lseek( fd, pos, SEEK_END );
104} 97}
105 98
106bool Bu::File::isEOS() 99bool Bu::File::isEOS()
107{ 100{
108 return feof( fh ); 101 return false;
109} 102}
110 103
111bool Bu::File::canRead() 104bool Bu::File::canRead()
@@ -145,13 +138,13 @@ void Bu::File::setBlocking( bool bBlocking )
145#else 138#else
146 if( bBlocking ) 139 if( bBlocking )
147 fcntl( 140 fcntl(
148 fileno( fh ), 141 fd,
149 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )&(~O_NONBLOCK) 142 F_SETFL, fcntl( fd, F_GETFL, 0 )&(~O_NONBLOCK)
150 ); 143 );
151 else 144 else
152 fcntl( 145 fcntl(
153 fileno( fh ), 146 fd,
154 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )|O_NONBLOCK 147 F_SETFL, fcntl( fd, F_GETFL, 0 )|O_NONBLOCK
155 ); 148 );
156#endif 149#endif
157} 150}
@@ -159,22 +152,52 @@ void Bu::File::setBlocking( bool bBlocking )
159#ifndef WIN32 152#ifndef WIN32
160void Bu::File::truncate( long nSize ) 153void Bu::File::truncate( long nSize )
161{ 154{
162 ftruncate( fileno( fh ), nSize ); 155 ftruncate( fd, nSize );
163} 156}
164 157
165void Bu::File::chmod( mode_t t ) 158void Bu::File::chmod( mode_t t )
166{ 159{
167 fchmod( fileno( fh ), t ); 160 fchmod( fd, t );
168} 161}
169#endif 162#endif
170 163
171void Bu::File::flush() 164void Bu::File::flush()
172{ 165{
173 fflush( fh ); 166 // There is no flushing with direct I/O...
167 //fflush( fh );
174} 168}
175 169
176bool Bu::File::isOpen() 170bool Bu::File::isOpen()
177{ 171{
178 return (fh != NULL); 172 return (fd > -1);
173}
174
175int Bu::File::getPosixFlags( int iFlags )
176{
177 int iRet = 0;
178 switch( (iFlags&ReadWrite) )
179 {
180 // According to posix, O_RDWR is not necesarilly O_RDONLY|O_WRONLY, so
181 // lets be proper and use the right value in the right place.
182 case Read: iRet = O_RDONLY; break;
183 case Write: iRet = O_WRONLY; break;
184 case ReadWrite: iRet = O_RDWR; break;
185 default:
186 throw FileException(
187 "You must specify Read, Write, or both when opening a file.");
188 }
189
190 if( (iFlags&Create) )
191 iRet |= O_CREAT;
192 if( (iFlags&Append) )
193 iRet |= O_APPEND;
194 if( (iFlags&Truncate) )
195 iRet |= O_TRUNC;
196 if( (iFlags&NonBlock) )
197 iRet |= O_NONBLOCK;
198 if( (iFlags&Exclusive) )
199 iRet |= O_EXCL;
200
201 return iRet;
179} 202}
180 203