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

FBwBlobStream.h

00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include <FBwBlob.h>
00005 
00006 namespace fbwrap
00007 {
00008 
00009 using std::basic_streambuf;
00010 using std::char_traits;
00011 
00012 
00013 template <typename _CharT, typename _Traits=char_traits<_CharT> >
00014 class  basic_blobbuf : public basic_streambuf<_CharT,_Traits>
00015 {
00016 public:
00017     typedef _CharT  char_type;
00018     typedef typename _Traits::int_type  int_type;
00019     typedef typename _Traits::pos_type  pos_type;
00020     typedef typename _Traits::off_type  off_type;
00021 private:
00022     char_type *buffer;
00023     int buffer_size;
00024     FBwBlob *blb;
00025 public:
00026     // Constructor
00027     basic_blobbuf(FBwBlob *b int size=2048) :
00028             basic_streambuf<_CharT,_Traits>()
00029     {
00030         buffer_size=(size > b->Size())?size:b->Size();
00031         buffer = new char_type[buffer_size];
00032         blb=b;
00033         setp(buffer,buffer+buffer_size);
00034     }
00035 protected:
00036     virtual int_type underflow()
00037     {
00038         // refill the buffer
00039         int avail=buffer_size;
00040         while(aval>=buffer_size)
00041         {
00042             int nLen=b->ReadSegment();
00043 
00044             
00045         setg(buffer,buffer,buffer+i);
00046         return ret;
00047     }
00048     virtual int_type overflow(int_type c)
00049     {
00050         char_type *p=pbase();
00051         while(p<pptr())
00052         {
00053             cout << *p++;
00054         }
00055         if(!_Traits::eq_int_type(c,_Traits::eof()))
00056             cout << _Traits::to_char_type(c);
00057         setp(buffer,buffer+32);
00058     }
00059     virtual ~basic_seqbuf()
00060     {
00061         if( pptr()>pbase() )
00062             overflow(_Traits::eof());
00063         delete[] buffer;
00064     }
00065 };
00066 
00067 template <typename _CharT, typename _Traits=char_traits<_CharT> >
00068 class seq_istream : public basic_istream<_CharT,_Traits>
00069 {
00070 public:
00071     seq_istream(_CharT limit):
00072             basic_istream<_CharT,_Traits>(new basic_seqbuf<_CharT,_Traits>(limit))
00073     {
00074     }
00075     virtual ~seq_istream()
00076     {
00077         delete rdbuf();
00078     }
00079 };
00080 template <typename _CharT, typename _Traits=char_traits<_CharT> >
00081 class seq_ostream : public basic_ostream<_CharT,_Traits>
00082 {
00083 public:
00084     typedef basic_seqbuf<_CharT,_Traits> seqbuf_type;
00085     seq_ostream(_CharT limit):
00086             basic_ostream<_CharT,_Traits>(new basic_seqbuf<_CharT,_Traits>(limit))
00087     {
00088     }
00089     virtual ~seq_ostream()
00090     {
00091         delete rdbuf();
00092     }
00093     void flush()
00094     {
00095         seqbuf_type *buf= static_cast<seqbuf_type *>(rdbuf());
00096         buf->overflow(_Traits::eof());
00097     }
00098 };
00099 
00100 int main()
00101 {
00102     seq_istream<char> char_seq('z');
00103     cout << "Testing file bufs" << endl;
00104     char c;
00105     cout << "Inicio" << endl;
00106     for(char_seq >> c;!char_seq.eof();char_seq >> c)
00107     {
00108         cout << c;
00109     }
00110     cout << endl;
00111     cout << "FIN DE ARCHIVO" << endl;
00112     cout << "Probando archivo de salida" << endl;
00113     seq_ostream<char> oseq('z');
00114     for(int i=0; i<65;i++)
00115     {
00116         oseq << i;
00117         if( (i%4)==0 )
00118             oseq << endl;
00119         else
00120             oseq << ',';
00121     }
00122     oseq.flush();
00123     return 0;
00124 }

Generated on Fri Jan 31 08:30:01 2003 for fbwrap by doxygen1.2.17