NCBI C++ ToolKit
rwstreambuf.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___RWSTREAMBUF__HPP
2 #define CORELIB___RWSTREAMBUF__HPP
3 
4 /* $Id: rwstreambuf.hpp 89418 2020-03-27 22:47:07Z lavr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Anton Lavrentiev
30  *
31  * File Description:
32  * Reader-writer-based stream buffer
33  *
34  */
35 
36 /// @file rwstreambuf.hpp
37 /// Reader-writer-based stream buffer.
38 /// @sa IReader, IWriter, IReaderWriter, CRStream, CWStream, CRWStream
39 
40 #include <corelib/ncbistre.hpp>
42 
43 #ifdef NCBI_COMPILER_MIPSPRO
44 # define CRWStreambufBase CMIPSPRO_ReadsomeTolerantStreambuf
45 #else
46 # define CRWStreambufBase CNcbiStreambuf
47 # ifdef NCBI_COMPILER_MSVC
48 # pragma warning(push)
49 # pragma warning(disable:4996)
50 # endif //NCBI_COMPILER_MSVC
51 #endif //NCBI_COMPILER_MIPSPRO
52 
53 
55 
56 
57 /// Reader-writer-based stream buffer.
58 
60 {
61 public:
62  /// Which of the objects (passed in the constructor) must be deleted on
63  /// this object's destruction, whether to tie I/O, and how to process
64  /// exceptions thrown at lower levels...
65  enum EFlags {
66  fOwnReader = 1 << 0, ///< Own the underlying reader
67  fOwnWriter = 1 << 1, ///< Own the underlying writer
68  fOwnAll = fOwnReader + fOwnWriter,
69  fUntie = 1 << 2, ///< Do not flush before reading
70  fNoStatusLog = 1 << 3, ///< Do not log unsuccessful I/O
71  fLogExceptions = 1 << 4, ///< Exceptions logged only
72  fLeakExceptions = 1 << 5 ///< Exceptions leaked out
73  };
74  typedef int TFlags; ///< Bitwise OR of EFlags
75 
76 
78  streamsize buf_size = 0,
79  CT_CHAR_TYPE* buf = 0,
80  TFlags flags = 0);
81 
82  /// NOTE: if both reader and writer have actually happened to be the same
83  /// object, then when owned, it will _not_ be deleted twice.
85  IWriter* w,
86  streamsize buf_size = 0,
87  CT_CHAR_TYPE* buf = 0,
88  TFlags flags = 0);
89 
90  virtual ~CRWStreambuf();
91 
92 protected:
93  virtual CT_INT_TYPE overflow(CT_INT_TYPE c);
94  virtual streamsize xsputn(const CT_CHAR_TYPE* buf, streamsize n);
95 
96  virtual CT_INT_TYPE underflow(void);
97  virtual streamsize xsgetn(CT_CHAR_TYPE* s, streamsize n);
98  virtual streamsize showmanyc(void);
99 
100  virtual int sync(void);
101 
102  /// Per the standard, setbuf(0, 0) makes I/O unbuffered. Other behavior is
103  /// implementation-dependent:
104  /// "buf_size" == 1 makes I/O unbuffered ("buf", if provided, may still be
105  /// used internally as a one-char un-get location).
106  /// Special case: setbuf(non-NULL, 0) creates an internal buffer of some
107  /// predefined size, which will be automatically deallocated in dtor; the
108  /// value of the first argument is ignored (can be any non-NULL pointer).
109  /// Otherwise, setbuf() sets I/O arena of size "buf_size" located at "buf",
110  /// and halved between the I/O directions, if both are used. If "buf" is
111  /// provided as NULL, the buffer of the requested size gets allocated
112  /// internally, and gets automatically freed upon CRWStreambuf destruction.
113  /// Before replacing the buffer, this call first attempts to flush any
114  /// pending output (sync) to the output device, and return any pending
115  /// input sequence (internal read buffer) to the input device (pushback).
116  virtual CNcbiStreambuf* setbuf(CT_CHAR_TYPE* buf, streamsize buf_size);
117 
118  /// Only seekoff(0, IOS_BASE::cur, *) to obtain current position, and input
119  /// skip-forward are permitted:
120  /// seekoff(off, IOS_BASE::cur or IOS_BASE::beg, IOS_BASE::in) when the
121  /// requested stream position is past the current input position (so the
122  /// stream can read forward internally to reach that position).
123  virtual CT_POS_TYPE seekoff(CT_OFF_TYPE off, IOS_BASE::seekdir whence,
124  IOS_BASE::openmode which =
126 
127 #ifdef NCBI_OS_MSWIN
128  // See comments in "connect/ncbi_conn_streambuf.hpp"
129  virtual streamsize _Xsgetn_s(CT_CHAR_TYPE* buf, size_t, streamsize n)
130  { return xsgetn(buf, n); }
131 #endif /*NCBI_OS_MSWIN*/
132 
133 protected:
135  { return x_GPos - (CT_OFF_TYPE)(gptr() ? egptr() - gptr() : 0); }
136 
138  { return x_PPos + (CT_OFF_TYPE)(pbase() ? pbase() - pptr() : 0); }
139 
140  int x_Sync(void)
141  { return pbase() < pptr() ? sync() : 0; }
142 
143  streamsize x_Read(CT_CHAR_TYPE* s, streamsize n);
144 
145  ERW_Result x_Pushback(void);
146 
147 protected:
149 
152 
153  size_t m_BufSize;
156 
159 
160  CT_POS_TYPE x_GPos; ///< get position [for istream::tellg()]
161  CT_POS_TYPE x_PPos; ///< put position [for ostream::tellp()]
162 
163  bool x_Eof; ///< whether at EOF
164  bool x_Err; ///< whether there was a _write_ error
165  CT_POS_TYPE x_ErrPos; ///< position of the _write_ error (if x_Err)
166 
167 private:
170 };
171 
172 
174 
175 
176 #ifdef NCBI_COMPILER_MSVC
177 # pragma warning(pop)
178 #endif //NCBI_COMPILER_MSVC
179 
180 #endif /* CORELIB___RWSTREAMBUF__HPP */
Reader-writer-based stream buffer.
Definition: rwstreambuf.hpp:60
size_t m_BufSize
CT_CHAR_TYPE * m_WriteBuf
CT_CHAR_TYPE x_Buf
CT_POS_TYPE x_ErrPos
position of the _write_ error (if x_Err)
virtual streamsize _Xsgetn_s(CT_CHAR_TYPE *buf, size_t, streamsize n)
EFlags
Which of the objects (passed in the constructor) must be deleted on this object's destruction,...
Definition: rwstreambuf.hpp:65
int TFlags
Bitwise OR of EFlags.
Definition: rwstreambuf.hpp:74
CT_POS_TYPE x_GPos
get position [for istream::tellg()]
CRWStreambuf operator=(const CRWStreambuf &)
int x_Sync(void)
bool x_Err
whether there was a _write_ error
CT_POS_TYPE x_GetGPos(void)
CT_CHAR_TYPE * m_ReadBuf
CRWStreambuf(const CRWStreambuf &)
bool x_Eof
whether at EOF
CT_POS_TYPE x_GetPPos(void)
AutoPtr< IReader > m_Reader
AutoPtr< IWriter > m_Writer
CT_CHAR_TYPE * m_pBuf
CT_POS_TYPE x_PPos
put position [for ostream::tellp()]
A very basic data-read/write interface.
A very basic data-read interface.
A very basic data-write interface.
Abstract reader-writer interface classes.
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ERW_Result
Result codes for I/O operations.
#define CT_OFF_TYPE
Definition: ncbistre.hpp:731
#define CT_INT_TYPE
Definition: ncbistre.hpp:728
#define CT_POS_TYPE
Definition: ncbistre.hpp:730
#define CT_CHAR_TYPE
Definition: ncbistre.hpp:729
IO_PREFIX::streambuf CNcbiStreambuf
Portable alias for streambuf.
Definition: ncbistre.hpp:143
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
char * buf
yy_size_t n
static int x_Pushback(SOCK sock, BUF buf)
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
std::istream & in(std::istream &in_, double &x_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
#define CRWStreambufBase
Definition: rwstreambuf.hpp:46
Modified on Sun Apr 14 05:25:56 2024 by modify_doxy.py rev. 669887