OpenTTD Source: src/misc/str.hpp Source File

OpenTTD Source 1.11.0-RC1

str.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifndef STR_HPP
11 #define STR_HPP
12
13 #include <errno.h>
14 #include <stdarg.h>
15 #include "blob.hpp"
16 #include "../core/math_func.hpp"
17 #include "../string_func.h"
18
20 struct CStrA : public CBlobT<char>
21 {
22 typedef CBlobT<char> base;
23
25 inline CStrA()
26 {
27 }
28
30 inline CStrA(const CStrA &src) : base(src)
31 {
32 base::FixTail();
33 }
34
36 inline CStrA(const OnTransfer &ot)
37 : base(ot)
38 {
39 }
40
42 inline char *GrowSizeNC(uint count)
43 {
44 char *ret = base::GrowSizeNC(count);
45 base::FixTail();
46 return ret;
47 }
48
50 inline void AppendStr(const char *str)
51 {
52 if (!StrEmpty(str)) {
53 base::AppendRaw(str, strlen(str));
54 base::FixTail();
55 }
56 }
57
59 inline void Append(const CStrA &src)
60 {
61 if (src.Length() > 0) {
62 base::AppendRaw(src);
63 base::FixTail();
64 }
65 }
66
68 inline CStrA &operator=(const char *src)
69 {
70 base::Clear();
71 AppendStr(src);
72 return *this;
73 }
74
76 inline CStrA &operator=(const CStrA &src)
77 {
78 if (&src != this) {
79 base::Clear();
80 base::AppendRaw(src.Data(), src.Size());
81 base::FixTail();
82 }
83 return *this;
84 }
85
87 inline bool operator<(const CStrA &other) const
88 {
89 return strcmp(base::Data(), other.Data()) < 0;
90 }
91
93 int AddFormatL(const char *format, va_list args) WARN_FORMAT(2, 0)
94 {
95 size_t addSize = std::max<size_t>(strlen(format), 16);
96 addSize += addSize / 2;
97 int ret;
98 int err = 0;
99 for (;;) {
100 char *buf = MakeFreeSpace(addSize);
101 ret = vseprintf(buf, buf + base::GetReserve() - 1, format, args);
102 if (ret >= (int)base::GetReserve()) {
103 /* Greater return than given count means needed buffer size. */
104 addSize = ret + 1;
105 continue;
106 }
107 if (ret >= 0) {
108 /* success */
109 break;
110 }
111 err = errno;
112 if (err != ERANGE && err != ENOENT && err != 0) {
113 /* some strange failure */
114 break;
115 }
116 /* small buffer (M$ implementation) */
117 addSize *= 2;
118 }
119 if (ret > 0) {
120 GrowSizeNC(ret);
121 } else {
122 base::FixTail();
123 }
124 return ret;
125 }
126
128 int CDECL WARN_FORMAT(2, 3) AddFormat(const char *format, ...)
129 {
130 va_list args;
131 va_start(args, format);
132 int ret = AddFormatL(format, args);
133 va_end(args);
134 return ret;
135 }
136
138 int CDECL WARN_FORMAT(2, 3) Format(const char *format, ...)
139 {
140 base::Free();
141 va_list args;
142 va_start(args, format);
143 int ret = AddFormatL(format, args);
144 va_end(args);
145 return ret;
146 }
147 };
148
149 #endif /* STR_HPP */
CBlobT< char >::GetReserve
size_t GetReserve() const
Return number of additional items that can fit in the Blob without buffer reallocation.
Definition: blob.hpp:394
CStrA
Blob based case sensitive ANSI/UTF-8 string.
Definition: str.hpp:20
CBlobT
Blob - simple dynamic T array.
Definition: blob.hpp:306
ByteBlob::Free
void Free()
free the blob's memory
Definition: blob.hpp:211
CStrA::GrowSizeNC
char * GrowSizeNC(uint count)
Grow the actual buffer and fix the trailing zero at the end.
Definition: str.hpp:42
CBlobT< char >::GrowSizeNC
char * GrowSizeNC(size_t num_items)
Grow number of data items in Blob by given number - doesn't construct items.
Definition: blob.hpp:400
ByteBlob::Clear
void Clear()
invalidate blob's data - doesn't free buffer
Definition: blob.hpp:205
ByteBlob::Length
size_t Length() const
return the number of valid data bytes in the blob
Definition: blob.hpp:181
CBlobT::Size
size_t Size() const
Return number of items in the Blob.
Definition: blob.hpp:382
CStrA::WARN_FORMAT
int CDECL WARN_FORMAT(2, 3) AddFormat(const char *format
Add formatted string (like sprintf) at the end of existing contents.
CStrA::AppendStr
void AppendStr(const char *str)
Append zero-ended C string.
Definition: str.hpp:50
CStrA::operator=
CStrA & operator=(const char *src)
Assignment from C string.
Definition: str.hpp:68
CStrA::base
CBlobT< char > base
base class
Definition: str.hpp:22
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
CStrA::CStrA
CStrA(const CStrA &src)
Copy constructor.
Definition: str.hpp:30
CStrA::CStrA
CStrA()
Create an empty CStrT.
Definition: str.hpp:25
vseprintf
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:61
CStrA::AddFormatL
int AddFormatL(const char *format, va_list args) WARN_FORMAT(2
Add formatted string (like vsprintf) at the end of existing contents.
ByteBlob::FixTail
void FixTail() const
fixing the four bytes at the end of blob data - useful when blob is used to hold string
Definition: blob.hpp:285
CStrA::operator<
bool operator<(const CStrA &other) const
Lower-than operator (to support stl collections)
Definition: str.hpp:87
ByteBlob::AppendRaw
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:220
blob.hpp
CBlobT< char >::MakeFreeSpace
char * MakeFreeSpace(size_t num_items)
Ensures that given number of items can be added to the end of Blob.
Definition: blob.hpp:409
CStrA::CStrA
CStrA(const OnTransfer &ot)
Take over ownership constructor.
Definition: str.hpp:36
CStrA::Append
void Append(const CStrA &src)
Append another CStrA.
Definition: str.hpp:59
CStrA::operator=
CStrA & operator=(const CStrA &src)
Assignment from another CStrA.
Definition: str.hpp:76
CBlobT::Data
T * Data()
Return pointer to the first data item - non-const version.
Definition: blob.hpp:356