NCBI C++ ToolKit
strbuffer.inl
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #if defined(STRBUFFER__HPP) && !defined(STRBUFFER__INL)
2 #define STRBUFFER__INL
3 
4 /* $Id: strbuffer.inl 74336 2016-08-25 14:24:12Z gouriano $
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 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 * !!! PUT YOUR DESCRIPTION HERE !!!
33 */
34 
35 inline
36 bool CIStreamBuffer::fail(void) const
37 {
38  return m_Error != 0;
39 }
40 
41 inline
43 {
44  m_Error = 0;
45 }
46 
47 inline
48 const char* CIStreamBuffer::GetError(void) const
49 {
50  return m_Error;
51 }
52 
53 inline
55  THROWS1((CIOException, bad_alloc))
56 {
57  const char* pos = m_CurrentPos + offset;
58  if ( pos >= m_DataEndPos )
59  pos = FillBuffer(pos);
60  return *pos;
61 }
62 
63 inline
65 {
66  const char* pos = m_CurrentPos + offset;
67  if ( pos >= m_DataEndPos )
68  return FillBufferNoEOF(pos);
69  return *pos;
70 }
71 
72 inline
73 bool CIStreamBuffer::SkipExpectedChar(char c, size_t offset)
74 {
75  const char* pos = m_CurrentPos+offset;
76  if ( pos >= m_DataEndPos )
77  pos = FillBuffer(pos);
78  if ( *pos != c )
79  return false;
80  m_CurrentPos = pos+1;
81  return true;
82 }
83 
84 inline
85 bool CIStreamBuffer::SkipExpectedChars(char c1, char c2, size_t offset)
86 {
87  return SkipExpectedChar(c1, offset) && SkipExpectedChar(c2, 0);
88 }
89 
90 inline
91 bool CIStreamBuffer::HasMore(void)
92 {
94 }
95 
96 inline
97 char CIStreamBuffer::GetChar(void)
98  THROWS1((CIOException, bad_alloc))
99 {
100  const char* pos = m_CurrentPos;
101  if ( pos >= m_DataEndPos )
102  pos = FillBuffer(pos);
103  m_CurrentPos = pos + 1;
104  return *pos;
105 }
106 
107 inline
109 {
110  const char* pos = m_CurrentPos;
111  _ASSERT(pos > m_Buffer);
112  _ASSERT(pos[-1] == c);
113  m_CurrentPos = pos - 1;
114 }
115 
116 inline
117 void CIStreamBuffer::SkipChars(size_t count)
118 {
119  _ASSERT(m_CurrentPos + count > m_CurrentPos);
120  _ASSERT(m_CurrentPos + count <= m_DataEndPos);
121  m_CurrentPos += count;
122 }
123 
124 inline
125 void CIStreamBuffer::SkipChar(void)
126 {
127  SkipChars(1);
128 }
129 
130 inline
131 void CIStreamBuffer::GetChars(size_t count)
133 {
134  // cache pos
135  const char* pos = m_CurrentPos;
136  for ( ;; ) {
137  size_t c = m_DataEndPos - pos;
138  if ( c >= count ) {
139  // all data is already in buffer -> skip it
140  m_CurrentPos = pos + count;
141  return;
142  }
143  else {
144  count -= c;
145  m_CurrentPos = pos += c;
146  pos = FillBuffer(pos);
147  }
148  }
149 }
150 
151 inline
152 const char* CIStreamBuffer::GetCurrentPos(void) const
154 {
155  return m_CurrentPos;
156 }
157 
158 inline
159 size_t CIStreamBuffer::GetLine(void) const
161 {
162  return m_Line;
163 }
164 
165 inline
168 {
169  return GetStreamPos();
170 }
171 
172 inline
175 {
176  return NcbiInt8ToStreampos( GetStreamPosAsInt8() );
177 }
178 
179 inline
181 {
182  return m_BufferPos + (m_CurrentPos - m_Buffer);
183 }
184 
185 inline
186 bool COStreamBuffer::fail(void) const
187 {
188  return m_Error != 0;
189 }
190 
191 inline
192 void COStreamBuffer::ResetFail(void)
193 {
194  m_Error = 0;
195 }
196 
197 inline
198 const char* COStreamBuffer::GetError(void) const
199 {
200  return m_Error;
201 }
202 
203 inline
204 size_t COStreamBuffer::GetLine(void) const
206 {
207  return m_Line;
208 }
209 
210 inline
213 {
214  return GetStreamPos();
215 }
216 
217 inline
220 {
221  return NcbiInt8ToStreampos( GetStreamPosAsInt8() );
222 }
223 
224 inline
226 {
227  return m_BufferPos + (m_CurrentPos - m_Buffer);
228 }
229 
230 inline
231 size_t COStreamBuffer::GetCurrentLineLength(void) const
233 {
234  return m_LineLength;
235 }
236 
237 inline
238 bool COStreamBuffer::ZeroIndentLevel(void) const
240 {
241  return m_IndentLevel == 0;
242 }
243 
244 inline
245 size_t COStreamBuffer::GetIndentLevel(size_t step) const
247 {
248  return m_IndentLevel / step;
249 }
250 
251 inline
252 void COStreamBuffer::IncIndentLevel(size_t step)
254 {
255  m_IndentLevel += step;
256 }
257 
258 inline
259 void COStreamBuffer::DecIndentLevel(size_t step)
261 {
262  _ASSERT(m_IndentLevel >= step);
263  m_IndentLevel -= step;
264 }
265 
266 inline
267 void COStreamBuffer::SetBackLimit(size_t limit)
268 {
269  m_BackLimit = limit;
270 }
271 
272 inline
273 char* COStreamBuffer::DoSkip(size_t reserve)
274  THROWS1((CIOException, bad_alloc))
275 {
276  char* pos = DoReserve(reserve);
277  m_CurrentPos = pos + reserve;
278  m_LineLength += reserve;
279  return pos;
280 }
281 
282 inline
283 char* COStreamBuffer::Skip(size_t count)
284  THROWS1((CIOException, bad_alloc))
285 {
286  char* pos = m_CurrentPos;
287  char* end = pos + count;
288  if ( end <= m_BufferEnd ) {
289  // enough space in buffer
290  m_CurrentPos = end;
291  m_LineLength += count;
292  return pos;
293  }
294  else {
295  return DoSkip(count);
296  }
297 }
298 
299 inline
300 char* COStreamBuffer::Reserve(size_t count)
301  THROWS1((CIOException, bad_alloc))
302 {
303  char* pos = m_CurrentPos;
304  char* end = pos + count;
305  if ( end <= m_BufferEnd ) {
306  // enough space in buffer
307  return pos;
308  }
309  else {
310  return DoReserve(count);
311  }
312 }
313 
314 inline
315 void COStreamBuffer::PutChar(char c)
317 {
318  *Skip(1) = c;
319 }
320 
321 inline
323 {
325  --m_CurrentPos;
326  _ASSERT(*m_CurrentPos == c);
327 }
328 
329 inline
330 void COStreamBuffer::PutString(const char* str, size_t length)
331  THROWS1((CIOException, bad_alloc))
332 {
333  if ( length < 1024 ) {
334  memcpy(Skip(length), str, length);
335  }
336  else {
337  Write(str, length);
338  }
339 }
340 
341 inline
342 void COStreamBuffer::PutString(const char* str)
343  THROWS1((CIOException, bad_alloc))
344 {
345  PutString(str, strlen(str));
346 }
347 
348 inline
349 void COStreamBuffer::PutString(const string& str)
350  THROWS1((CIOException, bad_alloc))
351 {
352  PutString(str.data(), str.size());
353 }
354 
355 inline
356 void COStreamBuffer::PutIndent(void)
357  THROWS1((CIOException, bad_alloc))
358 {
359  if (GetUseIndentation()) {
360  size_t count = m_IndentLevel;
361  memset(Skip(count), ' ', count);
362  }
363 }
364 
365 inline
367  THROWS1((CIOException, bad_alloc))
368 {
369  if (!GetUseEol()) {
370  return;
371  }
372  char* pos = Reserve(1);
373  *pos = '\n';
374  m_CurrentPos = pos + 1;
375  ++m_Line;
376  m_LineLength = 0;
377  if ( indent )
378  PutIndent();
379 }
380 
381 inline
382 void COStreamBuffer::WrapAt(size_t lineLength, bool keepWord)
383  THROWS1((CIOException, bad_alloc))
384 {
385  if ( keepWord ) {
386  if ( GetCurrentLineLength() > lineLength )
387  PutEolAtWordEnd(lineLength);
388  }
389  else {
390  if ( GetCurrentLineLength() >= lineLength )
391  PutEol(false);
392  }
393 }
394 
395 inline
396 size_t COStreamBuffer::GetUsedSpace(void) const
397 {
398  return static_cast<size_t>(m_CurrentPos - m_Buffer);
399 }
400 
401 inline
402 size_t COStreamBuffer::GetAvailableSpace(void) const
403 {
404  return static_cast<size_t>(m_BufferEnd - m_CurrentPos);
405 }
406 
407 inline
408 size_t COStreamBuffer::GetBufferSize(void) const
409 {
410  return static_cast<size_t>(m_BufferEnd - m_Buffer);
411 }
412 
413 inline
415 {
417 }
418 
419 inline
420 bool COStreamBuffer::GetUseIndentation(void) const
421 {
422  return m_UseIndentation;
423 }
424 inline
426 {
427  m_UseEol = set;
428 }
429 
430 inline
431 bool COStreamBuffer::GetUseEol(void) const
432 {
433  return m_UseEol;
434 }
435 
436 
437 #endif /* def STRBUFFER__HPP && ndef STRBUFFER__INL */
Definition: set.hpp:45
constexpr auto end(const ct_const_array< T, N > &in) noexcept
static const char * str(char *buf, int n)
Definition: stats.c:84
int offset
Definition: replacements.h:160
#define _DEBUG_ARG(arg)
Definition: ncbidbg.hpp:134
void Write(CObjectOStream &out, TConstObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:55
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
void IncIndentLevel(size_t step=2)
char PeekChar(size_t offset=0)
void PutIndent(void)
bool m_UseIndentation
Definition: strbuffer.hpp:335
void SetBackLimit(size_t limit)
bool GetUseIndentation(void) const
size_t GetIndentLevel(size_t step=2) const
Int8 GetStreamPosAsInt8(void) const
void PutChar(char c)
Int8 GetStreamPosAsInt8(void) const
void BackChar(char c)
char * DoSkip(size_t reserve)
void GetChars(char *buffer, size_t count)
Definition: strbuffer.cpp:471
size_t m_BackLimit
Definition: strbuffer.hpp:334
bool TryToFillBuffer(void)
Definition: strbuffer.cpp:465
size_t GetUsedSpace(void) const
bool fail(void) const
const char * m_CurrentPos
Definition: strbuffer.hpp:204
const char * m_Error
Definition: strbuffer.hpp:320
void UngetChar(char c)
void SetUseIndentation(bool set)
bool SkipExpectedChar(char c, size_t offset=0)
char * m_BufferEnd
Definition: strbuffer.hpp:331
size_t GetLine(void) const
#define THROWS1(arg)
Definition: strbuffer.hpp:56
CNcbiStreampos GetStreamPos(void) const
char * m_CurrentPos
Definition: strbuffer.hpp:330
size_t GetCurrentLineLength(void) const
void PutString(const char *str, size_t length)
CNcbiStreampos GetStreamOffset(void) const
bool GetUseEol(void) const
void SetUseEol(bool set)
CNcbiStreampos GetStreamPos(void) const
void SkipChar(void)
char GetChar(void)
size_t GetLine(void) const
void PutEol(bool indent=true)
void SkipChars(size_t count)
char FillBufferNoEOF(const char *pos)
Definition: strbuffer.cpp:455
void WrapAt(size_t lineLength, bool keepWord)
const char * FillBuffer(const char *pos, bool noEOF=false)
Definition: strbuffer.cpp:319
const char * GetError(void) const
CNcbiStreampos GetStreamOffset(void) const
const char * GetError(void) const
bool fail(void) const
const char * GetCurrentPos(void) const
const char * m_Error
Definition: strbuffer.hpp:199
char * Skip(size_t count)
void ResetFail(void)
void DecIndentLevel(size_t step=2)
size_t GetBufferSize(void) const
bool ZeroIndentLevel(void) const
const char * m_DataEndPos
Definition: strbuffer.hpp:205
void ResetFail(void)
char PeekCharNoEOF(size_t offset=0)
char * Reserve(size_t count)
bool SkipExpectedChars(char c1, char c2, size_t offset=0)
#define THROWS1_NONE
Definition: strbuffer.hpp:57
size_t GetAvailableSpace(void) const
bool HasMore(void)
NCBI_NS_STD::char_traits< char >::pos_type NcbiInt8ToStreampos(Int8 pos)
Convert plain numeric stream position (offset) into stream position usable with STL stream library.
Definition: ncbistre.hpp:782
IO_PREFIX::streampos CNcbiStreampos
Portable alias for streampos.
Definition: ncbistre.hpp:134
string indent(" ")
#define _ASSERT
Modified on Sun Jun 23 05:22:22 2024 by modify_doxy.py rev. 669887