Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

FileDescriptor

00001 /*
00002  * NOTE: "zlib/libpng" style License
00003  *
00004  *                 ----=== s t d n e t ===----
00005  *
00006  * Copyright (c) 2002 Warwick Molloy   w-molloy@users.sf.net
00007  *
00008  * Copyright (c) 2002 Stdnet Community
00009  * HTTP://Sourceforge.net/projects/stdnet
00010  *
00011  * All references to "software" refer to the stdnet.
00012  *
00013  * This software is provided 'as-is', without any express or
00014  * implied warranty. In no event will the authors be held liable
00015  * for any damages arising from the use of this software.
00016  *
00017  * Permission is granted to anyone to use this software for any
00018  * purpose, including commercial applications, and to alter it
00019  * and redistribute it freely, subject to the following
00020  * restrictions:
00021  *
00022  * 1.  The origin of this software must not be misrepresented;
00023  *     you must not claim that you wrote the original software.
00024  *     If you use this software in a product, an acknowledgment
00025  *     in the product documentation would be appreciated but
00026  *     is not required.
00027  *
00028  * 2.  Altered source versions must be plainly marked as such,
00029  *     and must not be misrepresented as being the original
00030  *     software.
00031  *
00032  * 3.  This notice may not be removed or altered from any source
00033  *     distribution.
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, // -- read/write mask
00059         
00060         BIN     =   0x0004,
00061         TEXT    =   0x0008,
00062         TYPEMASK=   0x000C, // -- content type mask
00063         
00064         CREATE  =   0x0010,
00065         APPEND  =   0x0020,
00066         OPNMASK =   0x0030, // -- open method mask
00067 
00068         // -- combinations --
00069         //
00070         CREATE_WRITE_BIN    =   CREATE | WRITE | BIN,
00071         CREATE_WRITE_TEXT   =   CREATE | WRITE | TEXT,
00072 
00073         APPEND_WRITE_BIN    =   APPEND | WRITE | BIN,
00074         APPEND_WRITE_TEXT   =   APPEND | WRITE | TEXT,
00075 
00076         CREATE_RDWR_BIN     =   CREATE | READ | WRITE | BIN,
00077         CREATE_RDWR_TEXT    =   CREATE | READ | WRITE | TEXT,
00078 
00079         APPEND_RDWR_BIN     =   APPEND | WRITE | BIN,
00080         APPEND_RDWR_TEXT    =   APPEND | WRITE | TEXT,
00081 
00082         OPEN_READ_BIN       =   READ | BIN,
00083         OPEN_READ_TEXT      =   READ | TEXT
00084         
00085     };
00086 
00087     struct  StdOut  {};
00088     struct  StdErr  {};
00089     struct  StdIn   {};
00090     
00091 public:
00092     FileDescriptor() : file(0), constraints(CLOSED) {};
00093     ~FileDescriptor() { close(); }
00094 
00100     inline bool    isOpen();
00101     
00107     inline bool    open( char * name, Flags f);
00108 
00112     inline bool    open( StdOut );
00113     
00117     inline bool    open( StdErr );
00118 
00122     inline bool    open( StdIn );
00123 
00129     inline bool    close();
00130     
00136     inline bool    write( char * buffer, int len);
00137 
00142     inline int     read( char * buffer, int max);
00143 
00149     inline bool    flush();
00150 
00155     inline bool     eof();
00156 
00157 private:
00158     FILE *  file;
00159     Flags   constraints;
00160 
00161 public:
00162     static FileDescriptor   stdinFileDescriptor;
00163     static FileDescriptor   stdoutFileDescriptor;
00164     static FileDescriptor   stderrFileDescriptor;
00165 
00166 private:
00167     FileDescriptor( StdIn ) : file(0), constraints(CLOSED)
00168     { open( StdIn() ); }
00169     
00170     FileDescriptor( StdOut) : file(0), constraints(CLOSED)
00171     { open( StdOut() ); }
00172 
00173     FileDescriptor( StdErr ) : file(0), constraints(CLOSED)
00174     { open( StdErr() ); }
00175 };
00176 
00177 bool FileDescriptor::
00178     isOpen()
00179 {
00180     return constraints != CLOSED;
00181 }
00182 
00183 bool FileDescriptor::
00184     open( char * name, Flags f)
00185 {
00186     int     index = 0;
00187     char    extra[ 10];
00188 
00189     //  If the file is already open (file != 0) then set fail.
00190     bool    fail = (file != 0) ? true : false;
00191     
00192     if ( (f & RDWRMASK) == READ && ! fail)
00193     {
00194         extra[index++] = 'r';
00195     }
00196     else if ( (f & RDWRMASK) == WRITE)
00197     {
00198         extra[index++] = 'w';
00199     }
00200     else
00201     {
00202         fail = true;
00203     }
00204 
00205     if ( (f & TYPEMASK) == BIN && ! fail)
00206     {
00207         extra[index++] = 'b';
00208     }
00209     else if ( (f & TYPEMASK) == TEXT && ! fail)
00210     {
00211         extra[index++] = 't';
00212     }
00213     else
00214     {
00215         fail = true;
00216     }
00217     
00218     if ( (f & OPNMASK) == APPEND && ! fail)
00219     {
00220         extra[index++] = 'a';
00221     }
00222     else if ( (f & OPNMASK) == CREATE && ! fail)
00223     {
00224         //extra[index++] = '+';
00225     }
00226     else
00227     {
00228         fail = true;
00229     }
00230     extra[index] = 0;
00231     
00232     if ( ! fail )
00233     {
00234         file = fopen( name, extra);
00235         if ( file != 0 )
00236         {
00237             constraints = f;
00238         }
00239     }
00240     return file != 0 && ! fail;
00241 }
00242 
00243 bool FileDescriptor::
00244     open( StdOut )
00245 {
00246     file = stdout;
00247     constraints = WRITE;
00248     return true;
00249 }
00250 
00251 bool FileDescriptor::
00252     open( StdErr )
00253 {
00254     file = stderr;
00255     constraints = WRITE;
00256     return true;
00257 }
00258 
00259 bool FileDescriptor::
00260     open( StdIn )
00261 {
00262     file = stdin;
00263     constraints = READ;
00264     return true;
00265 }
00266 
00267 bool FileDescriptor::
00268     close()
00269 {
00270     if ( file == 0 )
00271         return false;
00272 
00273     return fclose( file ) == 0;
00274 }
00275 
00276 bool FileDescriptor::
00277     write( char * buffer, int len)
00278 {
00279     if ( file == 0 )
00280         return false;
00281     
00282     return fwrite( static_cast<void*>( buffer ), 1, len, file ) > 0;
00283 }
00284 
00285 int FileDescriptor::
00286     read( char * buffer, int max)
00287 {
00288     if ( file == 0)
00289         return false;
00290     return fread( static_cast<void*>(buffer), 1, max, file);
00291 }
00292 
00293 bool FileDescriptor::
00294     flush()
00295 {
00296     if ( file == 0 )
00297         return false;
00298     fflush( file );
00299     return true;
00300 }
00301 
00302 
00303 bool FileDescriptor::
00304     eof()
00305 {
00306     if ( file == 0 )
00307         return false;
00308     return feof( file ) > 0;
00309 }
00310 
00311         } // -- end platform --
00312     } // ------ end system --
00313 } // ---------- end stdbase --
00314 
00315 #endif      //  __system_platform_FileDescriptor__
00316 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001