libassa  3.5.1
Streambuf.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Streambuf.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Created: 12/02/1999
13 //------------------------------------------------------------------------------
14 #ifndef STREAM_BUF_H
15 #define STREAM_BUF_H
16 
17 #include <stdio.h> // EOF
18 #include "assa/Assure.h" // trace() & Assert family
19 
20 namespace ASSA {
21 
30 class io_ptrs
31 {
32 public:
33  char* m_read_base;
34  char* m_read_ptr;
35  char* m_read_end;
36 
37  char* m_write_base;
38  char* m_write_ptr;
39  char* m_write_end;
40 
41  char* m_buf_base;
42  char* m_buf_end;
43 
44  enum { USER_BUF = 1, UNBUFFERED = 2, EOF_SEEN = 4, ERR_SEEN = 8 };
45 
46  int m_flags;
47  char m_shortbuf[1];
48 
50  m_write_base (0), m_write_ptr (0), m_write_end (0),
51  m_buf_base (0), m_buf_end (0), m_flags (0)
52  {
53  m_shortbuf [0] = 0;
54  }
55 
56  void dump () const;
57 };
58 
90 class Streambuf : public io_ptrs
91 {
92 public:
93 
94 // Old comment:
95 //
96 // "Maximum frame size that can be transmitted unfragmented by TCP
97 // with MTU 1500 (1500-20-60). TCP frame can have options
98 // (up to 60 bytes) which, if ignored, might cause fragmentation.
99 // Also, the length of the IP packet must be evenly divisible by 8."
100 //
101 // On 100Mb networks, the reasonable buffer size seems to be 64K.
102 
106  static const int MAXTCPFRAMESZ = 65536; // 64K
107 
108  virtual ~Streambuf ();
109 
113  Streambuf* pubsetbuf (char* s_, int n_);
114 
119  int pubsync ();
120 
126  int in_avail ();
127 
138  int snextc ();
139 
146  int sbumpc ();
147 
153  int sgetc ();
154 
162  int sgetn (char* b_, int len_);
163 
170  int sputc (char c_);
171 
179  int sputn (char* b_, int len_);
180 
181 
186  void unbuffered (int i_);
187 
190  int unbuffered ();
191 
192 protected:
197  Streambuf ();
198 
201 
205  char* base () const;
206 
213  char* gptr () const;
214 
218  char* egptr () const;
219 
222  void setg (char* gbeg_, char* gnext_, char* gend_);
223 
229  char* pbase () const;
230 
235  char* pptr () const;
236 
242  char* epptr () const;
243 
246  void setp (char* pbeg_, char* pend_);
247 
250  void pbump (int n_);
251 
262  void setb (char* b_, char* eb_, int del_);
263 
264  void init ();
265 
266 protected:
275  virtual Streambuf* setbuf (char* p_, int len_);
276 
288  virtual int sync ();
289 
300  virtual int showmanyc ();
301 
309  virtual int xsgetn (char* b_, int len_);
310 
323  virtual int underflow ();
324 
341  virtual int uflow ();
342 
351  virtual int xsputn (const char* b_, int len_);
352 
367  virtual int overflow (int c = EOF);
368 
376  virtual int doallocate ();
377 };
378 
379 inline Streambuf*
381 pubsetbuf (char* s_, int n_)
382 {
383  trace_with_mask("Streambuf::pubsetbuf",STRMBUFTRACE);
384 
385  return setbuf (s_, n_);
386 }
387 
388 inline int
390 pubsync ()
391 {
392  trace_with_mask("Streambuf::pubsync",STRMBUFTRACE);
393 
394  return sync ();
395 }
396 
397 inline int
399 in_avail ()
400 {
401  trace_with_mask("Streambuf::in_avail",STRMBUFTRACE);
402 
403  return m_read_end - m_read_ptr;
404 }
405 
406 inline int
408 unbuffered ()
409 {
410  trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
411 
412  return m_flags & UNBUFFERED ? 1 : 0;
413 }
414 
415 inline void
417 unbuffered (int i_)
418 {
419  trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
420 
421  if (i_)
422  m_flags |= UNBUFFERED; // set
423  else
424  m_flags &= ~ UNBUFFERED; // unset
425 }
426 
427 inline
429 Streambuf ()
430 {
431  trace_with_mask("Streambuf::Streambuf",STRMBUFTRACE);
432 
433  init ();
434 }
435 
436 inline void
438 init ()
439 {
440  trace_with_mask("Streambuf::init", STRMBUFTRACE);
441 
444  m_buf_base = m_buf_end = 0;
445  m_flags = 0;
446 
447  m_shortbuf[0] = 0;
448 }
449 
450 inline
452 ~Streambuf ()
453 {
454  trace_with_mask("Streambuf::~Streambuf",STRMBUFTRACE);
455 
456  if (!(m_flags & USER_BUF)) {
457  delete [] m_buf_base;
458  m_buf_base = m_buf_end = 0;
459  }
460 }
461 
462 inline char*
464 base () const
465 {
466  trace_with_mask("Streambuf::base",STRMBUFTRACE);
467 
468  return m_read_base;
469 }
470 
471 inline char*
473 gptr () const
474 {
475  trace_with_mask("Streambuf::gptr",STRMBUFTRACE);
476 
477  return m_read_ptr;
478 }
479 
480 inline char*
482 egptr () const
483 {
484  trace_with_mask("Streambuf::egptr",STRMBUFTRACE);
485 
486  return m_read_end;
487 }
488 
489 inline char*
491 pbase () const
492 {
493  trace_with_mask("Streambuf::pbase",STRMBUFTRACE);
494  return m_write_base;
495 }
496 
497 inline char*
499 pptr () const
500 {
501  trace_with_mask("Streambuf::pptr",STRMBUFTRACE);
502  return m_write_ptr;
503 }
504 
505 inline char*
507 epptr () const
508 {
509  trace_with_mask("Streambuf::epptr",STRMBUFTRACE);
510  return m_write_end;
511 }
512 
513 inline void
515 pbump (int n_)
516 {
517  trace_with_mask("Streambuf::pbump",STRMBUFTRACE);
518  m_write_ptr += n_;
519 }
520 
521 inline int
523 sync ()
524 {
525  trace_with_mask("Streambuf::sync",STRMBUFTRACE);
526  return 0;
527 }
528 
529 inline int
531 showmanyc ()
532 {
533  trace_with_mask("Streambuf::showmanyc",STRMBUFTRACE);
534  return -1;
535 }
536 
537 inline int
539 sbumpc ()
540 {
541  trace_with_mask("Streambuf::sbumpc",STRMBUFTRACE);
542 
543  return (m_read_ptr >= m_read_end ? uflow ()
544  : *(unsigned char *) m_read_ptr++);
545 }
546 
547 inline int
549 sgetc ()
550 {
551  trace_with_mask("Streambuf::sgetc",STRMBUFTRACE);
552 
553  return (m_read_ptr >= m_read_end && underflow () == EOF
554  ? EOF : *(unsigned char*) m_read_ptr);
555 }
556 
557 inline int
559 sgetn (char* data_, int len_)
560 {
561  trace_with_mask("Streambuf::sgetn",STRMBUFTRACE);
562 
563  return xsgetn (data_, len_);
564 }
565 
566 inline int
568 sputc (char c_)
569 {
570  trace_with_mask("Streambuf::sputc",STRMBUFTRACE);
571 
572  return (m_write_ptr >= m_write_end
573  ? overflow (c_)
574  : (unsigned char) (*m_write_ptr++ = c_));
575 }
576 
577 inline int
579 sputn (char* b_, int len_)
580 {
581  trace_with_mask("Streambuf::sputn",STRMBUFTRACE);
582 
583  return xsputn (b_, len_);
584 }
585 
586 inline void
588 setp (char* pbeg_, char* pend_)
589 {
590  trace_with_mask("Streambuf::setp",STRMBUFTRACE);
591 
592  m_write_base = m_write_ptr = pbeg_;
593  m_write_end = pend_;
594 }
595 
596 inline int
598 underflow ()
599 {
600  trace_with_mask("Streambuf::underflow",STRMBUFTRACE);
601 
602  return (EOF);
603 }
604 
605 inline int
607 overflow (int /* c_ */)
608 {
609  trace_with_mask("Streambuf::overflow",STRMBUFTRACE);
610 
611  return (EOF);
612 }
613 
614 } // end namespace ASSA
615 
616 #endif /* STREAM_BUF_H */
A collection of assert function wrappers.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Streambuf class.
Definition: Streambuf.h:91
char * pptr() const
Returns a pointer to the beginning of the put area, and thus to the location of the next character th...
Definition: Streambuf.h:499
char * base() const
Returns the lowest possible value for gptr() - the beginning of the get area.
Definition: Streambuf.h:464
virtual int underflow()
This function is called to supply characters for input (from some source) when the get area is empty,...
Definition: Streambuf.h:598
void setp(char *pbeg_, char *pend_)
Set put area pointers.
Definition: Streambuf.h:588
virtual Streambuf * setbuf(char *p_, int len_)
Performs an operation that is defined separately for each class derived from Streambuf.
Definition: Streambuf.cpp:100
virtual int showmanyc()
The morphemes of showmanyc are "es-how-many-see", not "show-man-ic".
Definition: Streambuf.h:531
virtual int overflow(int c=EOF)
This function is called to consume characters (flush them to output), typically when the put area is ...
Definition: Streambuf.h:607
char * epptr() const
Returns a pointer just past the end of the put area, the maximum possible value for pptr().
Definition: Streambuf.h:507
int sputc(char c_)
This function stores c just after the put pointer, and advances the pointer one position,...
Definition: Streambuf.h:568
Streambuf & operator=(const Streambuf &)
int unbuffered()
Definition: Streambuf.h:408
char * egptr() const
Returns a pointer just past the end of the get area, the maximum possible value for gptr().
Definition: Streambuf.h:482
static const int MAXTCPFRAMESZ
Size of the internal input/output buffer.
Definition: Streambuf.h:106
Streambuf()
The default constructor is protected for class Streambuf to asssure that only objects for classes der...
Definition: Streambuf.h:429
virtual int doallocate()
This function is called by allocate when unbuffered() is zero and base() is zero.
Definition: Streambuf.cpp:234
virtual int xsgetn(char *b_, int len_)
Assigns up to len_ characters to successive elements of the array whose first element is designated b...
Definition: Streambuf.cpp:128
char * pbase() const
Returns a pointer to the beginning fo the space available for the put area, the lowest possible value...
Definition: Streambuf.h:491
virtual int uflow()
Reads the characters from the input sequence, if possible, and moves the stream position past it,...
Definition: Streambuf.cpp:172
int sbumpc()
This function should probably have been called `‘sgetc’'.
Definition: Streambuf.h:539
void setb(char *b_, char *eb_, int del_)
Establish the reserve area (buffer).
Definition: Streambuf.cpp:80
void pbump(int n_)
Advances the next pointer for the output sequence by n_.
Definition: Streambuf.h:515
virtual int sync()
This function synchronizes the streambuf with its actual stream of characters.
Definition: Streambuf.h:523
int in_avail()
This function returns the number of characters immediately available in the get area.
Definition: Streambuf.h:399
int sgetc()
This function returns the character after the get pointer, or EOF if the get pointer is at the end of...
Definition: Streambuf.h:549
Streambuf(const Streambuf &)
char * gptr() const
Returns a pointer to the beginning of the get area, and thus to the next character to be fetched (if ...
Definition: Streambuf.h:473
Streambuf * pubsetbuf(char *s_, int n_)
Set buffer.
Definition: Streambuf.h:381
int sputn(char *b_, int len_)
From the location pointed to by ptr, stores exactly len characters after the put pointer,...
Definition: Streambuf.h:579
void setg(char *gbeg_, char *gnext_, char *gend_)
Set get area pointers.
Definition: Streambuf.cpp:69
virtual int xsputn(const char *b_, int len_)
Writes up to len_ characters to the output sequence as if by repeated calls to sputc (c).
Definition: Streambuf.cpp:184
int sgetn(char *b_, int len_)
This function gets the next len_ characters following the get pointer, copying them to the char array...
Definition: Streambuf.h:559
virtual ~Streambuf()
Definition: Streambuf.h:452
int snextc()
This function moves the get pointer forward one position, then returns the character after the get ...
Definition: Streambuf.cpp:57
io_ptrs structure.
Definition: Streambuf.h:31
char * m_write_base
Definition: Streambuf.h:37
char m_shortbuf[1]
Definition: Streambuf.h:47
char * m_write_ptr
Definition: Streambuf.h:38
char * m_write_end
Definition: Streambuf.h:39
char * m_read_ptr
Definition: Streambuf.h:34
char * m_read_end
Definition: Streambuf.h:35
char * m_buf_base
Definition: Streambuf.h:41
char * m_read_base
Definition: Streambuf.h:33
char * m_buf_end
Definition: Streambuf.h:42
void dump() const
Definition: Streambuf.cpp:23
Definition: Acceptor.h:40
@ STRMBUFTRACE
Extended Streambuf & friends messages
Definition: LogMask.h:46