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/>.
10 /** @file str.hpp String formating? */
18 #include "../core/math_func.hpp"
19 #include "../string_func.h"
21 /** Blob based case sensitive ANSI/UTF-8 string */
22 struct CStrA
: public CBlobT
<char>
24 typedef CBlobT
<char> base
; ///< base class
26 /** Create an empty CStrT */
31 /** Copy constructor */
32 inline CStrA(const CStrA
&src
) : base(src
)
37 /** Take over ownership constructor */
38 inline CStrA(const OnTransfer
&ot
)
43 /** Grow the actual buffer and fix the trailing zero at the end. */
44 inline char *GrowSizeNC(uint count
)
46 char *ret
= base::GrowSizeNC(count
);
51 /** Append zero-ended C string. */
52 inline void AppendStr(const char *str
)
55 base::AppendRaw(str
, strlen(str
));
60 /** Append another CStrA. */
61 inline void Append(const CStrA
&src
)
63 if (src
.Length() > 0) {
69 /** Assignment from C string. */
70 inline CStrA
&operator=(const char *src
)
77 /** Assignment from another CStrA. */
78 inline CStrA
&operator=(const CStrA
&src
)
82 base::AppendRaw(src
.Data(), src
.Size());
88 /** Lower-than operator (to support stl collections) */
89 inline bool operator<(const CStrA
&other
) const
91 return strcmp(base::Data(), other
.Data()) < 0;
94 /** Add formated string (like vsprintf) at the end of existing contents. */
95 int AddFormatL(const char *format
, va_list args
)
97 size_t addSize
= max
<size_t>(strlen(format
), 16);
98 addSize
+= addSize
/ 2;
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. */
114 if (err
!= ERANGE
&& err
!= ENOENT
&& err
!= 0) {
115 /* some strange failure */
118 /* small buffer (M$ implementation) */
129 /** Add formated string (like sprintf) at the end of existing contents. */
130 int CDECL
WARN_FORMAT(2, 3) AddFormat(const char *format
, ...)
133 va_start(args
, format
);
134 int ret
= AddFormatL(format
, args
);
139 /** Assign formated string (like sprintf). */
140 int CDECL
WARN_FORMAT(2, 3) Format(const char *format
, ...)
144 va_start(args
, format
);
145 int ret
= AddFormatL(format
, args
);