OpenTTD
str.hpp
Go to the documentation of this file.
1 /* $Id: str.hpp 27362 2015-08-08 10:06:24Z alberth $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef STR_HPP
13 #define STR_HPP
14 
15 #include <errno.h>
16 #include <stdarg.h>
17 #include "blob.hpp"
18 #include "../core/math_func.hpp"
19 #include "../string_func.h"
20 
22 struct CStrA : public CBlobT<char>
23 {
24  typedef CBlobT<char> base;
25 
27  inline CStrA()
28  {
29  }
30 
32  inline CStrA(const CStrA &src) : base(src)
33  {
34  base::FixTail();
35  }
36 
38  inline CStrA(const OnTransfer &ot)
39  : base(ot)
40  {
41  }
42 
44  inline char *GrowSizeNC(uint count)
45  {
46  char *ret = base::GrowSizeNC(count);
47  base::FixTail();
48  return ret;
49  }
50 
52  inline void AppendStr(const char *str)
53  {
54  if (!StrEmpty(str)) {
55  base::AppendRaw(str, strlen(str));
56  base::FixTail();
57  }
58  }
59 
61  inline void Append(const CStrA &src)
62  {
63  if (src.Length() > 0) {
64  base::AppendRaw(src);
65  base::FixTail();
66  }
67  }
68 
70  inline CStrA &operator=(const char *src)
71  {
72  base::Clear();
73  AppendStr(src);
74  return *this;
75  }
76 
78  inline CStrA &operator=(const CStrA &src)
79  {
80  if (&src != this) {
81  base::Clear();
82  base::AppendRaw(src.Data(), src.Size());
83  base::FixTail();
84  }
85  return *this;
86  }
87 
89  inline bool operator<(const CStrA &other) const
90  {
91  return strcmp(base::Data(), other.Data()) < 0;
92  }
93 
95  int AddFormatL(const char *format, va_list args)
96  {
97  size_t addSize = max<size_t>(strlen(format), 16);
98  addSize += addSize / 2;
99  int ret;
100  int err = 0;
101  for (;;) {
102  char *buf = MakeFreeSpace(addSize);
103  ret = vseprintf(buf, buf + base::GetReserve() - 1, format, args);
104  if (ret >= (int)base::GetReserve()) {
105  /* Greater return than given count means needed buffer size. */
106  addSize = ret + 1;
107  continue;
108  }
109  if (ret >= 0) {
110  /* success */
111  break;
112  }
113  err = errno;
114  if (err != ERANGE && err != ENOENT && err != 0) {
115  /* some strange failure */
116  break;
117  }
118  /* small buffer (M$ implementation) */
119  addSize *= 2;
120  }
121  if (ret > 0) {
122  GrowSizeNC(ret);
123  } else {
124  base::FixTail();
125  }
126  return ret;
127  }
128 
130  int CDECL WARN_FORMAT(2, 3) AddFormat(const char *format, ...)
131  {
132  va_list args;
133  va_start(args, format);
134  int ret = AddFormatL(format, args);
135  va_end(args);
136  return ret;
137  }
138 
140  int CDECL WARN_FORMAT(2, 3) Format(const char *format, ...)
141  {
142  base::Free();
143  va_list args;
144  va_start(args, format);
145  int ret = AddFormatL(format, args);
146  va_end(args);
147  return ret;
148  }
149 };
150 
151 #endif /* STR_HPP */
CStrA & operator=(const char *src)
Assignment from C string.
Definition: str.hpp:70
CStrA & operator=(const CStrA &src)
Assignment from another CStrA.
Definition: str.hpp:78
CBlobT< char > base
base class
Definition: str.hpp:24
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:50
bool operator<(const CStrA &other) const
Lower-than operator (to support stl collections)
Definition: str.hpp:89
size_t GetReserve() const
Return number of additional items that can fit in the Blob without buffer reallocation.
Definition: blob.hpp:396
int AddFormatL(const char *format, va_list args)
Add formated string (like vsprintf) at the end of existing contents.
Definition: str.hpp:95
size_t Length() const
return the number of valid data bytes in the blob
Definition: blob.hpp:183
Blob - simple dynamic T array.
Definition: blob.hpp:308
CStrA()
Create an empty CStrT.
Definition: str.hpp:27
CStrA(const OnTransfer &ot)
Take over ownership constructor.
Definition: str.hpp:38
char * GrowSizeNC(size_t num_items)
Grow number of data items in Blob by given number - doesn&#39;t construct items.
Definition: blob.hpp:402
void Clear()
invalidate blob&#39;s data - doesn&#39;t free buffer
Definition: blob.hpp:207
Blob based case sensitive ANSI/UTF-8 string.
Definition: str.hpp:22
char * GrowSizeNC(uint count)
Grow the actual buffer and fix the trailing zero at the end.
Definition: str.hpp:44
Support for storing random binary data.
void Append(const CStrA &src)
Append another CStrA.
Definition: str.hpp:61
char * MakeFreeSpace(size_t num_items)
Ensures that given number of items can be added to the end of Blob.
Definition: blob.hpp:411
CStrA(const CStrA &src)
Copy constructor.
Definition: str.hpp:32
int CDECL WARN_FORMAT(2, 3) AddFormat(const char *format
Add formated string (like sprintf) at the end of existing contents.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
T * Data()
Return pointer to the first data item - non-const version.
Definition: blob.hpp:358
void AppendRaw(const void *p, size_t num_bytes)
append new bytes at the end of existing data bytes - reallocates if necessary
Definition: blob.hpp:222
size_t Size() const
Return number of items in the Blob.
Definition: blob.hpp:384
void Free()
free the blob&#39;s memory
Definition: blob.hpp:213
void FixTail() const
fixing the four bytes at the end of blob data - useful when blob is used to hold string ...
Definition: blob.hpp:287
void AppendStr(const char *str)
Append zero-ended C string.
Definition: str.hpp:52