1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4
5 Copyright (c) 2006-2021, assimp team
6
7 All rights reserved.
8
9 Redistribution and use of this software in source and binary forms,
10 with or without modification, are permitted provided that the
11 following conditions are met:
12
13 * Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17 * Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22 * Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 ----------------------------------------------------------------------
40 */
41 #pragma once
42 #ifndef INCLUDED_AI_STRINGUTILS_H
43 #define INCLUDED_AI_STRINGUTILS_H
44
45 #ifdef __GNUC__
46 #pragma GCC system_header
47 #endif
48
49 #include <assimp/defs.h>
50
51 #include <cstdarg>
52 #include <algorithm>
53 #include <cctype>
54 #include <cstdlib>
55 #include <locale>
56 #include <sstream>
57 #include <iomanip>
58
59 #ifdef _MSC_VER
60 #define AI_SIZEFMT "%Iu"
61 #else
62 #define AI_SIZEFMT "%zu"
63 #endif
64
65 // ---------------------------------------------------------------------------------
66 /// @fn ai_snprintf
67 /// @brief The portable version of the function snprintf ( C99 standard ), which
68 /// works on visual studio compilers 2013 and earlier.
69 /// @param outBuf The buffer to write in
70 /// @param size The buffer size
71 /// @param format The format string
72 /// @param ap The additional arguments.
73 /// @return The number of written characters if the buffer size was big enough.
74 /// If an encoding error occurs, a negative number is returned.
75 // ---------------------------------------------------------------------------------
76 #if defined(_MSC_VER) && _MSC_VER < 1900
77
c99_ai_vsnprintf(char * outBuf,size_t size,const char * format,va_list ap)78 inline int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
79 int count(-1);
80 if (0 != size) {
81 count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
82 }
83 if (count == -1) {
84 count = _vscprintf(format, ap);
85 }
86
87 return count;
88 }
89
ai_snprintf(char * outBuf,size_t size,const char * format,...)90 inline int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
91 int count;
92 va_list ap;
93
94 va_start(ap, format);
95 count = c99_ai_vsnprintf(outBuf, size, format, ap);
96 va_end(ap);
97
98 return count;
99 }
100
101 #elif defined(__MINGW32__)
102 #define ai_snprintf __mingw_snprintf
103 #else
104 #define ai_snprintf snprintf
105 #endif
106
107 // ---------------------------------------------------------------------------------
108 /// @fn to_string
109 /// @brief The portable version of to_string ( some gcc-versions on embedded
110 /// devices are not supporting this).
111 /// @param value The value to write into the std::string.
112 /// @return The value as a std::string
113 // ---------------------------------------------------------------------------------
114 template <typename T>
ai_to_string(T value)115 AI_FORCE_INLINE std::string ai_to_string(T value) {
116 std::ostringstream os;
117 os << value;
118
119 return os.str();
120 }
121
122 // ---------------------------------------------------------------------------------
123 /// @fn ai_strtof
124 /// @brief The portable version of strtof.
125 /// @param begin The first character of the string.
126 /// @param end The last character
127 /// @return The float value, 0.0f in case of an error.
128 // ---------------------------------------------------------------------------------
129 AI_FORCE_INLINE
ai_strtof(const char * begin,const char * end)130 float ai_strtof(const char *begin, const char *end) {
131 if (nullptr == begin) {
132 return 0.0f;
133 }
134 float val(0.0f);
135 if (nullptr == end) {
136 val = static_cast<float>(::atof(begin));
137 } else {
138 std::string::size_type len(end - begin);
139 std::string token(begin, len);
140 val = static_cast<float>(::atof(token.c_str()));
141 }
142
143 return val;
144 }
145
146 // ---------------------------------------------------------------------------------
147 /// @fn DecimalToHexa
148 /// @brief The portable to convert a decimal value into a hexadecimal string.
149 /// @param toConvert Value to convert
150 /// @return The hexadecimal string, is empty in case of an error.
151 // ---------------------------------------------------------------------------------
152 template <class T>
ai_decimal_to_hexa(T toConvert)153 AI_FORCE_INLINE std::string ai_decimal_to_hexa(T toConvert) {
154 std::string result;
155 std::stringstream ss;
156 ss << std::hex << toConvert;
157 ss >> result;
158
159 for (size_t i = 0; i < result.size(); ++i) {
160 result[i] = (char)toupper((unsigned char)result[i]);
161 }
162
163 return result;
164 }
165
166 // ---------------------------------------------------------------------------------
167 /// @brief translate RGBA to String
168 /// @param r aiColor.r
169 /// @param g aiColor.g
170 /// @param b aiColor.b
171 /// @param a aiColor.a
172 /// @param with_head #
173 /// @return The hexadecimal string, is empty in case of an error.
174 // ---------------------------------------------------------------------------------
ai_rgba2hex(int r,int g,int b,int a,bool with_head)175 AI_FORCE_INLINE std::string ai_rgba2hex(int r, int g, int b, int a, bool with_head) {
176 std::stringstream ss;
177 if (with_head) {
178 ss << "#";
179 }
180 ss << std::hex << std::setfill('0') << std::setw(8) << (r << 24 | g << 16 | b << 8 | a);
181
182 return ss.str();
183 }
184
185 // ---------------------------------------------------------------------------------
186 /// @brief Performs a trim from start (in place)
187 /// @param s string to trim.
ai_trim_left(std::string & s)188 AI_FORCE_INLINE void ai_trim_left(std::string &s) {
189 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
190 return !std::isspace(ch);
191 }));
192 }
193
194 // ---------------------------------------------------------------------------------
195 /// @brief Performs a trim from end (in place).
196 /// @param s string to trim.
197 // ---------------------------------------------------------------------------------
198 // ---------------------------------------------------------------------------------
ai_trim_right(std::string & s)199 AI_FORCE_INLINE void ai_trim_right(std::string &s) {
200 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
201 return !std::isspace(ch);
202 }).base(), s.end());
203 }
204
205 // ---------------------------------------------------------------------------------
206 /// @brief Performs a trim from both ends (in place).
207 /// @param s string to trim.
208 // ---------------------------------------------------------------------------------
ai_trim(std::string & s)209 AI_FORCE_INLINE std::string ai_trim(std::string &s) {
210 std::string out(s);
211 ai_trim_left(out);
212 ai_trim_right(out);
213
214 return out;
215 }
216
217 // ---------------------------------------------------------------------------------
218 template <class char_t>
ai_tolower(char_t in)219 AI_FORCE_INLINE char_t ai_tolower(char_t in) {
220 return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in + 0x20) : in;
221 }
222
223 // ---------------------------------------------------------------------------------
224 /// @brief Performs a ToLower-operation and return the lower-case string.
225 /// @param in The incoming string.
226 /// @return The string as lowercase.
227 // ---------------------------------------------------------------------------------
ai_tolower(const std::string & in)228 AI_FORCE_INLINE std::string ai_tolower(const std::string &in) {
229 std::string out(in);
230 ai_trim_left(out);
231 ai_trim_right(out);
232 std::transform(out.begin(), out.end(), out.begin(), [](unsigned char c) { return ai_tolower(c); });
233 return out;
234 }
235
236 // ---------------------------------------------------------------------------------
237 template <class char_t>
ai_toupper(char_t in)238 AI_FORCE_INLINE char_t ai_toupper(char_t in) {
239 return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in - 0x20) : in;
240 }
241
242 // ---------------------------------------------------------------------------------
243 /// @brief Performs a ToLower-operation and return the upper-case string.
244 /// @param in The incoming string.
245 /// @return The string as uppercase.
ai_str_toupper(const std::string & in)246 AI_FORCE_INLINE std::string ai_str_toupper(const std::string &in) {
247 std::string out(in);
248 std::transform(out.begin(), out.end(), out.begin(), [](char c) { return ai_toupper(c); });
249 return out;
250 }
251
252 // ---------------------------------------------------------------------------------
253 /// @brief Make a string printable by replacing all non-printable characters with
254 /// the specified placeholder character.
255 /// @param in The incoming string.
256 /// @param placeholder Placeholder character, default is a question mark.
257 /// @return The string, with all non-printable characters replaced.
258 AI_FORCE_INLINE std::string ai_str_toprintable(const std::string &in, char placeholder = '?') {
259 std::string out(in);
260 std::transform(out.begin(), out.end(), out.begin(), [placeholder] (unsigned char c) {
261 return isprint(c) ? (char)c : placeholder;
262 });
263 return out;
264 }
265
266 // ---------------------------------------------------------------------------------
267 /// @brief Make a string printable by replacing all non-printable characters with
268 /// the specified placeholder character.
269 /// @param in The incoming string.
270 /// @param len The length of the incoming string.
271 /// @param placeholder Placeholder character, default is a question mark.
272 /// @return The string, with all non-printable characters replaced. Will return an
273 /// empty string if in is null or len is <= 0.
274 AI_FORCE_INLINE std::string ai_str_toprintable(const char *in, int len, char placeholder = '?') {
275 return (in && len > 0) ? ai_str_toprintable(std::string(in, len), placeholder) : std::string();
276 }
277
278
279 #endif // INCLUDED_AI_STRINGUTILS_H
280