00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef __system_platform_FileDescriptor__
00037 #define __system_platform_FileDescriptor__
00038
00039 #include <stdio.h>
00040
00041 namespace stdbase {
00042 namespace system {
00043 namespace platform {
00044
00049 class FileDescriptor
00050 {
00051 public:
00052 enum Flags
00053 {
00054 CLOSED = 0,
00055
00056 READ = 0x0001,
00057 WRITE = 0x0002,
00058 RDWRMASK= 0x0003,
00059
00060 BIN = 0x0004,
00061 TEXT = 0x0008,
00062 TYPEMASK= 0x000C,
00063
00064 CREATE = 0x0010,
00065 APPEND = 0x0020,
00066 OPEN = 0x0040,
00067 OPNMASK = 0x0070,
00068
00069
00070
00071 CREATE_WRITE_BIN = CREATE | WRITE | BIN,
00072 CREATE_WRITE_TEXT = CREATE | WRITE | TEXT,
00073
00074 APPEND_WRITE_BIN = APPEND | WRITE | BIN,
00075 APPEND_WRITE_TEXT = APPEND | WRITE | TEXT,
00076
00077 CREATE_RDWR_BIN = CREATE | READ | WRITE | BIN,
00078 CREATE_RDWR_TEXT = CREATE | READ | WRITE | TEXT,
00079
00080 APPEND_RDWR_BIN = APPEND | WRITE | BIN,
00081 APPEND_RDWR_TEXT = APPEND | WRITE | TEXT,
00082
00083 OPEN_READ_BIN = OPEN | READ | BIN,
00084 OPEN_READ_TEXT = OPEN | READ | TEXT
00085
00086 };
00087
00088 struct StdOut {};
00089 struct StdErr {};
00090 struct StdIn {};
00091
00092 public:
00093 FileDescriptor() : file(0), constraints(CLOSED) {};
00094 ~FileDescriptor() { close(); }
00095
00101 inline bool isOpen();
00102
00108 inline bool open( char * name, Flags f);
00109
00113 inline bool open( StdOut );
00114
00118 inline bool open( StdErr );
00119
00123 inline bool open( StdIn );
00124
00130 inline bool close();
00131
00137 inline bool write( char * buffer, int len);
00138
00143 inline int read( char * buffer, int max);
00144
00150 inline bool flush();
00151
00156 inline bool eof();
00157
00158 private:
00159 FILE * file;
00160 Flags constraints;
00161
00162 public:
00163 static FileDescriptor stdinFileDescriptor;
00164 static FileDescriptor stdoutFileDescriptor;
00165 static FileDescriptor stderrFileDescriptor;
00166
00167 private:
00168 FileDescriptor( StdIn ) : file(0), constraints(CLOSED)
00169 { open( StdIn() ); }
00170
00171 FileDescriptor( StdOut) : file(0), constraints(CLOSED)
00172 { open( StdOut() ); }
00173
00174 FileDescriptor( StdErr ) : file(0), constraints(CLOSED)
00175 { open( StdErr() ); }
00176 };
00177
00178 bool FileDescriptor::
00179 isOpen()
00180 {
00181 return constraints != CLOSED;
00182 }
00183
00184 bool FileDescriptor::
00185 open( char * name, Flags f)
00186 {
00187 int index = 0;
00188 char extra[ 10];
00189
00190
00191 bool fail = (file != 0) ? true : false;
00192
00193 if ( (f & RDWRMASK) == READ && ! fail)
00194 {
00195 extra[index++] = 'r';
00196 }
00197 else if ( (f & RDWRMASK) == WRITE)
00198 {
00199 extra[index++] = 'w';
00200 }
00201 else
00202 {
00203 fail = true;
00204 }
00205
00206 if ( (f & TYPEMASK) == BIN && ! fail)
00207 {
00208 extra[index++] = 'b';
00209 }
00210 else if ( (f & TYPEMASK) == TEXT && ! fail)
00211 {
00212 extra[index++] = 't';
00213 }
00214 else
00215 {
00216 fail = true;
00217 }
00218
00219 if ( (f & OPNMASK) == APPEND && ! fail)
00220 {
00221 extra[index++] = 'a';
00222 }
00223 else if ( (f & OPNMASK) == CREATE && ! fail)
00224 {
00225
00226 }
00227 else if ( (f & OPNMASK) == OPEN && ! fail )
00228 {
00229
00230 }
00231 else
00232 {
00233 fail = true;
00234 }
00235 extra[index] = 0;
00236
00237 if ( ! fail )
00238 {
00239 file = fopen( name, extra);
00240 if ( file != 0 )
00241 {
00242 constraints = f;
00243 }
00244 }
00245 return file != 0 && ! fail;
00246 }
00247
00248 bool FileDescriptor::
00249 open( StdOut )
00250 {
00251 file = stdout;
00252 constraints = WRITE;
00253 return true;
00254 }
00255
00256 bool FileDescriptor::
00257 open( StdErr )
00258 {
00259 file = stderr;
00260 constraints = WRITE;
00261 return true;
00262 }
00263
00264 bool FileDescriptor::
00265 open( StdIn )
00266 {
00267 file = stdin;
00268 constraints = READ;
00269 return true;
00270 }
00271
00272 bool FileDescriptor::
00273 close()
00274 {
00275 if ( file == 0 )
00276 return false;
00277
00278 return fclose( file ) == 0;
00279 }
00280
00281 bool FileDescriptor::
00282 write( char * buffer, int len)
00283 {
00284 if ( file == 0 )
00285 return false;
00286
00287 return fwrite( static_cast<void*>( buffer ), 1, len, file ) > 0;
00288 }
00289
00290 int FileDescriptor::
00291 read( char * buffer, int max)
00292 {
00293 if ( file == 0)
00294 return 0;
00295 return fread( static_cast<void*>(buffer), 1, max, file);
00296 }
00297
00298 bool FileDescriptor::
00299 flush()
00300 {
00301 if ( file == 0 )
00302 return false;
00303 fflush( file );
00304 return true;
00305 }
00306
00307 bool FileDescriptor::
00308 eof()
00309 {
00310 if ( file == 0 )
00311 return false;
00312 return feof( file ) > 0;
00313 }
00314
00315 }
00316 }
00317 }
00318
00319 #endif // __system_platform_FileDescriptor__
00320