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

miodevice.h

Go to the documentation of this file.
00001 
00025 #ifndef __MAGIC_IODEVICE_H__
00026 #define __MAGIC_IODEVICE_H__
00027 
00028 #include <magic/mobject.h>
00029 #include <magic/mlist.h>
00030 #include <magic/mexception.h>
00031 
00032 
00033 // I/O Modes
00034 
00035 BEGIN_NAMESPACE (MagiC);
00036 
00037 #ifdef IO_Raw
00038 #undef IO_Raw
00039 #undef IO_Readable
00040 #undef IO_ReadWrite
00041 #undef IO_Truncate
00042 #undef IO_Open
00043 #undef IO_Append
00044 #undef IO_Ok
00045 #undef IO_ReadError
00046 #undef IO_WriteError
00047 #undef IO_FatalError
00048 #undef IO_OpenError
00049 #undef IO_ConnectError
00050 #undef IO_AbortError
00051 #undef IO_TimeOutError
00052 #undef IO_OnCloseError
00053 #endif
00054 
00055 enum io_modes {
00056     IO_Raw          = 0x00000001,   
00057     IO_Readable     = 0x00000002,   
00058     IO_Writable     = 0x00000004,   
00059     IO_ReadWrite    = 0x00000006,   
00060     IO_Append       = 0x00000008,   
00061     IO_Truncate     = 0x00000010    
00062 };
00063     
00064 // I/O Types
00065 
00066 // I/O States
00067 enum io_states {
00068     IO_Open         = 0x00000100,   
00069     IO_EOS          = 0x00000200    
00070 };
00071 
00072 // I/O Statuses
00073 enum io_statuses {
00074     IO_Ok           = 0x00000000,   
00075     IO_ReadError    = 0x00000001,   
00076     IO_WriteError   = 0x00000002,   
00077     IO_FatalError   = 0x00000003,   
00078     IO_OpenError    = 0x00000004,   
00079     IO_ConnectError = 0x00000005,   
00080     IO_AbortError   = 0x00000006,
00081     IO_TimeOutError = 0x00000007,   
00082     IO_OnCloseError = 0x00000008,   
00083 };
00084 
00085 
00087 //                 ---  ___  ___               o                             //
00088 //                  |  |   | |  \   ___           ___   ___                  //
00089 //                  |  |   | |   | /   ) |   | | |   \ /   )                 //
00090 //                  |  |   | |   | |---   \ /  | |     |---                  //
00091 //                 _|_ `___´ |__/   \__    V   |  \__/  \__                  //
00093 
00096 class IODevice : public Object {
00097   public:
00098                         IODevice            ();
00099     virtual             ~IODevice           ();
00100     int                 flags               () const {return mFlags;}
00101     int                 mode                () const {return mMode;}
00102 
00103     bool                isDirectAccess      () const;
00104     bool                isSequentialAccess  () const;
00105     bool                isBuffered          () const {return !(mMode & IO_Raw);}
00106     bool                isRaw               () const {return mMode & IO_Raw;}
00107     bool                isSynchronous       () const;
00108     bool                isAsynchronous      () const;
00109     bool                isReadable          () const {return mMode & IO_Readable;}
00110     bool                isWritable          () const {return mMode & IO_Writable;}
00111     bool                isReadWrite         () const {return mMode & (IO_Readable | IO_Writable);}
00112     bool                isClosed            () const {return !(mState & IO_Open);}
00113     bool                isOpen              () const {return mState & IO_Open;}
00114 
00115     int                 status              () const {return mStatus;}
00116     void                resetStatus         () {mStatus = IO_Ok;}
00117     int                 state               () const {return mState;}
00118     virtual bool        open                (int mode);
00119     virtual void        close               ();
00120     virtual void        flush               ();
00121     virtual uint        size                () const;
00122     virtual int         at                  () const;
00123     virtual bool        atEnd               () const;
00124     virtual bool        seek                (int);
00125     bool                reset               ();
00126 
00127     virtual int         readBlock           (char* data, uint maxlen) throw (device_not_open);
00128     virtual int         readLine            (char* data, uint maxlen) throw (device_not_open);
00129     Ref<String>         readAll             () throw (device_not_open);
00130     List<String>*       readLines           () throw (device_not_open);
00131     virtual int         writeBlock          (const char* data, uint len) throw (device_not_open);
00132     int                 writeBlock          (const String&) throw (device_not_open);
00133     virtual int         getch               () throw (device_not_open);
00134     virtual void        putch               (char ch) throw (device_not_open);
00135     virtual void        ungetch             (char ch) throw (device_not_open);
00136 
00137   protected:
00138     void                setFlags            (int flags) {mFlags = flags;}
00139     void                setType             (int ftype) {mType = ftype;}
00140     void                setMode             (int fmode) {mMode = fmode;}
00141     void                setStatus           (int fstatus) {mStatus = fstatus;}
00142     void                setState            (int fstate, bool on=true) {if (on) mState |= fstate; else mState &= ~fstate;}
00143 
00144     void                setOpen             () {mState |= IO_Open;}
00145     void                setClosed           () {mState &= ~IO_Open;}
00146 
00147   protected:
00148     void                addStream           () {mStreams++;}
00149     void                removeStream        () {mStreams--;}
00150     int                 streamCount         () const {return mStreams;}
00151 
00152   private:
00153     int     mFlags;
00154     int     mType;
00155     int     mMode;
00156     int     mState;
00157     int     mStatus;
00158     int     mStreams;
00159 };
00160 
00161 
00162 
00164 //                              ----- o |                                    //
00165 //                              |       |  ___                               //
00166 //                              |---  | | /   )                              //
00167 //                              |     | | |---                               //
00168 //                              |     | |  \__                               //
00170 
00171 EXCEPTIONCLASS (file_already_open);
00172 
00188 class File : public IODevice {
00189   public:
00190                         File        (FILE* str=stdout);
00191                         File        (const String& name, int mode=0) throw (open_failure);
00192 
00193     const String&       name        () const {return mName;}
00194     void                setName     (const String& name);
00195     bool                exists      () const throw (system_failure);
00196     bool                remove      ();
00197     virtual bool        open        (int mode);
00198     virtual void        close       ();
00199     virtual void        flush       ();
00200     virtual uint        size        () const;
00201     virtual int         at          () const;
00202     virtual bool        seek        (int position);
00203     virtual bool        atEnd       () const;
00204     virtual int         readBlock   (char* data, uint len) throw (device_not_open);
00205     virtual int         readLine    (char* data, uint maxlen) throw (device_not_open);
00206     int                 readLine    (String& str, int maxlen=-1) throw (device_not_open);
00207     virtual int         writeBlock  (const char* data, uint len) throw (device_not_open);
00208     virtual int         getch       () throw (device_not_open);
00209     virtual void        putch       (char) throw (device_not_open);
00210     virtual void        ungetch     (char) throw (device_not_open);
00211     int                 handle      ();
00212 
00213   private:
00214     FILE*   mpFile;     
00215     int     mFd;        
00216     String  mName;      
00218     static String       translateToFOpenMode    (int mode) throw (invalid_flags);
00219     static int          translateToOpenMode     (int mode) throw (invalid_flags);
00220 };
00221 
00222 
00223 
00225 //                       ----                                                //
00226 //                       |   )        __  __  ___                            //
00227 //                       |---  |   | /   /   /   ) |/\                       //
00228 //                       |   ) |   | +-- +-- |---  |                         //
00229 //                       |___   \__! |   |    \__  |                         //
00230 //                                   |   |                                   //
00232 
00244 class Buffer : public IODevice {
00245   public:
00246                         Buffer              (int mode = IO_ReadWrite);
00247                         Buffer              (String& buffer, int mode = IO_ReadWrite);
00248                         ~Buffer             ();
00249 
00250     virtual bool        open                (int mode);
00251     virtual uint        size                () const                    {return mpBuffer? mpBuffer->length() : 0;}
00252     virtual int         at                  () const                    {return mPosition;}
00253     bool                reset               ();
00254     virtual bool        atEnd               () const;
00255     virtual int         readBlock           (char* data, uint maxlen) throw (device_not_open);
00256     virtual int         writeBlock          (const char* data, uint len) throw (device_not_open);
00257     int                 writeBlock          (const String&) throw (device_not_open);
00258     virtual int         readLine            (char* data, uint maxlen) throw (device_not_open);
00259     virtual int         getch               () throw (device_not_open);
00260     virtual void        putch               (char ch) throw (device_not_open);
00261     virtual void        ungetch             (char ch) throw (device_not_open);
00262     const String&       getBuffer           () const {return *mpBuffer;}
00263     
00264   protected:
00265     String* mpBuffer;
00266     bool    mOwnBuffer;
00267     int     mPosition;
00268 };
00269 
00270 END_NAMESPACE;
00271 
00272 #endif
00273 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18