lib/fdbuf/fdobuf.h
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #ifndef FDBUF__FDOBUF__H__
       
    18 #define FDBUF__FDOBUF__H__
       
    19 
       
    20 class fdobuf : protected fdbuf
       
    21 {
       
    22 public:
       
    23   enum openflags { create=O_CREAT, 
       
    24 		   excl=O_EXCL,
       
    25 		   trunc=O_TRUNC,
       
    26 		   append=O_APPEND };
       
    27 
       
    28   fdobuf(const char* filename, int, int mode = 0666,
       
    29 	 unsigned bufsz = FDBUF_SIZE);
       
    30   fdobuf(int fdesc, bool dc=false, unsigned bufsz = FDBUF_SIZE);
       
    31   virtual ~fdobuf();
       
    32   bool close();
       
    33   bool operator!() const;
       
    34   operator bool() const
       
    35     {
       
    36       return !operator!();
       
    37     }
       
    38   bool flush();
       
    39   bool sync();
       
    40   virtual bool write(char);
       
    41   bool write(unsigned char c) { return write((char)c); }
       
    42   bool write(signed char c) { return write((char)c); }
       
    43   virtual bool write(const char*, unsigned);
       
    44   bool write(const unsigned char* b, unsigned l) { return write((char*)b, l); }
       
    45   bool write(const signed char* b, unsigned l) { return write((char*)b, l); }
       
    46   virtual bool write_large(const char*, unsigned);
       
    47   unsigned last_count() { return count; }
       
    48   bool seek(unsigned o);
       
    49   bool rewind() { return seek(0); }
       
    50   unsigned tell() const { return offset + bufpos; }
       
    51 
       
    52   bool chown(uid_t, gid_t) const;
       
    53   bool chmod(mode_t) const;
       
    54   
       
    55   fdobuf& operator<<(const char* str)
       
    56     {
       
    57       write(str, strlen(str));
       
    58       return *this;
       
    59     }
       
    60   fdobuf& operator<<(char ch)
       
    61     {
       
    62       write(ch);
       
    63       return *this;
       
    64     }
       
    65   fdobuf& operator<<(fdobuf& (*manip)(fdobuf&))
       
    66     {
       
    67       return manip(*this);
       
    68     }
       
    69   fdobuf& operator<<(unsigned long);
       
    70   fdobuf& operator<<(signed long);
       
    71   fdobuf& operator<<(unsigned i) { return operator<<((unsigned long)i); }
       
    72   fdobuf& operator<<(signed i) { return operator<<((signed long)i); }
       
    73   fdobuf& operator<<(unsigned short i) { return operator<<((unsigned long)i); }
       
    74   fdobuf& operator<<(signed short i) { return operator<<((signed long)i); }
       
    75 
       
    76   int error_number() const { return errnum; }
       
    77 protected:
       
    78   virtual bool nflush(bool withsync);
       
    79 
       
    80   unsigned bufpos;		// Current write position in the buffer
       
    81   unsigned count;		// Number of bytes written by last operation
       
    82 };
       
    83 
       
    84 fdobuf& endl(fdobuf& fd);
       
    85 
       
    86 extern fdobuf fout;
       
    87 extern fdobuf ferr;
       
    88 
       
    89 #endif // FDBUF__FDOBUF__H__