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 
00151 private:
00152     FILE *  file;
00153     Flags   constraints;
00154 
00155 public:
00156     static FileDescriptor   stdinFileDescriptor;
00157     static FileDescriptor   stdoutFileDescriptor;
00158     static FileDescriptor   stderrFileDescriptor;
00159 
00160 private:
00161     FileDescriptor( StdIn ) : file(0), constraints(CLOSED)
00162     { open( StdIn() ); }
00163     
00164     FileDescriptor( StdOut) : file(0), constraints(CLOSED)
00165     { open( StdOut() ); }
00166 
00167     FileDescriptor( StdErr ) : file(0), constraints(CLOSED)
00168     { open( StdErr() ); }
00169 };
00170 
00171 bool FileDescriptor::
00172     isOpen()
00173 {
00174     return constraints != CLOSED;
00175 }
00176 
00177 bool FileDescriptor::
00178     open( char * name, Flags f)
00179 {
00180     int     index = 0;
00181     char    extra[ 10];
00182 
00183     //  If the file is already open (file != 0) then set fail.
00184     bool    fail = (file != 0) ? true : false;
00185     
00186     if ( (f & RDWRMASK) == READ && ! fail)
00187     {
00188         extra[index++] = 'r';
00189     }
00190     else if ( (f & RDWRMASK) == WRITE)
00191     {
00192         extra[index++] = 'w';
00193     }
00194     else
00195     {
00196         fail = true;
00197     }
00198 
00199     if ( (f & TYPEMASK) == BIN && ! fail)
00200     {
00201         extra[index++] = 'b';
00202     }
00203     else if ( (f & TYPEMASK) == TEXT && ! fail)
00204     {
00205         extra[index++] = 't';
00206     }
00207     else
00208     {
00209         fail = true;
00210     }
00211     
00212     if ( (f & OPNMASK) == APPEND && ! fail)
00213     {
00214         extra[index++] = 'a';
00215     }
00216     else if ( (f & OPNMASK) == CREATE && ! fail)
00217     {
00218         //extra[index++] = '+';
00219     }
00220     else
00221     {
00222         fail = true;
00223     }
00224     extra[index] = 0;
00225     
00226     if ( ! fail )
00227     {
00228         file = fopen( name, extra);
00229         if ( file != 0 )
00230         {
00231             constraints = f;
00232         }
00233     }
00234     return file != 0 && ! fail;
00235 }
00236 
00237 bool FileDescriptor::
00238     open( StdOut )
00239 {
00240     file = stdout;
00241     constraints = WRITE;
00242     return true;
00243 }
00244 
00245 bool FileDescriptor::
00246     open( StdErr )
00247 {
00248     file = stderr;
00249     constraints = WRITE;
00250     return true;
00251 }
00252 
00253 bool FileDescriptor::
00254     open( StdIn )
00255 {
00256     file = stdin;
00257     constraints = READ;
00258     return true;
00259 }
00260 
00261 bool FileDescriptor::
00262     close()
00263 {
00264     if ( file == 0 )
00265         return false;
00266 
00267     return fclose( file ) == 0;
00268 }
00269 
00270 bool FileDescriptor::
00271     write( char * buffer, int len)
00272 {
00273     if ( file == 0 )
00274         return false;
00275     
00276     return fwrite( static_cast<void*>( buffer ), 1, len, file ) > 0;
00277 }
00278 
00279 int FileDescriptor::
00280     read( char * buffer, int max)
00281 {
00282     if ( file == 0)
00283         return false;
00284     return fread( static_cast<void*>(buffer), 1, max, file);
00285 }
00286 
00287 bool FileDescriptor::
00288     flush()
00289 {
00290     if ( file == 0 )
00291         return false;
00292     fflush( file );
00293     return true;
00294 }
00295 
00296 
00297         } // -- end platform --
00298     } // ------ end system --
00299 } // ---------- end stdbase --
00300 
00301 #endif      //  __system_platform_FileDescriptor__
00302 

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