buf_rdwr.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008-2009.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00018 #ifndef __DERS__BUF_RDWR_HPP__
00019 #define __DERS__BUF_RDWR_HPP__
00020 
00021 #include <ders/config.hpp>
00022 #include <ders/text.hpp>
00023 
00024 namespace ders {  // ::ders
00025 
00029 class readable : virtual public destroyable {
00030  public:
00031       virtual int read(void* buf, int n)=0;
00032 };
00033 
00037 class writable : virtual public destroyable {
00038  public:
00039       virtual int write(const void* buf, int n)=0;
00040 };
00041 
00046 class buf_reader {
00047       mem_pool& mp;
00048       readable& rd;
00049       unsigned char* buf;
00050       int bsz;
00051       int it, lim;
00052 
00053       int read_impl();
00054 
00055       buf_reader(const buf_reader&);
00056       buf_reader& operator=(const buf_reader&);
00057 
00058  public:
00059       buf_reader(mem_pool& m, readable& rdbl, int bufsz=4*1024);
00060       ~buf_reader() { mp.deallocate(buf, bsz); }
00061 
00062       int read(void* b, int n);
00063       int read(sh_text& buf, int n);
00064       int read() { return (it<lim) ? buf[it++] : read_impl(); }
00065       bool read_line(sh_text& line, bool noendl=true);
00066 
00067       void reset() { it=lim=0; }
00068 
00069       const char* begin() const { return reinterpret_cast<char*>(buf)+it; }
00070       const char* end() const { return reinterpret_cast<char*>(buf)+lim; }
00071 };
00072 
00077 class buf_writer {
00078       mem_pool& mp;
00079       writable& wr;
00080       unsigned char* buf;
00081       int bsz;
00082       int it;
00083 
00084       int write_impl(char ch);
00085 
00086       buf_writer(const buf_writer&);
00087       buf_writer& operator=(const buf_writer&);
00088 
00089  public:
00090       buf_writer(mem_pool& m, writable& wrbl, int bufsz=4*1024);
00091       ~buf_writer();
00092 
00093       int write(const void* b, int n);
00094       int write(const ch_rng& r) { return write(r.beg, r.size()); }
00095       int write(char ch) { return (it<bsz) ? buf[it++]=ch : write_impl(ch); }
00096       bool flush();
00097 
00098       void reset() { it=0; }
00099 
00100       const char* begin() const { return reinterpret_cast<char*>(buf); }
00101       const char* end() const { return reinterpret_cast<char*>(buf)+it; }
00102 };
00103 
00104 }  // namespace ::ders
00105 
00106 #endif  // __DERS__BUF_RDWR_HPP__
00107 

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5