Pioneer
utils.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _UTILS_H
5 #define _UTILS_H
6 
7 #if defined(_MSC_VER) && !defined(NOMINMAX)
8 #define NOMINMAX
9 #endif
10 
11 #include "core/Log.h"
12 #include "libs.h"
13 #include <fmt/printf.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <string>
17 #include <vector>
18 
19 #ifndef __GNUC__
20 #define __attribute(x)
21 #endif /* __GNUC__ */
22 
23 // GCC warns when a function marked __attribute((noreturn)) actually returns a value
24 // but other compilers which don't see the noreturn attribute of course require that
25 // a function with a non-void return type should return something.
26 #ifndef __GNUC__
27 #define RETURN_ZERO_NONGNU_ONLY return 0;
28 #else
29 #define RETURN_ZERO_NONGNU_ONLY
30 #endif
31 
32 // align x to a. taken from the Linux kernel
33 #define ALIGN(x, a) __ALIGN_MASK(x, (a - 1))
34 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
35 
36 // void Error(const char *format, ...) __attribute((format(printf, 1, 2))) __attribute((noreturn));
37 // void Warning(const char *format, ...) __attribute((format(printf, 1, 2)));
38 // void Output(const char *format, ...) __attribute((format(printf, 1, 2)));
39 
40 template <typename... Args>
41 inline void Output(const char *message, Args... args)
42 {
43  Log::LogOld(Log::Severity::Info, fmt::sprintf(message, args...));
44 }
45 
46 template <typename... Args>
47 inline void Warning(const char *message, Args... args)
48 {
49  Log::LogOld(Log::Severity::Warning, fmt::sprintf(message, args...));
50 }
51 
52 template <typename... Args>
53 [[noreturn]] inline void Error(const char *message, Args... args)
54 {
55  Log::LogFatalOld(fmt::sprintf(message, args...));
56 }
57 
58 template <typename... Args>
59 inline void DebugMsg(const char *message, Args... args)
60 {
61  Log::LogOld(Log::Severity::Debug, fmt::sprintf(message, args...));
62 }
63 
64 std::string string_join(std::vector<std::string> &v, std::string sep);
65 std::string format_date(double time);
66 std::string format_date_only(double time);
67 std::string format_distance(double dist, int precision = 2);
68 std::string format_money(double cents, bool showCents = true);
69 std::string format_duration(double seconds);
70 
71 static inline Sint64 isqrt(Sint64 a)
72 {
73  // replace with cast from sqrt below which is between x7.3 (win32, Debug) & x15 (x64, Release) times faster
74  return static_cast<int64_t>(sqrt(static_cast<double>(a)));
75 }
76 
77 static inline Sint64 isqrt(fixed v)
78 {
79  Sint64 ret = 0;
80  Sint64 s;
81  Sint64 ret_sq = -v.v - 1;
82  for (s = 62; s >= 0; s -= 2) {
83  Sint64 b;
84  ret += ret;
85  b = ret_sq + ((2 * ret + 1) << s);
86  if (b < 0) {
87  ret_sq = b;
88  ret++;
89  }
90  }
91  return ret;
92 }
93 
94 // find string in bigger string, ignoring case
95 const char *pi_strcasestr(const char *haystack, const char *needle);
96 
97 inline bool starts_with(const char *s, const char *t)
98 {
99  assert(s && t);
100  while ((*s == *t) && *t) {
101  ++s;
102  ++t;
103  }
104  return (*t == '\0');
105 }
106 
107 inline bool starts_with(const std::string &s, const char *t)
108 {
109  assert(t);
110  return starts_with(s.c_str(), t);
111 }
112 
113 inline bool starts_with(const std::string &s, const std::string &t)
114 {
115  return starts_with(s.c_str(), t.c_str());
116 }
117 
118 inline bool ends_with(const char *s, size_t ns, const char *t, size_t nt)
119 {
120  return (ns >= nt) && (memcmp(s + (ns - nt), t, nt) == 0);
121 }
122 
123 inline bool ends_with(const char *s, const char *t)
124 {
125  return ends_with(s, strlen(s), t, strlen(t));
126 }
127 
128 inline bool ends_with(const std::string &s, const char *t)
129 {
130  return ends_with(s.c_str(), s.size(), t, strlen(t));
131 }
132 
133 inline bool ends_with(const std::string &s, const std::string &t)
134 {
135  return ends_with(s.c_str(), s.size(), t.c_str(), t.size());
136 }
137 
138 inline bool ends_with_ci(const char *s, size_t ns, const char *t, size_t nt)
139 {
140  if (ns < nt) return false;
141  s += (ns - nt);
142  for (size_t i = 0; i < nt; i++)
143  if (tolower(*s++) != tolower(*t++)) return false;
144  return true;
145 }
146 
147 inline bool ends_with_ci(const char *s, const char *t)
148 {
149  return ends_with_ci(s, strlen(s), t, strlen(t));
150 }
151 
152 inline bool ends_with_ci(const std::string &s, const char *t)
153 {
154  return ends_with_ci(s.c_str(), s.size(), t, strlen(t));
155 }
156 
157 inline bool ends_with_ci(const std::string &s, const std::string &t)
158 {
159  return ends_with_ci(s.c_str(), s.size(), t.c_str(), t.size());
160 }
161 
162 static inline size_t SplitSpec(const std::string &spec, std::vector<int> &output)
163 {
164  static const std::string delim(",");
165 
166  size_t i = 0, start = 0, end = 0;
167  while (end != std::string::npos) {
168  // get to the first non-delim char
169  start = spec.find_first_not_of(delim, end);
170 
171  // read the end, no more to do
172  if (start == std::string::npos)
173  break;
174 
175  // find the end - next delim or end of string
176  end = spec.find_first_of(delim, start);
177 
178  // extract the fragment and remember it
179  output[i++] = atoi(spec.substr(start, (end == std::string::npos) ? std::string::npos : end - start).c_str());
180  }
181 
182  return i;
183 }
184 
185 static inline size_t SplitSpec(const std::string &spec, std::vector<float> &output)
186 {
187  static const std::string delim(",");
188 
189  size_t i = 0, start = 0, end = 0;
190  while (end != std::string::npos) {
191  // get to the first non-delim char
192  start = spec.find_first_not_of(delim, end);
193 
194  // read the end, no more to do
195  if (start == std::string::npos)
196  break;
197 
198  // find the end - next delim or end of string
199  end = spec.find_first_of(delim, start);
200 
201  // extract the fragment and remember it
202  output[i++] = atof(spec.substr(start, (end == std::string::npos) ? std::string::npos : end - start).c_str());
203  }
204 
205  return i;
206 }
207 
208 std::vector<std::string> SplitString(const std::string &source, const std::string &delim);
209 
210 // 'Numeric type' to string conversions.
211 std::string FloatToStr(float val);
212 std::string DoubleToStr(double val);
213 std::string AutoToStr(Sint32 val);
214 std::string AutoToStr(Sint64 val);
215 std::string AutoToStr(float val);
216 std::string AutoToStr(double val);
217 
218 void Vector3fToStr(const vector3f &val, char *out, size_t size);
219 void Vector3dToStr(const vector3d &val, char *out, size_t size);
220 void Matrix3x3fToStr(const matrix3x3f &val, char *out, size_t size);
221 void Matrix3x3dToStr(const matrix3x3d &val, char *out, size_t size);
222 void Matrix4x4fToStr(const matrix4x4f &val, char *out, size_t size);
223 void Matrix4x4dToStr(const matrix4x4d &val, char *out, size_t size);
224 
225 // String to 'Numeric type' conversions.
226 Sint64 StrToSInt64(const std::string &str);
227 Uint64 StrToUInt64(const std::string &str);
228 float StrToFloat(const std::string &str);
229 double StrToDouble(const std::string &str);
230 void StrToAuto(Sint32 *pVal, const std::string &str);
231 void StrToAuto(Sint64 *pVal, const std::string &str);
232 void StrToAuto(float *pVal, const std::string &str);
233 void StrToAuto(double *pVal, const std::string &str);
234 
235 void StrToVector3f(const char *str, vector3f &val);
236 void StrToVector3d(const char *str, vector3d &val);
237 void StrToMatrix3x3f(const char *str, matrix3x3f &val);
238 void StrToMatrix3x3d(const char *str, matrix3x3d &val);
239 void StrToMatrix4x4f(const char *str, matrix4x4f &val);
240 void StrToMatrix4x4d(const char *str, matrix4x4d &val);
241 
242 // Convert decimal coordinates to degree/minute/second format and return as string
243 std::string DecimalToDegMinSec(float dec);
244 
245 // add a few things that MSVC is missing
246 #if defined(_MSC_VER) && (_MSC_VER < 1800)
247 
248 // round & roundf. taken from http://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/auxiliary/util/u_math.h
249 static inline double round(double x)
250 {
251  return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
252 }
253 
254 static inline float roundf(float x)
255 {
256  return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
257 }
258 #endif /* _MSC_VER < 1800 */
259 
260 static inline Uint32 ceil_pow2(Uint32 v)
261 {
262  v--;
263  v |= v >> 1;
264  v |= v >> 2;
265  v |= v >> 4;
266  v |= v >> 8;
267  v |= v >> 16;
268  v++;
269  return v;
270 }
271 
272 // An adaptor for automagic reverse range-for iteration of containers
273 // One might be able to specialize this for raw arrays, but that's beyond the
274 // point of its use-case.
275 // One might also point out that this is surely more work to code than simply
276 // writing an explicit iterator loop, to which I say: bah humbug!
277 template <typename T>
279  using iterator = typename T::reverse_iterator;
280  using const_iterator = typename T::const_reverse_iterator;
281 
282  using value_type = typename std::remove_reference<T>::type;
283 
285  ref(ref) {}
286 
287  iterator begin() { return ref.rbegin(); }
288  const_iterator begin() const { return ref.crbegin(); }
289 
290  iterator end() { return ref.rend(); }
291  const_iterator end() const { return ref.crend(); }
292 
293 private:
294  value_type &ref;
295 };
296 
297 // Use this function for automatic template parameter deduction
298 template <typename T>
300 
301 void hexdump(const unsigned char *buf, int bufsz);
302 
303 #endif /* _UTILS_H */
Sint64 v
Definition: fixed.h:239
void LogOld(Severity sv, std::string message)
Definition: Log.cpp:144
void LogFatalOld(std::string message)
Definition: Log.cpp:152
Definition: utils.h:278
reverse_container_t(value_type &ref)
Definition: utils.h:284
iterator end()
Definition: utils.h:290
typename T::reverse_iterator iterator
Definition: utils.h:279
const_iterator end() const
Definition: utils.h:291
typename std::remove_reference< T >::type value_type
Definition: utils.h:282
const_iterator begin() const
Definition: utils.h:288
iterator begin()
Definition: utils.h:287
typename T::const_reverse_iterator const_iterator
Definition: utils.h:280
float StrToFloat(const std::string &str)
Definition: utils.cpp:449
bool ends_with(const char *s, size_t ns, const char *t, size_t nt)
Definition: utils.h:118
std::string format_date_only(double time)
Definition: utils.cpp:84
std::vector< std::string > SplitString(const std::string &source, const std::string &delim)
Definition: utils.cpp:191
void Vector3fToStr(const vector3f &val, char *out, size_t size)
Definition: utils.cpp:275
void Vector3dToStr(const vector3d &val, char *out, size_t size)
Definition: utils.cpp:293
void Matrix4x4fToStr(const matrix4x4f &val, char *out, size_t size)
Definition: utils.cpp:359
void Matrix3x3dToStr(const matrix3x3d &val, char *out, size_t size)
Definition: utils.cpp:335
void StrToAuto(Sint32 *pVal, const std::string &str)
Definition: utils.cpp:486
void Output(const char *message, Args... args)
Definition: utils.h:41
void StrToVector3f(const char *str, vector3f &val)
Definition: utils.cpp:506
void Matrix4x4dToStr(const matrix4x4d &val, char *out, size_t size)
Definition: utils.cpp:385
void Matrix3x3fToStr(const matrix3x3f &val, char *out, size_t size)
Definition: utils.cpp:311
Uint64 StrToUInt64(const std::string &str)
Definition: utils.cpp:442
bool starts_with(const char *s, const char *t)
Definition: utils.h:97
reverse_container_t< T > reverse_container(T &ref)
Definition: utils.h:299
std::string format_money(double cents, bool showCents=true)
Definition: utils.cpp:18
std::string FloatToStr(float val)
Definition: utils.cpp:241
void DebugMsg(const char *message, Args... args)
Definition: utils.h:59
void StrToMatrix3x3f(const char *str, matrix3x3f &val)
Definition: utils.cpp:542
std::string string_join(std::vector< std::string > &v, std::string sep)
Definition: utils.cpp:95
void StrToMatrix4x4d(const char *str, matrix4x4d &val)
Definition: utils.cpp:602
void Warning(const char *message, Args... args)
Definition: utils.h:47
void hexdump(const unsigned char *buf, int bufsz)
Definition: utils.cpp:636
std::string format_date(double time)
Definition: utils.cpp:71
std::string format_duration(double seconds)
Definition: utils.cpp:108
void StrToMatrix4x4f(const char *str, matrix4x4f &val)
Definition: utils.cpp:582
std::string AutoToStr(Sint32 val)
Definition: utils.cpp:411
void StrToMatrix3x3d(const char *str, matrix3x3d &val)
Definition: utils.cpp:562
std::string format_distance(double dist, int precision=2)
Definition: utils.cpp:132
const char * pi_strcasestr(const char *haystack, const char *needle)
Definition: utils.cpp:160
bool ends_with_ci(const char *s, size_t ns, const char *t, size_t nt)
Definition: utils.h:138
double StrToDouble(const std::string &str)
Definition: utils.cpp:467
std::string DoubleToStr(double val)
Definition: utils.cpp:258
void Error(const char *message, Args... args)
Definition: utils.h:53
Sint64 StrToSInt64(const std::string &str)
Definition: utils.cpp:435
std::string DecimalToDegMinSec(float dec)
Definition: utils.cpp:626
void StrToVector3d(const char *str, vector3d &val)
Definition: utils.cpp:524