nx_server_plugin_sdk 1.0 Server Plugin SDK

utils.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
12 #include <cstddef>
13 #include <cstring>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <stdint.h>
17 #include <string>
18 #include <sstream>
19 #include <vector>
20 #include <map>
21
22 #if defined(QT_CORE_LIB)
23 // To be supported in toString().
24 #include <QtCore/QByteArray>
25 #include <QtCore/QString>
26 #include <QtCore/QUrl>
27 #endif
28
29 #if !defined(NX_KIT_API)
30 #define NX_KIT_API /*empty*/
31 #endif
32
33 namespace nx {
34 namespace kit {
35 namespace utils {
36
37 //-------------------------------------------------------------------------------------------------
38 // Strings.
39
40 inline bool isAsciiPrintable(int c)
41 {
42 return c >= 32 && c <= 126;
43 }
44
45 inline bool isSpaceOrControlChar(char c)
46 {
47 // NOTE: Chars 128..255 should be treated as non-whitespace, thus, isprint() will not do.
48 return (((unsigned char) c) <= 32) || (c == 127);
49 }
50
60 NX_KIT_API std::string decodeEscapedString(
61 const std::string& s, std::string* outErrorMessage = nullptr);
62
69 template<typename T>
70 std::string toString(T value);
71
75 template<typename... Args>
76 std::string format(const std::string& formatStr, Args... args)
77 {
78 const int size = snprintf(nullptr, 0, formatStr.c_str(), args...) + /*space for \0*/ 1;
79 if (size <= 0)
80 return formatStr; //< No better way to handle out-of-memory-like errors.
81 std::string result(size, '\0');
82 snprintf(&result[0], size, formatStr.c_str(), args...);
83 result.resize(size - /*terminating \0*/ 1);
84 return result;
85 }
86
87 NX_KIT_API bool fromString(const std::string& s, int* value);
88 NX_KIT_API bool fromString(const std::string& s, double* value);
89 NX_KIT_API bool fromString(const std::string& s, float* value);
90 NX_KIT_API bool fromString(const std::string& s, bool* value);
91
92 NX_KIT_API void stringReplaceAllChars(std::string* s, char sample, char replacement);
93 NX_KIT_API void stringInsertAfterEach(std::string* s, char sample, const char* insertion);
94 NX_KIT_API void stringReplaceAll(
95 std::string* s, const std::string& sample, const std::string& replacement);
96
97 // TODO: Remove when migrating to C++20 - it has std::string::starts_with()/ends_with().
98 NX_KIT_API bool stringStartsWith(const std::string& s, const std::string& prefix);
99 NX_KIT_API bool stringEndsWith(const std::string& s, const std::string& suffix);
100
101 // TODO: Remove when migrating to C++23 - it has std::string::contains().
102 NX_KIT_API bool stringContains(const std::string& s, const std::string& substring);
103
104 NX_KIT_API std::string trimString(const std::string& s);
105
106 //-------------------------------------------------------------------------------------------------
107 // OS support.
108
109 constexpr char kPathSeparator =
110 #if defined(_WIN32)
111 '\\';
112 #else
113 '/';
114 #endif
115
120 NX_KIT_API std::string baseName(std::string path);
121
127 NX_KIT_API std::string absolutePath(
128 const std::string& originDir, const std::string& path);
129
133 NX_KIT_API std::string getProcessName();
134
139 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
140
145 NX_KIT_API std::string getPathToExecutable();
146
147 NX_KIT_API bool fileExists(const char* filename);
148
149 //-------------------------------------------------------------------------------------------------
150 // Aligned allocation.
151
156 inline size_t alignUp(size_t value, size_t alignment)
157 {
158 if (alignment == 0)
159 return value;
160 const size_t remainder = value % alignment;
161 if (remainder == 0)
162 return value;
163 return value + alignment - remainder;
164 }
165
167 inline uint8_t* misalignedPtr(void* data)
168 {
169 return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
170 }
171
180 template<class MallocFunc>
181 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
182 {
183 if (alignment == 0)
184 return nullptr;
185 const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
186 if (!ptr) //< allocation error
187 return ptr;
188
189 char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
190 const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
191 memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
192 return alignedPtr + misalignment;
193 }
194
196 inline void* mallocAligned(size_t size, size_t alignment)
197 {
198 // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
199 // deprecated exception specification.
200 return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
201 }
202
210 template<class FreeFunc>
211 void freeAligned(void* ptr, FreeFunc freeFunc)
212 {
213 if (!ptr)
214 return freeFunc(ptr);
215
216 ptr = (char*) ptr - sizeof(size_t);
217 size_t misalignment = 0;
218 memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
219 ptr = (char*) ptr - misalignment;
220
221 freeFunc(ptr);
222 }
223
225 inline void freeAligned(void* ptr)
226 {
227 // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
228 // deprecated exception specification.
229 return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
230 }
231
232 //-------------------------------------------------------------------------------------------------
233 // Implementation.
234
235 // The order of overloads below is important - it defines which will be chosen by inline functions.
236 NX_KIT_API std::string toString(bool b);
237 NX_KIT_API std::string toString(const void* ptr);
238 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
239 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
240 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
241 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
242 NX_KIT_API std::string toString(char c);
243 NX_KIT_API std::string toString(const char* s);
244 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
245 NX_KIT_API std::string toString(wchar_t c);
246 NX_KIT_API std::string toString(const wchar_t* w);
247 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
248
249 // std::string can contain '\0' inside, hence a dedicated implementation.
250 NX_KIT_API std::string toString(const std::string& s);
251 NX_KIT_API std::string toString(const std::wstring& w);
252
253
255 template<typename T>
256 std::string toString(T value)
257 {
258 std::ostringstream outputString;
259 outputString << value;
260 return outputString.str();
261 }
262
263 #if defined(QT_CORE_LIB)
264
265 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
266 {
267 return toString(b.toStdString());
268 }
269
270 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
271 {
272 return toString(s.toUtf8().constData());
273 }
274
275 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
276 {
277 return toString(u.toEncoded().toStdString());
278 }
279
280 #endif // defined(QT_CORE_LIB)
281
282 template<typename P>
283 std::string toString(P* ptr)
284 {
285 return toString((const void*) ptr);
286 }
287
288 //-------------------------------------------------------------------------------------------------
289 // Configuration file parsing.
290
291 NX_KIT_API bool parseNameValueFile(
292 const std::string& nameValueFilePath,
293 std::map<std::string, std::string>* nameValueMap,
294 const std::string& errorPrefix,
295 std::ostream* out,
296 bool* isFileEmpty);
297
298 //-------------------------------------------------------------------------------------------------
299 // String conversions.
300
302 NX_KIT_API std::string toUpper(const std::string& str);
303
304 #if defined(_WIN32)
305
307 NX_KIT_API std::string wideCharToStdString(const wchar_t* str);
308
309 #endif // defined(_WIN32)
310
311 } // namespace utils
312 } // namespace kit
313 } // namespace nx
nx::kit::utils::format
std::string format(const std::string &formatStr, Args... args)
Definition: utils.h:76
nx::kit::utils::alignUp
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:156
nx::kit::utils::mallocAligned
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:181
nx
Definition: apple_utils.h:6
nx::kit::utils::freeAligned
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:211
utils
Definition: url.cpp:6
nx::kit::utils::misalignedPtr
uint8_t * misalignedPtr(void *data)
Definition: utils.h:167