Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
StrBuilder.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_StrBuilder_H
7 #define _INC_StrBuilder_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "StrT.h"
13 #include "cMem.h"
14 
15 namespace Gray
16 {
17  class StrBuilder : public cMemBlock // GRAYCORE_LINK
18  {
22  public:
23  char* m_pCur;
25 
26  protected:
27  void Advance(StrLen_t nLen)
28  {
29  if (((UINT)nLen) >= (UINT)m_nLenLeft)
30  {
31  m_pCur = ((char*)get_DataEnd()) - 1;
32  m_nLenLeft = 0;
33  }
34  else
35  {
36  m_pCur += nLen;
37  m_nLenLeft -= nLen;
38  }
39  m_pCur[0] = '\0'; // always force terminate.
40  }
41 
42  public:
43  StrBuilder(void* p, StrLen_t nSize)
44  : cMemBlock(p, nSize)
45  , m_pCur((char*)p)
46  , m_nLenLeft(nSize)
47  {
48  // nSize = sizeof(*p);
49  ASSERT(m_nLenLeft > 0);
50  ASSERT(m_pCur != nullptr);
51  *m_pCur = '\0';
52  }
54  : cMemBlock(m)
55  , m_pCur(m.get_DataA())
57  {
58  // nSize = sizeof(*p);
59  ASSERT(m_nLenLeft > 0);
60  ASSERT(m_pCur != nullptr);
61  *m_pCur = '\0';
62  }
63 
64  void ResetStr()
65  {
66  m_pCur = get_DataA();
68  *m_pCur = '\0';
69  }
71  {
73  return StrT::Diff(m_pCur, get_DataA());
74  }
75  const char* get_Str() const noexcept
76  {
77  // get_StrA();
78  return get_DataA();
79  }
81  {
82  return m_nLenLeft;
83  }
84  bool isOverflow() const
85  {
87  return m_nLenLeft <= 0;
88  }
89  StrLen_t _cdecl AddFormat(const char* pszFormat, ...)
90  {
91  // m_nLenLeft includes space for terminator '\0'
92  va_list vargs;
93  va_start(vargs, pszFormat);
94  StrLen_t nLenRet = StrT::vsprintfN(m_pCur, m_nLenLeft, pszFormat, vargs);
95  va_end(vargs);
96  Advance(nLenRet);
97  return nLenRet;
98  }
99  StrLen_t AddStr(const char* pszStr)
100  {
101  // m_nLenLeft includes space for terminator '\0'
102  if (pszStr == nullptr) // just add nothing.
103  return 0;
104  StrLen_t nLenRet = StrT::CopyLen(m_pCur, pszStr, m_nLenLeft);
105  Advance(nLenRet);
106  return nLenRet;
107  }
108  void AddCRLF()
109  {
110  // AKA CRNL
111  AddStr(STR_CRLF);
112  }
113  void AddChar(char ch)
114  {
115  // m_nLenLeft includes space for terminator '\0'
116  if (m_nLenLeft < 1)
117  return;
118  *m_pCur = ch;
119  Advance(1);
120  }
121  void AddCharRepeat(char ch, int iRepeat)
122  {
123  // m_nLenLeft includes space for terminator '\0'
124  for (int i = 0; i < iRepeat; i++)
125  {
126  AddChar(ch);
127  }
128  }
129  void AddBytesRaw(const void* p, size_t nSize)
130  {
131  StrLen_t nLenRet = MIN(m_nLenLeft, (StrLen_t)nSize);
132  ::memcpy(m_pCur, p, nSize);
133  Advance(nLenRet);
134  }
135  void AddBytes(const void* p, size_t nSize)
136  {
137  // Just add a string from void*. Don't assume terminated string. filter for printable characters.
138  StrLen_t nLenRet = MIN(m_nLenLeft, (StrLen_t)nSize);
139  for (StrLen_t i = 0; i < nLenRet; i++)
140  {
141  BYTE ch = ((BYTE*)p)[i];
142  if (ch < 32 || ch == 127 || (ch > 128 && ch < 160)) // strip junk chars.
143  m_pCur[i] = '?';
144  else
145  m_pCur[i] = ch;
146  }
147  Advance(nLenRet);
148  }
149  };
150 
151  class StrBuilderAlloc : public StrBuilder // GRAYCORE_LINK
152  {
155  public:
156  StrBuilderAlloc(size_t nSizeStart = 1024)
157  : StrBuilder(nullptr, 0)
158  {
159  UNREFERENCED_PARAMETER(nSizeStart);
160  }
161  };
162 }
163 
164 #endif
165 
166 
#define STR_CRLF
CR+LF for DOS/Windows format text files. (13,10)
Definition: StrT.h:79
#define MIN(a, b)
Definition: SysTypes.h:457
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: StrBuilder.h:152
StrBuilderAlloc(size_t nSizeStart=1024)
Definition: StrBuilder.h:156
Definition: StrBuilder.h:18
StrLen_t m_nLenLeft
Definition: StrBuilder.h:24
StrLen_t AddStr(const char *pszStr)
Definition: StrBuilder.h:99
StrLen_t get_LenLeft() const
Definition: StrBuilder.h:80
bool isOverflow() const
Definition: StrBuilder.h:84
StrBuilder(cMemBlock &m)
Definition: StrBuilder.h:53
void AddCRLF()
Definition: StrBuilder.h:108
void AddBytes(const void *p, size_t nSize)
Definition: StrBuilder.h:135
const char * get_Str() const noexcept
Definition: StrBuilder.h:75
StrBuilder(void *p, StrLen_t nSize)
Definition: StrBuilder.h:43
void AddChar(char ch)
Definition: StrBuilder.h:113
StrLen_t get_Length() const
Definition: StrBuilder.h:70
void AddBytesRaw(const void *p, size_t nSize)
Definition: StrBuilder.h:129
StrLen_t _cdecl AddFormat(const char *pszFormat,...)
Definition: StrBuilder.h:89
void Advance(StrLen_t nLen)
Definition: StrBuilder.h:27
char * m_pCur
Definition: StrBuilder.h:23
void AddCharRepeat(char ch, int iRepeat)
Definition: StrBuilder.h:121
void ResetStr()
Definition: StrBuilder.h:64
Definition: cMem.h:311
char * get_DataA() const noexcept
Definition: cMem.h:359
size_t get_DataSize() const noexcept
Definition: cMem.h:344
const void * get_DataEnd() const noexcept
Definition: cMem.h:433
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
static StrLen_t vsprintfN(OUT TYPE *pszOut, StrLen_t iLenOutMax, const TYPE *pszFormat, va_list vlist)
static StrLen_t Diff(const TYPE *pszEnd, const TYPE *pszStart)
Definition: StrT.h:138
static __DECL_IMPORT StrLen_t __stdcall CopyLen(TYPE *pszDst, const TYPE *pSrc, StrLen_t iLenCharsMax) noexcept