diff options
Diffstat (limited to 'src/file.cpp')
-rw-r--r-- | src/file.cpp | 111 |
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 | ||
14 | namespace Bu { subExceptionDef( FileException ) } | 15 | namespace Bu { subExceptionDef( FileException ) } |
15 | 16 | ||
16 | Bu::File::File( const char *sName, const char *sFlags ) | 17 | Bu::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 | ||
25 | Bu::File::File( const Bu::FString &sName, const char *sFlags ) | 28 | Bu::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 | |||
34 | Bu::File::File( int fd, const char *sFlags ) | ||
35 | { | ||
36 | fh = fdopen( fd, sFlags ); | ||
37 | } | 31 | } |
38 | 32 | ||
39 | Bu::File::~File() | 33 | Bu::File::~File() |
@@ -43,69 +37,68 @@ Bu::File::~File() | |||
43 | 37 | ||
44 | void Bu::File::close() | 38 | void 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 | ||
53 | size_t Bu::File::read( void *pBuf, size_t nBytes ) | 51 | size_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 | ||
66 | size_t Bu::File::write( const void *pBuf, size_t nBytes ) | 59 | size_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 | ||
74 | long Bu::File::tell() | 67 | long 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 | ||
82 | void Bu::File::seek( long offset ) | 75 | void 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 | ||
90 | void Bu::File::setPos( long pos ) | 83 | void 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 | ||
98 | void Bu::File::setPosEnd( long pos ) | 91 | void 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 | ||
106 | bool Bu::File::isEOS() | 99 | bool Bu::File::isEOS() |
107 | { | 100 | { |
108 | return feof( fh ); | 101 | return false; |
109 | } | 102 | } |
110 | 103 | ||
111 | bool Bu::File::canRead() | 104 | bool 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 |
160 | void Bu::File::truncate( long nSize ) | 153 | void Bu::File::truncate( long nSize ) |
161 | { | 154 | { |
162 | ftruncate( fileno( fh ), nSize ); | 155 | ftruncate( fd, nSize ); |
163 | } | 156 | } |
164 | 157 | ||
165 | void Bu::File::chmod( mode_t t ) | 158 | void 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 | ||
171 | void Bu::File::flush() | 164 | void Bu::File::flush() |
172 | { | 165 | { |
173 | fflush( fh ); | 166 | // There is no flushing with direct I/O... |
167 | //fflush( fh ); | ||
174 | } | 168 | } |
175 | 169 | ||
176 | bool Bu::File::isOpen() | 170 | bool Bu::File::isOpen() |
177 | { | 171 | { |
178 | return (fh != NULL); | 172 | return (fd > -1); |
173 | } | ||
174 | |||
175 | int 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 | ||