nx_metadata_sdk 1.0 Metadata 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
54 NX_KIT_API std::string decodeEscapedString(
55 const std::string& s, std::string* outErrorMessage = nullptr);
56
63 template<typename T>
64 std::string toString(T value);
65
69 template<typename... Args>
70 std::string format(const std::string& formatStr, Args... args)
71 {
72 const int size = snprintf(nullptr, 0, formatStr.c_str(), args...) + /*space for \0*/ 1;
73 if (size <= 0)
74 return formatStr; //< No better way to handle out-of-memory-like errors.
75 std::string result(size, '\0');
76 snprintf(&result[0], size, formatStr.c_str(), args...);
77 result.resize(size - /*terminating \0*/ 1);
78 return result;
79 }
80
81 NX_KIT_API bool fromString(const std::string& s, int* value);
82 NX_KIT_API bool fromString(const std::string& s, double* value);
83 NX_KIT_API bool fromString(const std::string& s, float* value);
84 NX_KIT_API bool fromString(const std::string& s, bool* value);
85
86 NX_KIT_API void stringReplaceAllChars(std::string* s, char sample, char replacement);
87 NX_KIT_API void stringInsertAfterEach(std::string* s, char sample, const char* insertion);
88 NX_KIT_API void stringReplaceAll(
89 std::string* s, const std::string& sample, const std::string& replacement);
90
91 // TODO: Remove when migrating to C++20 - it has std::string::starts_with()/ends_with().
92 NX_KIT_API bool stringStartsWith(const std::string& s, const std::string& prefix);
93 NX_KIT_API bool stringEndsWith(const std::string& s, const std::string& suffix);
94
95 NX_KIT_API std::string trimString(const std::string& s);
96
97 //-------------------------------------------------------------------------------------------------
98 // OS support.
99
104 NX_KIT_API std::string baseName(std::string path);
105
111 NX_KIT_API std::string absolutePath(
112 const std::string& originDir, const std::string& path);
113
117 NX_KIT_API std::string getProcessName();
118
123 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
124
125 NX_KIT_API bool fileExists(const char* filename);
126
127 //-------------------------------------------------------------------------------------------------
128 // Aligned allocation.
129
134 inline size_t alignUp(size_t value, size_t alignment)
135 {
136 if (alignment == 0)
137 return value;
138 const size_t remainder = value % alignment;
139 if (remainder == 0)
140 return value;
141 return value + alignment - remainder;
142 }
143
145 inline uint8_t* misalignedPtr(void* data)
146 {
147 return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
148 }
149
158 template<class MallocFunc>
159 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
160 {
161 if (alignment == 0)
162 return nullptr;
163 const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
164 if (!ptr) //< allocation error
165 return ptr;
166
167 char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
168 const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
169 memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
170 return alignedPtr + misalignment;
171 }
172
174 inline void* mallocAligned(size_t size, size_t alignment)
175 {
176 // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
177 // deprecated exception specification.
178 return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
179 }
180
188 template<class FreeFunc>
189 void freeAligned(void* ptr, FreeFunc freeFunc)
190 {
191 if (!ptr)
192 return freeFunc(ptr);
193
194 ptr = (char*) ptr - sizeof(size_t);
195 size_t misalignment = 0;
196 memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
197 ptr = (char*) ptr - misalignment;
198
199 freeFunc(ptr);
200 }
201
203 inline void freeAligned(void* ptr)
204 {
205 // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
206 // deprecated exception specification.
207 return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
208 }
209
210 //-------------------------------------------------------------------------------------------------
211 // Implementation.
212
213 // The order of overloads below is important - it defines which will be chosen by inline functions.
214 NX_KIT_API std::string toString(bool b);
215 NX_KIT_API std::string toString(const void* ptr);
216 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
217 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
218 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
219 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
220 NX_KIT_API std::string toString(char c);
221 NX_KIT_API std::string toString(const char* s);
222 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
223 NX_KIT_API std::string toString(wchar_t c);
224 NX_KIT_API std::string toString(const wchar_t* w);
225 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
226
227 // std::string can contain '\0' inside, hence a dedicated implementation.
228 NX_KIT_API std::string toString(const std::string& s);
229 NX_KIT_API std::string toString(const std::wstring& w);
230
231
233 template<typename T>
234 std::string toString(T value)
235 {
236 std::ostringstream outputString;
237 outputString << value;
238 return outputString.str();
239 }
240
241 #if defined(QT_CORE_LIB)
242
243 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
244 {
245 return toString(b.toStdString());
246 }
247
248 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
249 {
250 return toString(s.toUtf8().constData());
251 }
252
253 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
254 {
255 return toString(u.toEncoded().toStdString());
256 }
257
258 #endif // defined(QT_CORE_LIB)
259
260 template<typename P>
261 std::string toString(P* ptr)
262 {
263 return toString((const void*) ptr);
264 }
265
266 //-------------------------------------------------------------------------------------------------
267 // Configuration file parsing.
268
269 NX_KIT_API bool parseNameValueFile(
270 const std::string& nameValueFilePath,
271 std::map<std::string, std::string>* nameValueMap,
272 const std::string& errorPrefix,
273 std::ostream* out,
274 bool* isFileEmpty);
275
276 } // namespace utils
277 } // namespace kit
278 } // namespace nx
nx::kit::utils::format
std::string format(const std::string &formatStr, Args... args)
Definition: utils.h:70
nx::kit::utils::alignUp
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:134
nx::kit::utils::mallocAligned
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:159
nx
Definition: apple_utils.h:6
nx::kit::utils::freeAligned
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:189
nx::kit::utils::misalignedPtr
uint8_t * misalignedPtr(void *data)
Definition: utils.h:145