/home/vlg/develop/libASSA/libassa/assa/Streambuf.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                         Streambuf.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (c) 1999 by Vladislav Grinchenko
00006 //
00007 //  This library is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU Library General Public
00009 //  License as published by the Free Software Foundation; either
00010 //  version 2 of the License, or (at your option) any later version.
00011 //------------------------------------------------------------------------------
00012 //  Created: 12/02/1999
00013 //------------------------------------------------------------------------------
00014 #ifndef STREAM_BUF_H
00015 #define STREAM_BUF_H
00016 
00017 #include <stdio.h>      // EOF
00018 #include "assa/Assure.h"        // trace() & Assert family
00019 
00020 namespace ASSA {
00021 
00030 class io_ptrs
00031 {
00032 public:
00033     char* m_read_base;
00034     char* m_read_ptr;
00035     char* m_read_end;
00036 
00037     char* m_write_base;
00038     char* m_write_ptr;
00039     char* m_write_end;
00040 
00041     char* m_buf_base;
00042     char* m_buf_end;
00043 
00044     enum { USER_BUF = 1, UNBUFFERED = 2, EOF_SEEN = 4, ERR_SEEN = 8 };
00045 
00046     int   m_flags;
00047     char  m_shortbuf[1];
00048 
00049     io_ptrs () : m_read_base (0), m_read_ptr (0), m_read_end (0),
00050              m_write_base (0), m_write_ptr (0), m_write_end (0),
00051              m_buf_base (0), m_buf_end (0), m_flags (0)
00052         { 
00053             m_shortbuf [0] = 0; 
00054         }
00055 
00056     void dump () const;
00057 };
00058 
00090 class Streambuf : public io_ptrs
00091 {
00092 public:
00093 
00094 //  Old comment:
00095 //
00096 //  "Maximum frame size that can be transmitted unfragmented by TCP
00097 //   with MTU 1500 (1500-20-60). TCP frame can have options
00098 //   (up to 60 bytes) which, if ignored, might cause fragmentation.
00099 //   Also, the length of the IP packet must be evenly divisible by 8."
00100 //
00101 //  On 100Mb networks, the reasonable buffer size seems to be 64K.
00102 
00106     static const int MAXTCPFRAMESZ = 65536; // 64K
00107 
00108     virtual ~Streambuf ();
00109 
00113     Streambuf* pubsetbuf (char* s_, int n_);
00114 
00119     int        pubsync ();
00120 
00126     int        in_avail ();
00127 
00138     int        snextc ();
00139 
00146     int        sbumpc ();
00147 
00153     int        sgetc ();
00154 
00162     int        sgetn (char* b_, int len_);
00163 
00170     int        sputc (char c_);
00171 
00179     int        sputn (char* b_, int len_);
00180 
00181 
00186     void       unbuffered (int i_);
00187     
00190     int        unbuffered ();
00191 
00192 protected:
00197     Streambuf ();
00198 
00199     Streambuf (const Streambuf&);
00200     Streambuf& operator= (const Streambuf&);
00201 
00205     char*      base () const;
00206 
00213     char*      gptr ()  const;
00214 
00218     char*      egptr () const;
00219 
00222     void       setg (char* gbeg_, char* gnext_, char* gend_);
00223 
00229     char*      pbase () const;
00230 
00235     char*      pptr ()  const;
00236 
00242     char*      epptr () const;
00243 
00246     void       setp (char* pbeg_, char* pend_);
00247 
00250     void       pbump (int n_);
00251 
00262     void       setb (char* b_, char* eb_, int del_);
00263 
00264     void       init ();
00265 
00266 protected:
00275     virtual Streambuf* setbuf (char* p_, int len_);
00276 
00288     virtual int        sync ();
00289     
00300     virtual int        showmanyc ();
00301 
00309     virtual int        xsgetn (char* b_, int len_);
00310 
00323     virtual int        underflow ();
00324 
00341     virtual int        uflow ();
00342 
00351     virtual int        xsputn (const char* b_, int len_);
00352 
00367     virtual int        overflow (int c = EOF);
00368 
00376     virtual int        doallocate ();
00377 };
00378 
00379 inline Streambuf* 
00380 Streambuf::
00381 pubsetbuf (char* s_, int n_) 
00382 { 
00383     trace_with_mask("Streambuf::pubsetbuf",STRMBUFTRACE);
00384     
00385     return setbuf (s_, n_); 
00386 }
00387 
00388 inline int        
00389 Streambuf::
00390 pubsync () 
00391 { 
00392     trace_with_mask("Streambuf::pubsync",STRMBUFTRACE);
00393 
00394     return sync (); 
00395 }
00396 
00397 inline int
00398 Streambuf::
00399 in_avail () 
00400 { 
00401     trace_with_mask("Streambuf::in_avail",STRMBUFTRACE);
00402 
00403     return m_read_end - m_read_ptr; 
00404 }
00405 
00406 inline int
00407 Streambuf::
00408 unbuffered () 
00409 { 
00410     trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
00411     
00412     return m_flags & UNBUFFERED ? 1 : 0; 
00413 }
00414 
00415 inline void
00416 Streambuf::
00417 unbuffered (int i_)
00418 {
00419     trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
00420 
00421     if (i_) 
00422         m_flags |= UNBUFFERED; // set
00423     else
00424         m_flags &= ~ UNBUFFERED; // unset
00425 }
00426 
00427 inline
00428 Streambuf::
00429 Streambuf () 
00430 { 
00431     trace_with_mask("Streambuf::Streambuf",STRMBUFTRACE);
00432 
00433     init (); 
00434 }
00435 
00436 inline void
00437 Streambuf::
00438 init ()
00439 {
00440     trace_with_mask("Streambuf::init", STRMBUFTRACE);
00441 
00442     m_read_base = m_read_ptr = m_read_end = 0;
00443     m_write_base = m_write_ptr = m_write_end = 0;
00444     m_buf_base = m_buf_end = 0;
00445     m_flags = 0;
00446 
00447     m_shortbuf[0] = 0;
00448 }
00449 
00450 inline 
00451 Streambuf::
00452 ~Streambuf ()
00453 {
00454     trace_with_mask("Streambuf::~Streambuf",STRMBUFTRACE);
00455 
00456     if (!(m_flags & USER_BUF)) {
00457         delete [] m_buf_base;
00458         m_buf_base = m_buf_end = 0;
00459     }
00460 }
00461 
00462 inline char*
00463 Streambuf::
00464 base () const 
00465 { 
00466     trace_with_mask("Streambuf::base",STRMBUFTRACE);
00467 
00468     return m_read_base; 
00469 }
00470 
00471 inline char* 
00472 Streambuf::
00473 gptr ()  const 
00474 { 
00475     trace_with_mask("Streambuf::gptr",STRMBUFTRACE);
00476 
00477     return m_read_ptr; 
00478 }
00479 
00480 inline char*
00481 Streambuf::
00482 egptr () const 
00483 { 
00484     trace_with_mask("Streambuf::egptr",STRMBUFTRACE);
00485 
00486     return m_read_end; 
00487 }
00488 
00489 inline char*      
00490 Streambuf::
00491 pbase () const 
00492 { 
00493     trace_with_mask("Streambuf::pbase",STRMBUFTRACE);
00494     return m_write_base; 
00495 }
00496 
00497 inline char*
00498 Streambuf::
00499 pptr ()  const 
00500 { 
00501     trace_with_mask("Streambuf::pptr",STRMBUFTRACE);
00502     return m_write_ptr; 
00503 }
00504 
00505 inline char*
00506 Streambuf::
00507 epptr () const 
00508 { 
00509     trace_with_mask("Streambuf::epptr",STRMBUFTRACE);
00510     return m_write_end; 
00511 }
00512 
00513 inline void       
00514 Streambuf::
00515 pbump (int n_) 
00516 { 
00517     trace_with_mask("Streambuf::pbump",STRMBUFTRACE);
00518     m_write_ptr += n_; 
00519 }
00520 
00521 inline int        
00522 Streambuf::
00523 sync () 
00524 { 
00525     trace_with_mask("Streambuf::sync",STRMBUFTRACE);
00526     return 0; 
00527 }
00528 
00529 inline int
00530 Streambuf::
00531 showmanyc () 
00532 { 
00533     trace_with_mask("Streambuf::showmanyc",STRMBUFTRACE);
00534     return -1; 
00535 }
00536 
00537 inline int 
00538 Streambuf::
00539 sbumpc ()
00540 {
00541     trace_with_mask("Streambuf::sbumpc",STRMBUFTRACE);
00542 
00543     return (m_read_ptr >= m_read_end ? uflow ()
00544             : *(unsigned char *) m_read_ptr++);
00545 }
00546 
00547 inline int
00548 Streambuf::
00549 sgetc ()
00550 {
00551     trace_with_mask("Streambuf::sgetc",STRMBUFTRACE);
00552 
00553     return (m_read_ptr >= m_read_end && underflow () == EOF 
00554             ? EOF : *(unsigned char*) m_read_ptr);
00555 }
00556 
00557 inline int 
00558 Streambuf::
00559 sgetn (char* data_, int len_)
00560 {
00561     trace_with_mask("Streambuf::sgetn",STRMBUFTRACE);
00562 
00563     return xsgetn (data_, len_);
00564 }
00565 
00566 inline int 
00567 Streambuf::
00568 sputc (char c_)
00569 {
00570     trace_with_mask("Streambuf::sputc",STRMBUFTRACE);
00571 
00572     return (m_write_ptr >= m_write_end
00573             ? overflow (c_)
00574             : (unsigned char) (*m_write_ptr++ = c_));
00575 }
00576 
00577 inline int
00578 Streambuf::
00579 sputn (char* b_, int len_)
00580 {
00581     trace_with_mask("Streambuf::sputn",STRMBUFTRACE);
00582 
00583     return xsputn (b_, len_);
00584 }
00585 
00586 inline void
00587 Streambuf::
00588 setp (char* pbeg_, char* pend_)
00589 {
00590     trace_with_mask("Streambuf::setp",STRMBUFTRACE);
00591 
00592     m_write_base = m_write_ptr = pbeg_;
00593     m_write_end  = pend_;
00594 }
00595 
00596 inline int
00597 Streambuf::
00598 underflow ()
00599 {
00600     trace_with_mask("Streambuf::underflow",STRMBUFTRACE);
00601 
00602     return (EOF);
00603 }
00604 
00605 inline int
00606 Streambuf::
00607 overflow (int /* c_ */)
00608 {
00609     trace_with_mask("Streambuf::overflow",STRMBUFTRACE);
00610 
00611     return (EOF);
00612 }
00613 
00614 } // end namespace ASSA
00615 
00616 #endif /* STREAM_BUF_H */  

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6