OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdBuf.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // Standard buffer classes
17 
18 #ifndef STDBUF_H
19 #define STDBUF_H
20 
22 
23 #include <zlib.h>
24 
25 #include <cstdlib>
26 #include <cstring>
27 #include <cassert>
28 #include <cstdarg>
29 #include <algorithm>
30 
31 // debug memory management
32 #if defined(_DEBUG) && defined(_MSC_VER)
33 #include <crtdbg.h>
34 #endif
35 
36 // Base buffer class. Either references or holds data.
37 class StdBuf
38 {
39 public:
40 
41  // *** Construction
42  // Standard constructor
43  StdBuf() = default;
44 
45  // Constructor from other buffer (copy construction):
46  // Will take over buffer ownership. Copies data if specified.
47  // Note: Construct with Buf2.getRef() to construct a reference (This will work for a constant Buf2, too)
48  StdBuf(StdBuf & Buf2, bool fCopy = false)
49  : fRef(true), pData(nullptr), iSize(0)
50  {
51  if (fCopy)
52  Copy(Buf2);
53  else if (!Buf2.isRef())
54  Take(std::move(Buf2));
55  else
56  Ref(Buf2);
57  }
58  StdBuf(const StdBuf & Buf2, bool fCopy = true)
59  : fRef(true), pData(nullptr), iSize(0)
60  {
61  if (fCopy)
62  Copy(Buf2);
63  else
64  Ref(Buf2);
65  }
66  StdBuf(StdBuf && Buf2) noexcept
67  : fRef(true), pData(nullptr), iSize(0)
68  {
69  if (!Buf2.isRef())
70  Take(std::move(Buf2));
71  else
72  Ref(Buf2);
73  }
74 
75  // Set by constant data. Copies data if desired.
76  StdBuf(const void *pData, size_t iSize, bool fCopy = false)
77  : fRef(true), pData(pData), iSize(iSize)
78  {
79  if (fCopy) Copy();
80  }
81 
83  {
84  Clear();
85  }
86 
87 protected:
88 
89  // Reference? Otherwise, this object holds the data.
90  bool fRef = true;
91  // Data
92  union
93  {
94  const void *pData = 0;
95  void *pMData;
96 #if defined(_DEBUG)
97  char *szString; // for debugger preview
98 #endif
99  };
100  unsigned int iSize = 0;
101 
102 public:
103 
104  // *** Getters
105 
106  bool isNull() const { return ! getData(); }
107  const void *getData() const { return fRef ? pData : pMData; }
108  void *getMData() { assert(!fRef); return pMData; }
109  size_t getSize() const { return iSize; }
110  bool isRef() const { return fRef; }
111 
112  const void *getPtr(size_t i) const { return reinterpret_cast<const char*>(getData()) + i; }
113  void *getMPtr(size_t i) { return reinterpret_cast<char*>(getMData()) + i; }
114 
115  StdBuf getPart(size_t iStart, size_t inSize) const
116  {
117  assert(iStart + inSize <= iSize);
118  return StdBuf(getPtr(iStart), inSize);
119  }
120 
121  // *** Setters
122 
123  // * Direct setters
124 
125  // Reference given data
126  void Ref(const void *pnData, size_t inSize)
127  {
128  Clear();
129  fRef = true; pData = pnData; iSize = inSize;
130  }
131  // Take over data (hold it)
132  void Take(void *pnData, size_t inSize)
133  {
134  Clear();
135  if (pnData)
136  {
137  fRef = false; pMData = pnData; iSize = inSize;
138  }
139  }
140  // Transfer puffer ownership to the caller
141  void *GrabPointer()
142  {
143  if (isNull()) return nullptr;
144  // Do not give out a buffer which someone else will free
145  if (fRef) Copy();
146  void *pMData = getMData();
147  pData = pMData; fRef = true;
148  return pMData;
149  }
150 
151  // * Buffer data operations
152 
153  // Create new buffer with given size
154  void New(size_t inSize)
155  {
156  Clear();
157  pMData = malloc(iSize = inSize);
158  fRef = false;
159  }
160  // Write data into the buffer
161  void Write(const void *pnData, size_t inSize, size_t iAt = 0)
162  {
163  assert(iAt + inSize <= iSize);
164  if (pnData && inSize) std::memcpy(getMPtr(iAt), pnData, inSize);
165  }
166  // Move data around inside the buffer (checks overlap)
167  void Move(size_t iFrom, size_t inSize, size_t iTo = 0)
168  {
169  assert(iFrom + inSize <= iSize); assert(iTo + inSize <= iSize);
170  std::memmove(getMPtr(iTo), getPtr(iFrom), inSize);
171  }
172  // Compare to memory
173  int Compare(const void *pCData, size_t iCSize, size_t iAt = 0) const
174  {
175  assert(iAt + iCSize <= getSize());
176  return std::memcmp(getPtr(iAt), pCData, iCSize);
177  }
178  // Grow the buffer
179  void Grow(size_t iGrow)
180  {
181  // Grow dereferences
182  if (fRef) { Copy(iSize + iGrow); return; }
183  if (!iGrow) return;
184  // Realloc
185  pMData = realloc(pMData, iSize += iGrow);
186  }
187  // Shrink the buffer
188  void Shrink(size_t iShrink)
189  {
190  assert(iSize >= iShrink);
191  // Shrink dereferences
192  if (fRef) { Copy(iSize - iShrink); return; }
193  if (!iShrink) return;
194  // Realloc
195  pMData = realloc(pMData, iSize -= iShrink);
196  }
197  // Clear buffer
198  void Clear()
199  {
200  if (!fRef) free(pMData);
201  pMData = nullptr; fRef = true; iSize = 0;
202  }
203  // Free buffer that had been grabbed
204  static void DeletePointer(void *data)
205  {
206  free(data);
207  }
208 
209  // * Composed actions
210 
211  // Set buffer size (dereferences)
212  void SetSize(size_t inSize)
213  {
214  if (inSize > iSize)
215  Grow(inSize - iSize);
216  else
217  Shrink(iSize - inSize);
218  }
219 
220  // Write buffer contents into the buffer
221  void Write(const StdBuf &Buf2, size_t iAt = 0)
222  {
223  Write(Buf2.getData(), Buf2.getSize(), iAt);
224  }
225 
226  // Compare (a part of) this buffer's contents to another's
227  int Compare(const StdBuf &Buf2, size_t iAt = 0) const
228  {
229  return Compare(Buf2.getData(), Buf2.getSize(), iAt);
230  }
231 
232  // Create a copy of the data (dereferences, obviously)
233  void Copy(size_t inSize)
234  {
235  if (isNull() && !inSize) return;
236  const void *pOldData = getData();
237  size_t iOldSize = iSize;
238  New(inSize);
239  Write(pOldData, std::min(iOldSize, inSize));
240  }
241  void Copy()
242  {
243  Copy(iSize);
244  }
245  // Copy data from address
246  void Copy(const void *pnData, size_t inSize)
247  {
248  Ref(pnData, inSize); Copy();
249  }
250  // Copy from another buffer
251  void Copy(const StdBuf &Buf2)
252  {
253  Copy(Buf2.getData(), Buf2.getSize());
254  }
255  // Create a copy and return it
257  {
258  StdBuf Buf; Buf.Copy(*this); return Buf;
259  }
260 
261  // Append data from address
262  void Append(const void *pnData, size_t inSize)
263  {
264  Grow(inSize);
265  Write(pnData, inSize, iSize - inSize);
266  }
267  // Append data from another buffer
268  void Append(const StdBuf &Buf2)
269  {
270  Append(Buf2.getData(), Buf2.getSize());
271  }
272 
273  // Reference another buffer's contents
274  void Ref(const StdBuf &Buf2)
275  {
276  Ref(Buf2.getData(), Buf2.getSize());
277  }
278  // Create a reference to this buffer's contents
279  StdBuf getRef() const
280  {
281  return StdBuf(getData(), getSize());
282  }
283  // take over another buffer's contents
284  void Take(StdBuf & Buf2)
285  {
286  Take(Buf2.GrabPointer(), Buf2.getSize());
287  }
288  void Take(StdBuf &&Buf2)
289  {
290  Take(Buf2.GrabPointer(), Buf2.getSize());
291  }
292 
293  // * File support
294  bool LoadFromFile(const char *szFile);
295  bool SaveToFile(const char *szFile) const;
296 
297  // *** Operators
298 
299  // Null check
300  bool operator ! () const { return isNull(); }
301 
302  // Appending
304  {
305  Append(Buf2);
306  return *this;
307  }
308  StdBuf operator + (const StdBuf &Buf2) const
309  {
310  StdBuf Buf(getRef());
311  Buf.Append(Buf2);
312  return Buf;
313  }
314 
315  // Compare
316  bool operator == (const StdBuf &Buf2) const
317  {
318  return getSize() == Buf2.getSize() && !Compare(Buf2);
319  }
320  bool operator != (const StdBuf &Buf2) const { return ! operator == (Buf2); }
321 
322  // Set (as constructor: take if possible)
324  {
325  if (Buf2.isRef()) Ref(Buf2); else Take(std::move(Buf2));
326  return *this;
327  }
328 
329  // build a simple hash
330  int GetHash() const
331  {
332  if (isNull()) return 0;
333  return crc32(0, reinterpret_cast<const Bytef *>(getData()), getSize());
334  }
335 
336  // *** Compiling
337 
338  void CompileFunc(class StdCompiler *pComp, int iType = 0);
339 
340 };
341 
342 // Cast Hide Helpers - MSVC doesn't allow this as member template
343 template <class elem_t>
344 const elem_t *getBufPtr(const StdBuf &Buf, size_t iPos = 0)
345 {
346  // assert(iPos + sizeof(elem_t) <= Buf.getSize());
347  const void *pPos = reinterpret_cast<const char *>(Buf.getData()) + iPos;
348  return reinterpret_cast<const elem_t *>(pPos);
349 }
350 template <class elem_t>
351 elem_t *getMBufPtr(StdBuf &Buf, size_t iPos = 0)
352 {
353  // assert(iPos + sizeof(elem_t) <= Buf.getSize());
354  void *pPos = reinterpret_cast<char *>(Buf.getMData()) + iPos;
355  return reinterpret_cast<elem_t *>(pPos);
356 }
357 
358 // Copy-Buffer - Just copies data in the copy constructor.
359 class StdCopyBuf : public StdBuf
360 {
361 public:
362 
363  StdCopyBuf() = default;
364 
365  // Set by buffer. Copies data by default.
366  StdCopyBuf(const StdBuf &Buf2, bool fCopy = true)
367  : StdBuf(Buf2.getRef(), fCopy)
368  { }
369 
370  // Set by buffer. Copies data by default.
371  StdCopyBuf(const StdCopyBuf &Buf2, bool fCopy = true)
372  : StdBuf(Buf2.getRef(), fCopy)
373  { }
374  StdCopyBuf(StdBuf & Buf2) noexcept
375  : StdBuf(std::move(Buf2))
376  { }
377  StdCopyBuf(StdCopyBuf &&Buf2) noexcept
378  : StdBuf(std::move(Buf2))
379  { }
380 
381  // Set by constant data. Copies data by default.
382  StdCopyBuf(const void *pData, size_t iSize, bool fCopy = true)
383  : StdBuf(pData, iSize, fCopy)
384  { }
385 
386  StdCopyBuf &operator = (const StdBuf &Buf2) { Copy(Buf2); return *this; }
387  StdCopyBuf &operator = (const StdCopyBuf &Buf2) { Copy(Buf2); return *this; }
388 
389 };
390 
391 // Stringbuffer (operates on null-terminated character buffers)
392 class StdStrBuf : protected StdBuf
393 {
394 public:
395 
396  // *** Construction
397 
398  StdStrBuf() = default;
399 
400  // See StdBuf::StdBuf. Will take data if possible.
401  // The static_cast is necessary to pass a rvalue reference to
402  // the StdBuf constructor. Without it, the const lvalue
403  // StdBuf constructor will be used, which will ref the contents
404  // instead of moving them.
405  StdStrBuf(StdStrBuf & Buf2, bool fCopy = false)
406 // : StdBuf(static_cast<StdStrBuf &>(Buf2), fCopy)
407  : StdBuf(Buf2, fCopy)
408  { }
409 
410  // This constructor is important, because the compiler will create one
411  // otherwise, despite having two other constructors to choose from
412  StdStrBuf(const StdStrBuf & Buf2, bool fCopy = true)
413  : StdBuf(Buf2, fCopy)
414  { }
415  StdStrBuf(StdStrBuf && Buf2) noexcept
416  : StdBuf(std::move(Buf2))
417  { }
418 
419  // Set by constant data. References data by default, copies if specified.
420  explicit StdStrBuf(const char *pData, bool fCopy = false)
421  : StdBuf(pData, pData ? strlen(pData) + 1 : 0, fCopy)
422  { }
423 
424 #ifdef _WIN32
425  explicit StdStrBuf(const wchar_t * utf16);
426  struct wchar_t_holder {
427  wchar_t * p;
428  wchar_t_holder(wchar_t * p): p(p) { }
429  wchar_t_holder(const wchar_t_holder &);
430  ~wchar_t_holder() { delete[] p; }
431  operator wchar_t * () { return p; }
432  };
433  wchar_t_holder GetWideChar() const;
435 #endif
436 
437  // As previous constructor, but set length manually.
438  StdStrBuf(const char *pData, long int iLength)
439  : StdBuf(pData, pData ? iLength + 1 : 0, false)
440  { }
441  StdStrBuf(const char *pData, size_t iLength, bool fCopy = false)
442  : StdBuf(pData, pData ? iLength + 1 : 0, fCopy)
443  { }
444 
445 public:
446 
447  // *** Getters
448 
449  bool isNull() const { return StdBuf::isNull(); }
450  const char *getData() const { return getBufPtr<char>(*this); }
451  char *getMData() { return getMBufPtr<char>(*this); }
452  size_t getSize() const { return StdBuf::getSize(); }
453  size_t getLength() const { return getSize() ? getSize() - 1 : 0; }
454  bool isRef() const { return StdBuf::isRef(); }
455 
456  const char *getPtr(size_t i) const { return getBufPtr<char>(*this, i); }
457  char *getMPtr(size_t i) { return getMBufPtr<char>(*this, i); }
458 
459  // For convenience. Note that writing can't be allowed.
460  char operator [] (size_t i) const { return *getPtr(i); }
461 
462  // Analogous to StdBuf
463  void Ref(const char *pnData) { StdBuf::Ref(pnData, pnData ? std::strlen(pnData) + 1 : 0); }
464  void Ref(const char *pnData, size_t iLength) { assert((!pnData && !iLength) || std::strlen(pnData) == iLength); StdBuf::Ref(pnData, iLength + 1); }
465  void Take(char *pnData) { StdBuf::Take(pnData, pnData ? std::strlen(pnData) + 1 : 0); }
466  void Take(char *pnData, size_t iLength) { assert((!pnData && !iLength) || std::strlen(pnData) == iLength); StdBuf::Take(pnData, iLength + 1); }
467  char *GrabPointer() { return reinterpret_cast<char *>(StdBuf::GrabPointer()); }
468 
469  void Ref(const StdStrBuf &Buf2) { StdBuf::Ref(Buf2.getData(), Buf2.getSize()); }
470  StdStrBuf getRef() const { return StdStrBuf(getData(), getLength()); }
471  void Take(StdStrBuf & Buf2) { StdBuf::Take(Buf2); }
472  void Take(StdStrBuf &&Buf2) { StdBuf::Take(std::move(Buf2)); }
473 
474  void Clear() { StdBuf::Clear(); }
475  void Copy() { StdBuf::Copy(); }
476  void Copy(const char *pnData) { StdBuf::Copy(pnData, pnData ? std::strlen(pnData) + 1 : 0); }
477  void Copy(const StdStrBuf &Buf2) { StdBuf::Copy(Buf2); }
478  StdStrBuf Duplicate() const { StdStrBuf Buf; Buf.Copy(*this); return Buf; }
479  void Move(size_t iFrom, size_t inSize, size_t iTo = 0) { StdBuf::Move(iFrom, inSize, iTo); }
480 
481  // Byte-wise compare (will compare this string from iAt to the full string in Buf2)
482  int Compare(const StdStrBuf &Buf2, size_t iAt = 0) const
483  {
484  assert(iAt <= getLength());
485  const int result = StdBuf::Compare(Buf2.getData(), std::min(getLength() - iAt, Buf2.getLength()), iAt);
486  if (result) return result;
487 
488  if (getLength() < Buf2.getLength() + iAt) return -1;
489  else if (getLength() > Buf2.getLength() + iAt) return 1;
490  return 0;
491  }
492  int Compare_(const char *pCData, size_t iAt = 0) const
493  {
494  StdStrBuf str(pCData); // GCC needs this, for some obscure reason
495  return Compare(str, iAt);
496  }
497  bool BeginsWith(const char *beginning) const
498  {
499  // Return whether string starts with beginning
500  return strncmp((const char
501  *)pData, beginning, strlen(beginning)) == 0;
502  }
503 
504  // Grows the string to contain the specified number more/less characters.
505  // Note: Will set the terminator, but won't initialize - use Append* instead.
506  void Grow(size_t iGrow)
507  {
508  StdBuf::Grow(getSize() ? iGrow : iGrow + 1);
509  *getMPtr(getLength()) = '\0';
510  }
511  void Shrink(size_t iShrink)
512  {
513  assert(iShrink <= getLength());
514  StdBuf::Shrink(iShrink);
515  *getMPtr(getLength()) = '\0';
516  }
517  void SetLength(size_t iLength)
518  {
519  if (iLength == getLength() && !isNull()) return;
520  if (iLength >= getLength())
521  Grow(iLength - getLength());
522  else
523  Shrink(getLength() - iLength);
524  }
525 
526  // Append string
527  void Append(const char *pnData, size_t iChars)
528  {
529  Grow(iChars);
530  Write(pnData, iChars, iSize - iChars - 1);
531  }
532  void Append(const char *pnData)
533  {
534  Append(pnData, std::strlen(pnData));
535  }
536  void Append(const StdStrBuf &Buf2)
537  {
538  Append(Buf2.getData(), Buf2.getLength());
539  }
540 
541  // Copy string
542  void Copy(const char *pnData, size_t iChars)
543  {
544  Clear();
545  Append(pnData, iChars);
546  }
547 
548  // * File support
549  bool LoadFromFile(const char *szFile);
550  bool SaveToFile(const char *szFile) const;
551 
552  // * Operators
553 
554  bool operator ! () const { return isNull(); }
555 
556  StdStrBuf &operator += (const StdStrBuf &Buf2) { Append(Buf2); return *this; }
557  StdStrBuf &operator += (const char *szString) { Append(szString); return *this; }
558  StdStrBuf operator + (const StdStrBuf &Buf2) const { StdStrBuf Buf = getRef(); Buf.Append(Buf2); return Buf; }
559  StdStrBuf operator + (const char *szString) const { StdStrBuf Buf = getRef(); Buf.Append(szString); return Buf; }
560  StdStrBuf operator + (char c) const { StdStrBuf Buf = getRef(); Buf.AppendChar(c); return Buf; }
561 
562  bool operator == (const StdStrBuf &Buf2) const
563  {
564  return getLength() == Buf2.getLength() && !Compare(Buf2);
565  }
566  bool operator != (const StdStrBuf &Buf2) const { return !operator == (Buf2); }
567 
568  bool operator == (const char *szString) const { return StdStrBuf(szString) == *this; }
569  bool operator != (const char *szString) const { return ! operator == (szString); }
570 
571  // Note this references the data.
572  StdStrBuf &operator = (const StdStrBuf &Buf2) { Ref(Buf2); return *this; }
573  StdStrBuf &operator = (const char *szString) { Ref(szString); return *this; }
574 
575  // conversion to "bool"
576  operator const void *() const { return getData(); }
577 
578  // less-than operation for map
579  inline bool operator <(const StdStrBuf &v2) const
580  {
581  size_t iLen = getLength(), iLen2 = v2.getLength();
582  if (iLen == iLen2)
583  return iLen ? (std::strcmp(getData(), v2.getData())<0) : false;
584  else
585  return iLen < iLen2;
586  }
587 
588  // * String specific
589 
590  void AppendChars(char cChar, size_t iCnt)
591  {
592  Grow(iCnt);
593  for (size_t i = getLength() - iCnt; i < getLength(); i++)
594  *getMPtr(i) = cChar;
595  }
596  void AppendChar(char cChar)
597  {
598  AppendChars(cChar, 1);
599  }
600  void AppendCharacter(uint32_t unicodechar);
601  void AppendBackslash();
602  void InsertChar(char cChar, size_t insert_before)
603  {
604  assert(insert_before <= getLength());
605  Grow(1);
606  for (size_t i = getLength()-1; i > insert_before; --i)
607  *getMPtr(i) = *getPtr(i-1);
608  *getMPtr(insert_before) = cChar;
609  }
610 
611  // Append data until given character (or string end) occurs.
612  void AppendUntil(const char *szString, char cUntil)
613  {
614  const char *pPos = std::strchr(szString, cUntil);
615  if (pPos)
616  Append(szString, pPos - szString);
617  else
618  Append(szString);
619  }
620  // See above
621  void CopyUntil(const char *szString, char cUntil)
622  {
623  Clear();
624  AppendUntil(szString, cUntil);
625  }
626  // cut end after given char into another string. Return whether char was found at all
627  bool SplitAtChar(char cSplit, StdStrBuf *psSplit)
628  {
629  if (!getData()) return false;
630  const char *pPos = std::strchr(getData(), cSplit);
631  if (!pPos) return false;
632  size_t iPos = pPos - getData();
633  if (psSplit) psSplit->Take(copyPart(iPos + 1, getLength() - iPos - 1));
634  Shrink(getLength() - iPos);
635  return true;
636  }
637 
638  void Format(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
639  void FormatV(const char *szFmt, va_list args);
640  void AppendFormat(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE_O;
641  void AppendFormatV(const char *szFmt, va_list args);
642 
643  StdStrBuf copyPart(size_t iStart, size_t inSize) const
644  {
645  assert(iStart + inSize <= iSize);
646  if (!inSize) return StdStrBuf();
647  StdStrBuf sResult;
648  sResult.Copy(getPtr(iStart), inSize);
649  return sResult;
650  }
651 
652  // replace all occurences of one string with another. Return number of replacements.
653  int Replace(const char *szOld, const char *szNew, size_t iStartSearch=0);
654  int ReplaceChar(char cOld, char cNew);
655 
656  // replace the trailing part of a string with something else
657  void ReplaceEnd(size_t iPos, const char *szNewEnd);
658 
659  // get an indexed section from the string like Section1;Section2;Section3
660  bool GetSection(size_t idx, StdStrBuf *psOutSection, char cSeparator=';') const;
661 
662  // Checks whether the content is valid UTF-8, and if not, convert it from windows-1252 to UTF-8 and return true.
663  bool EnsureUnicode();
664 
665  // convert to lower case
666  void ToLowerCase();
667 
668  // check if a string consists only of the given chars
669  bool ValidateChars(const char *szInitialChars, const char *szMidChars);
670 
671  // build a simple hash
672  int GetHash() const
673  {
674  return StdBuf::GetHash();
675  }
676 
678  {
679  Replace("\\", "\\\\");
680  Replace("\"", "\\\"");
681  }
682 
683  bool TrimSpaces(); // kill spaces at beginning and end. Return if changed.
684 
685  // * Compiling
686 
687  void CompileFunc(class StdCompiler *pComp, int iRawType = 0);
688 
689 };
690 
691 // Copy-Stringbuffer - Just copies data in the copy constructor.
692 class StdCopyStrBuf : public StdStrBuf
693 {
694 public:
695 
696  StdCopyStrBuf() = default;
697 
698  explicit StdCopyStrBuf(const StdStrBuf &Buf2, bool fCopy = true)
699  : StdStrBuf(Buf2.getRef(), fCopy)
700  { }
701 
702  StdCopyStrBuf(const StdCopyStrBuf &Buf2, bool fCopy = true)
703  : StdStrBuf(Buf2.getRef(), fCopy)
704  { }
705  StdCopyStrBuf(StdStrBuf && Buf2) noexcept
706  : StdStrBuf(std::move(Buf2))
707  { }
708  StdCopyStrBuf(StdCopyStrBuf && Buf2) noexcept
709  : StdStrBuf(std::move(Buf2))
710  { }
711 
712  // Set by constant data. Copies data if desired.
713  explicit StdCopyStrBuf(const char *pData, bool fCopy = true)
714  : StdStrBuf(pData, fCopy)
715  { }
716 
717 #ifdef _WIN32
718  explicit StdCopyStrBuf(const wchar_t * utf16): StdStrBuf(utf16) {}
719 #endif
720 
721  StdCopyStrBuf(const std::string &s) noexcept
722  : StdStrBuf(s.c_str(), s.size(), true)
723  { }
724 
725  StdCopyStrBuf &operator = (const StdStrBuf &Buf2) { Copy(Buf2); return *this; }
726  StdCopyStrBuf &operator = (const StdCopyStrBuf &Buf2) { Copy(Buf2); return *this; }
727  StdCopyStrBuf &operator = (const char *szString) { Copy(szString); return *this; }
728  StdCopyStrBuf &operator = (const std::string &s) { Copy(s.c_str(), s.size()); return *this; }
729 
730  operator std::string() const
731  {
732  return std::string(getData(), getLength());
733  }
734 };
735 
736 // Wrappers
737 extern StdStrBuf FormatString(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE;
738 extern StdStrBuf FormatStringV(const char *szFmt, va_list args);
739 
740 #ifdef _WIN32
741 // Converts a wide char string to UTF-8 std::string
742 std::string WStrToString(wchar_t *s);
743 #endif
744 
745 #endif
const char * getData() const
Definition: StdBuf.h:450
void Append(const StdBuf &Buf2)
Definition: StdBuf.h:268
~StdBuf()
Definition: StdBuf.h:82
bool GetSection(size_t idx, StdStrBuf *psOutSection, char cSeparator=';') const
Definition: StdBuf.cpp:376
const void * getData() const
Definition: StdBuf.h:107
void Copy(const char *pnData)
Definition: StdBuf.h:476
char * GrabPointer()
Definition: StdBuf.h:467
Definition: StdBuf.h:37
void Take(void *pnData, size_t inSize)
Definition: StdBuf.h:132
bool isRef() const
Definition: StdBuf.h:110
int Replace(const char *szOld, const char *szNew, size_t iStartSearch=0)
Definition: StdBuf.cpp:291
bool isRef() const
Definition: StdBuf.h:454
bool operator!=(const StdStrBuf &Buf2) const
Definition: StdBuf.h:566
#define GNUC_FORMAT_ATTRIBUTE
StdBuf operator+=(const StdBuf &Buf2)
Definition: StdBuf.h:303
StdCopyBuf & operator=(const StdBuf &Buf2)
Definition: StdBuf.h:386
StdCopyBuf(StdCopyBuf &&Buf2) noexcept
Definition: StdBuf.h:377
void Copy(const void *pnData, size_t inSize)
Definition: StdBuf.h:246
bool SplitAtChar(char cSplit, StdStrBuf *psSplit)
Definition: StdBuf.h:627
void Clear()
Definition: StdBuf.h:474
void Ref(const StdBuf &Buf2)
Definition: StdBuf.h:274
void AppendUntil(const char *szString, char cUntil)
Definition: StdBuf.h:612
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
StdCopyStrBuf(const StdCopyStrBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:702
void Take(StdBuf &Buf2)
Definition: StdBuf.h:284
int Compare(const StdStrBuf &Buf2, size_t iAt=0) const
Definition: StdBuf.h:482
void Clear()
Definition: StdBuf.h:198
StdCopyBuf(StdBuf &Buf2) noexcept
Definition: StdBuf.h:374
bool LoadFromFile(const char *szFile)
Definition: StdBuf.cpp:39
static void DeletePointer(void *data)
Definition: StdBuf.h:204
void * GrabPointer()
Definition: StdBuf.h:141
StdCopyBuf(const StdBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:366
void Copy()
Definition: StdBuf.h:241
elem_t * getMBufPtr(StdBuf &Buf, size_t iPos=0)
Definition: StdBuf.h:351
bool ValidateChars(const char *szInitialChars, const char *szMidChars)
Definition: StdBuf.cpp:367
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:181
StdStrBuf(const StdStrBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:412
void CompileFunc(class StdCompiler *pComp, int iType=0)
Definition: StdBuf.cpp:121
int GetHash() const
Definition: StdBuf.h:672
void Grow(size_t iGrow)
Definition: StdBuf.h:179
StdStrBuf(StdStrBuf &&Buf2) noexcept
Definition: StdBuf.h:415
void AppendChars(char cChar, size_t iCnt)
Definition: StdBuf.h:590
bool isNull() const
Definition: StdBuf.h:106
StdStrBuf(const char *pData, bool fCopy=false)
Definition: StdBuf.h:420
StdCopyStrBuf(const StdStrBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:698
StdCopyStrBuf & operator=(const StdStrBuf &Buf2)
Definition: StdBuf.h:725
size_t getSize() const
Definition: StdBuf.h:109
void SetSize(size_t inSize)
Definition: StdBuf.h:212
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:115
void AppendFormat(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:197
StdCopyStrBuf()=default
char * getMData()
Definition: StdBuf.h:451
void AppendChar(char cChar)
Definition: StdBuf.h:596
StdCopyStrBuf(const std::string &s) noexcept
Definition: StdBuf.h:721
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:173
void Take(StdStrBuf &&Buf2)
Definition: StdBuf.h:472
void ToLowerCase()
Definition: StdBuf.cpp:392
bool LoadFromFile(const char *szFile)
Definition: StdBuf.cpp:80
StdStrBuf(const char *pData, long int iLength)
Definition: StdBuf.h:438
void AppendBackslash()
Definition: StdBuf.cpp:255
StdBuf & operator=(StdBuf &&Buf2)
Definition: StdBuf.h:323
#define GNUC_FORMAT_ATTRIBUTE_O
StdStrBuf getRef() const
Definition: StdBuf.h:470
bool SaveToFile(const char *szFile) const
Definition: StdBuf.cpp:101
int iCnt
Definition: TstC4NetIO.cpp:35
void Take(StdBuf &&Buf2)
Definition: StdBuf.h:288
StdBuf(StdBuf &Buf2, bool fCopy=false)
Definition: StdBuf.h:48
StdCopyStrBuf(StdStrBuf &&Buf2) noexcept
Definition: StdBuf.h:705
void Copy(size_t inSize)
Definition: StdBuf.h:233
void Ref(const void *pnData, size_t inSize)
Definition: StdBuf.h:126
void AppendCharacter(uint32_t unicodechar)
Definition: StdBuf.cpp:399
void Append(const StdStrBuf &Buf2)
Definition: StdBuf.h:536
void Take(char *pnData)
Definition: StdBuf.h:465
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
void ReplaceEnd(size_t iPos, const char *szNewEnd)
Definition: StdBuf.cpp:358
StdCopyStrBuf(const char *pData, bool fCopy=true)
Definition: StdBuf.h:713
void Shrink(size_t iShrink)
Definition: StdBuf.h:188
StdCopyBuf(const StdCopyBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:371
StdBuf(StdBuf &&Buf2) noexcept
Definition: StdBuf.h:66
void InsertChar(char cChar, size_t insert_before)
Definition: StdBuf.h:602
StdBuf(const void *pData, size_t iSize, bool fCopy=false)
Definition: StdBuf.h:76
StdStrBuf FormatString(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE
Definition: StdBuf.cpp:277
bool operator!() const
Definition: StdBuf.h:554
bool operator!() const
Definition: StdBuf.h:300
bool BeginsWith(const char *beginning) const
Definition: StdBuf.h:497
StdStrBuf & operator+=(const StdStrBuf &Buf2)
Definition: StdBuf.h:556
int ReplaceChar(char cOld, char cNew)
Definition: StdBuf.cpp:343
void Ref(const StdStrBuf &Buf2)
Definition: StdBuf.h:469
void New(size_t inSize)
Definition: StdBuf.h:154
bool SaveToFile(const char *szFile) const
Definition: StdBuf.cpp:60
void CopyUntil(const char *szString, char cUntil)
Definition: StdBuf.h:621
StdStrBuf copyPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:643
void AppendFormatV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:205
int Compare(const StdBuf &Buf2, size_t iAt=0) const
Definition: StdBuf.h:227
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:161
StdStrBuf(const char *pData, size_t iLength, bool fCopy=false)
Definition: StdBuf.h:441
void Ref(const char *pnData)
Definition: StdBuf.h:463
StdStrBuf operator+(const StdStrBuf &Buf2) const
Definition: StdBuf.h:558
StdCopyBuf()=default
void Append(const void *pnData, size_t inSize)
Definition: StdBuf.h:262
void Grow(size_t iGrow)
Definition: StdBuf.h:506
const elem_t * getBufPtr(const StdBuf &Buf, size_t iPos=0)
Definition: StdBuf.h:344
void Write(const StdBuf &Buf2, size_t iAt=0)
Definition: StdBuf.h:221
void Copy(const char *pnData, size_t iChars)
Definition: StdBuf.h:542
StdBuf getRef() const
Definition: StdBuf.h:279
void * getMPtr(size_t i)
Definition: StdBuf.h:113
StdBuf GetWideCharBuf(const char *utf8)
bool fRef
Definition: StdBuf.h:90
size_t getSize() const
Definition: StdBuf.h:452
bool EnsureUnicode()
Definition: StdBuf.cpp:428
bool isNull() const
Definition: StdBuf.h:449
void Take(char *pnData, size_t iLength)
Definition: StdBuf.h:466
void Shrink(size_t iShrink)
Definition: StdBuf.h:511
void Take(StdStrBuf &Buf2)
Definition: StdBuf.h:471
bool operator==(const StdStrBuf &Buf2) const
Definition: StdBuf.h:562
bool operator<(const StdStrBuf &v2) const
Definition: StdBuf.h:579
void FormatV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:189
StdBuf()=default
unsigned int iSize
Definition: StdBuf.h:100
const char * getPtr(size_t i) const
Definition: StdBuf.h:456
const void * getPtr(size_t i) const
Definition: StdBuf.h:112
size_t getLength() const
Definition: StdBuf.h:453
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:167
void Copy(const StdStrBuf &Buf2)
Definition: StdBuf.h:477
bool TrimSpaces()
Definition: StdBuf.cpp:477
void SetLength(size_t iLength)
Definition: StdBuf.h:517
StdCopyStrBuf(StdCopyStrBuf &&Buf2) noexcept
Definition: StdBuf.h:708
StdBuf(const StdBuf &Buf2, bool fCopy=true)
Definition: StdBuf.h:58
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:479
char * getMPtr(size_t i)
Definition: StdBuf.h:457
StdCopyBuf(const void *pData, size_t iSize, bool fCopy=true)
Definition: StdBuf.h:382
int GetHash() const
Definition: StdBuf.h:330
StdStrBuf & operator=(const StdStrBuf &Buf2)
Definition: StdBuf.h:572
void Ref(const char *pnData, size_t iLength)
Definition: StdBuf.h:464
void EscapeString()
Definition: StdBuf.h:677
StdStrBuf(StdStrBuf &Buf2, bool fCopy=false)
Definition: StdBuf.h:405
StdBuf operator+(const StdBuf &Buf2) const
Definition: StdBuf.h:308
bool operator!=(const StdBuf &Buf2) const
Definition: StdBuf.h:320
void Copy()
Definition: StdBuf.h:475
#define s
void CompileFunc(class StdCompiler *pComp, int iRawType=0)
Definition: StdBuf.cpp:261
char operator[](size_t i) const
Definition: StdBuf.h:460
void * getMData()
Definition: StdBuf.h:108
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:283
void Copy(const StdBuf &Buf2)
Definition: StdBuf.h:251
StdStrBuf Duplicate() const
Definition: StdBuf.h:478
bool operator==(const StdBuf &Buf2) const
Definition: StdBuf.h:316
void Append(const char *pnData)
Definition: StdBuf.h:532
StdBuf Duplicate() const
Definition: StdBuf.h:256
StdStrBuf()=default
int Compare_(const char *pCData, size_t iAt=0) const
Definition: StdBuf.h:492