HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StringStream.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_StringStream.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_StringStream__
12 #define __UT_StringStream__
13 
14 #include "UT_API.h"
15 #include "UT_WorkBuffer.h"
16 
17 #include <iostream>
18 
19 /// An a string buffer based on UT_WorkBuffer. Supports both input and output.
20 /// Provides cleaner semantics than UT_OStrStream, which is based on the
21 /// obsolete std::strstream class. Unlike UT_OStrStream, it is not necessary
22 /// to manage the stream's memory externally, if it was dynamically allocated,
23 /// or add a null byte termination.
24 /// For example, this code:
25 /// @code{.cpp}
26 /// UT_OStrStream os;
27 /// SomeClass::save(os); // write things to os.
28 /// os << std::ends;
29 /// fprintf(stderr, "Buffer: %s", os.str());
30 /// os.rdbuf()->freeze(false);
31 /// @endcode
32 /// Turns into:
33 //// @code{.cpp}
34 /// UT_OStringStream os;
35 /// SomeClass::save(os); // write things to os.
36 /// fprintf(stderr, "Buffer: %s", os.str());
37 /// @endcode
38 /// Note that the explicit requirement for null termination, and the call
39 /// to @c freeze to relinquish the buffer use, are both gone.
40 ///
41 /// However, the downside is that you cannot pass explicit buffers to the
42 /// stream. On the other hand, you should not be using fixed-sized buffers for
43 /// output streams in the first place.
44 ///
45 /// There's also support for input and input/output buffers, using
46 /// UT_IStringStream and UT_StringStream.
47 
48 /// Class for the memory storage and stream buffering.
49 ///
50 /// std::streambuf provides buffered reading and writing to an underlying
51 /// stream. A UT_WorkBuffer is used for the underlying storage. Like a file,
52 /// seeking past the end of the existing data is allowed.
53 ///
54 /// The get buffer (reading) is defined by the eback(), gptr(), and egptr()
55 /// pointers. These pointers are all nullptr iff the stream does not allow
56 /// reading. myGPos >= 0 when seeked past the end of the existing data. In
57 /// this situation, eback() == egptr() to prevent methods from pulling values
58 /// from the get buffer.
59 ///
60 /// The put buffer (writing) is defined by the pbase(), pptr(), and epptr()
61 /// pointers. These pointers are all nullptr iff the stream does not allow
62 /// writing. myPPos >= 0 when seeked past the end of the existing data. In
63 /// this situation, pbase() == epptr() to prevent methods from putting values
64 /// into the put buffer.
65 class UT_StringStreamBuffer : public std::streambuf
66 {
67 public:
69  std::ios_base::openmode mode = (std::ios_base::in | std::ios_base::out),
70  const char *buf=nullptr,
71  exint len=-1);
72 
73  const char *buffer() const { return myBuffer.buffer(); }
74  exint length() const { return myBuffer.length(); }
75 
76  int64 getMemoryUsage(bool inclusive) const
77  {
78  int64 mem = myBuffer.getMemoryUsage(false);
79  if(inclusive)
80  mem += sizeof(*this);
81  return mem;
82  }
83 
84  // Resets the buffer to an empty state. Does not de-allocate existing
85  // memory.
86  void reset();
87 
88  // Returns a reference to the current string.
89  const UT_WorkBuffer &str();
90  void swap(UT_WorkBuffer &buf);
93 
94 private:
95  // iostream implementation helpers
96  std::streambuf::pos_type seekoff(
97  std::streambuf::off_type offset,
98  std::ios_base::seekdir dir,
99  std::ios_base::openmode mode) override;
100 
101  std::streambuf::pos_type seekpos(
102  std::streambuf::pos_type pos,
103  std::ios_base::openmode mode) override;
104 
105  // istream implementation helpers
106  std::streambuf::int_type underflow() override;
107 
108  std::streamsize xsgetn(
109  std::streambuf::char_type *dst,
110  std::streamsize num) override;
111 
112  std::streamsize showmanyc() override;
113 
114  std::streambuf::int_type pbackfail(std::streambuf::int_type c) override;
115 
116  // ostream implementation helpers
117  std::streambuf::int_type overflow(int_type c) override;
118 
119  std::streamsize xsputn(
120  const std::streambuf::char_type *src,
121  std::streamsize num) override;
122 
123  // get/put buffer management
124  void init();
125  void setg64(char *start, char *cur, char *end);
126  void setp64(char *start, char *cur, char *end);
127  void updategpos(exint pos);
128  void updateppos(exint pos);
129 
130  void reserve(exint bytes);
131 
132  // Updates the underlying buffer with proper termination.
133  void updateBufferEnd();
134 
135  UT_WorkBuffer myBuffer;
136  exint myGPos;
137  exint myPPos;
138 };
139 
140 /// An input stream object that owns its own string buffer storage.
141 class UT_IStringStream : public std::istream
142 {
143 public:
144  /// Populate the input stream with an existing raw character buffer.
145  /// The contents of the buffer are copied into the internal storage.
146  /// The input position start at the beginning of the buffer.
147  UT_IStringStream(const char *buf=nullptr, exint len=-1)
148  : myBuffer(std::ios_base::in, buf, len)
149  , std::istream(&myBuffer) {}
150 
151  /// Returns the underlying stream buffer object.
152  UT_StringStreamBuffer *rdbuf() const { return SYSconst_cast(&myBuffer); }
153 
154 private:
155  UT_StringStreamBuffer myBuffer;
156 };
157 
158 /// An output stream object that owns its own string buffer storage.
159 class UT_OStringStream : public std::ostream
160 {
161 public:
162  /// Start with an empty string buffer with a given size reservation, if it
163  /// is known in advance how much data will be written out.
164  UT_OStringStream(exint reserved_size=-1)
165  : myBuffer(std::ios_base::out, nullptr, reserved_size)
166  , std::ostream(&myBuffer) {}
167 
168  int64 getMemoryUsage(bool inclusive) const
169  {
170  int64 mem = myBuffer.getMemoryUsage(false);
171  if(inclusive)
172  mem += sizeof(*this);
173  return mem;
174  }
175 
176  /// Returns the underlying stream buffer object.
177  UT_StringStreamBuffer *rdbuf() const { return SYSconst_cast(&myBuffer); }
178 
179  /// Returns a read-only reference to the underlying UT_WorkBuffer.
180  const UT_WorkBuffer &str() { return myBuffer.str(); }
181 
182  /// Resets the input stream and clears all existing input data.
183  void reset() { myBuffer.reset(); }
184 
185 private:
186  UT_StringStreamBuffer myBuffer;
187 };
188 
189 /// An bi-directional stream object that owns its own string buffer storage.
190 class UT_StringStream : public std::iostream
191 {
192 public:
193  /// Start with an empty string buffer with a given size reservation, if it
194  /// is known in advance how much data will be written out. The input stream
195  /// will still be empty.
196  UT_StringStream(exint reserved_size=-1)
197  : myBuffer(std::ios_base::in | std::ios_base::out, nullptr, reserved_size)
198  , std::iostream(&myBuffer) {}
199 
200  /// Returns the underlying stream buffer object.
201  UT_StringStreamBuffer *rdbuf() const { return SYSconst_cast(&myBuffer); }
202 
203  /// Returns a read-only reference to the underlying UT_WorkBuffer.
204  const UT_WorkBuffer &str() { return myBuffer.str(); }
205 
206  /// Resets the input stream and clears all existing input data.
207  void reset() { myBuffer.reset(); }
208 
209 private:
210  UT_StringStreamBuffer myBuffer;
211 };
212 
213 // Implementation file
214 #include "UT_StringStreamImpl.h"
215 
216 #endif //__UT_StringStream__
void stealIntoStringHolder(UT_StringHolder &s)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
const char * buffer() const
SYS_FORCE_INLINE exint length() const
UT_StringStreamBuffer * rdbuf() const
Returns the underlying stream buffer object.
void reset()
Resets the input stream and clears all existing input data.
int64 getMemoryUsage(bool inclusive) const
int64 getMemoryUsage(bool inclusive) const
GLuint start
Definition: glcorearb.h:475
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
int64 exint
Definition: SYS_Types.h:125
SYS_FORCE_INLINE const char * buffer() const
GLdouble s
Definition: glad.h:3009
An output stream object that owns its own string buffer storage.
An input stream object that owns its own string buffer storage.
UT_OStringStream(exint reserved_size=-1)
const UT_WorkBuffer & str()
Returns a read-only reference to the underlying UT_WorkBuffer.
exint length() const
UT_IStringStream(const char *buf=nullptr, exint len=-1)
void reset()
Resets the input stream and clears all existing input data.
GLintptr offset
Definition: glcorearb.h:665
UT_StringStreamBuffer(std::ios_base::openmode mode=(std::ios_base::in|std::ios_base::out), const char *buf=nullptr, exint len=-1)
GLuint GLuint end
Definition: glcorearb.h:475
An bi-directional stream object that owns its own string buffer storage.
long long int64
Definition: SYS_Types.h:116
int64 getMemoryUsage(bool inclusive) const
void swap(UT_WorkBuffer &buf)
GLenum mode
Definition: glcorearb.h:99
GLenum GLenum dst
Definition: glcorearb.h:1793
const UT_WorkBuffer & str()
UT_StringStreamBuffer * rdbuf() const
Returns the underlying stream buffer object.
const UT_WorkBuffer & str()
Returns a read-only reference to the underlying UT_WorkBuffer.
UT_StringStream(exint reserved_size=-1)
UT_StringStreamBuffer * rdbuf() const
Returns the underlying stream buffer object.
void stealIntoString(UT_String &s)
Definition: format.h:2459
GLenum src
Definition: glcorearb.h:1793