Back to index

courier  0.68.2
mio.h
Go to the documentation of this file.
00001 #ifndef       mio_h
00002 #define       mio_h
00003 
00004 
00005 #include      "config.h"
00006 #include      <sys/types.h>
00007 #include      <string.h>
00008 
00009 // Fault-tolerant wrappers around I/O functions - they automatically retry
00010 // when they get an EINTR (for kernels that do not restart the calls themselves)
00011 
00012 int mopen(const char *fname, int flags, mode_t mode);
00013 int mread(int fd, void *buf, size_t count);
00014 int mwrite(int fd, const void *buf, size_t count);
00015 int mclose(int fd);
00016 off_t mseek(int, off_t, int);
00017 
00019 //
00020 // Class Mio - reinventing the stdio wheel.
00021 //
00023 
00024 class Mio {
00025        int fd_;
00026        unsigned char *readptr;
00027        unsigned char *writeptr;
00028        off_t readstartpos;
00029        int readsize;
00030        int readcnt;
00031        int writecnt;
00032        int err;
00033        unsigned char buf[1024];
00034 public:
00035        Mio();
00036        virtual ~Mio();
00037        int Open(const char *, int, mode_t=0666);
00038        void Close();
00039        int peek() { return (readcnt ? *readptr:
00040                      fill() < 0 ? -1:(++readcnt,*--readptr)); }
00041        int get() { return (readcnt ? (--readcnt,*readptr++):fill()); }
00042        int put(int c) { return (writecnt ?
00043                             (--writecnt,*writeptr++=c):flush(c)); }
00044        int seek(off_t, int);
00045        int Rewind();
00046        off_t tell();
00047        virtual int write(const void *, int);
00048        int fd() { return (fd_); }
00049        void fd(int f) { Close();
00050                      err=0;
00051                      readcnt=0;
00052                      writecnt=0;
00053                      readptr=buf;
00054                      writeptr=buf;
00055                      fd_=f; }
00056 
00057        int errflag() { return (err); }
00058        void my_clearerr() { err=0; }
00059        int flush() { return (flush(-1)); }
00060 
00061        void write(const char *p) { write(p, strlen(p)); }
00062 
00063        Mio &operator<<(const char *p) { write(p); return (*this); }
00064        Mio &operator<<(const class Buffer &);
00065 
00066 private:
00067        int fill();
00068        int flush(int);
00069 } ;
00070 
00071 class MioStdio : public Mio {
00072 public:
00073        MioStdio(int);
00074        ~MioStdio();
00075        int write(const void *, int);
00076        void write(const char *p) { write(p, strlen(p)); }
00077 } ;
00078 
00079 extern MioStdio mout, merr;
00080 
00081 #endif