nx_video_source_sdk  1.0
Video Source 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 
100 constexpr char kPathSeparator =
101  #if defined(_WIN32)
102  '\\';
103  #else
104  '/';
105  #endif
106 
111 NX_KIT_API std::string baseName(std::string path);
112 
118 NX_KIT_API std::string absolutePath(
119  const std::string& originDir, const std::string& path);
120 
124 NX_KIT_API std::string getProcessName();
125 
130 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
131 
132 NX_KIT_API bool fileExists(const char* filename);
133 
134 //-------------------------------------------------------------------------------------------------
135 // Aligned allocation.
136 
141 inline size_t alignUp(size_t value, size_t alignment)
142 {
143  if (alignment == 0)
144  return value;
145  const size_t remainder = value % alignment;
146  if (remainder == 0)
147  return value;
148  return value + alignment - remainder;
149 }
150 
152 inline uint8_t* misalignedPtr(void* data)
153 {
154  return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
155 }
156 
165 template<class MallocFunc>
166 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
167 {
168  if (alignment == 0)
169  return nullptr;
170  const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
171  if (!ptr) //< allocation error
172  return ptr;
173 
174  char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
175  const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
176  memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
177  return alignedPtr + misalignment;
178 }
179 
181 inline void* mallocAligned(size_t size, size_t alignment)
182 {
183  // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
184  // deprecated exception specification.
185  return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
186 }
187 
195 template<class FreeFunc>
196 void freeAligned(void* ptr, FreeFunc freeFunc)
197 {
198  if (!ptr)
199  return freeFunc(ptr);
200 
201  ptr = (char*) ptr - sizeof(size_t);
202  size_t misalignment = 0;
203  memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
204  ptr = (char*) ptr - misalignment;
205 
206  freeFunc(ptr);
207 }
208 
210 inline void freeAligned(void* ptr)
211 {
212  // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
213  // deprecated exception specification.
214  return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
215 }
216 
217 //-------------------------------------------------------------------------------------------------
218 // Implementation.
219 
220 // The order of overloads below is important - it defines which will be chosen by inline functions.
221 NX_KIT_API std::string toString(bool b);
222 NX_KIT_API std::string toString(const void* ptr);
223 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
224 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
225 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
226 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
227 NX_KIT_API std::string toString(char c);
228 NX_KIT_API std::string toString(const char* s);
229 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
230 NX_KIT_API std::string toString(wchar_t c);
231 NX_KIT_API std::string toString(const wchar_t* w);
232 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
233 
234 // std::string can contain '\0' inside, hence a dedicated implementation.
235 NX_KIT_API std::string toString(const std::string& s);
236 NX_KIT_API std::string toString(const std::wstring& w);
237 
238 
240 template<typename T>
241 std::string toString(T value)
242 {
243  std::ostringstream outputString;
244  outputString << value;
245  return outputString.str();
246 }
247 
248 #if defined(QT_CORE_LIB)
249 
250 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
251 {
252  return toString(b.toStdString());
253 }
254 
255 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
256 {
257  return toString(s.toUtf8().constData());
258 }
259 
260 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
261 {
262  return toString(u.toEncoded().toStdString());
263 }
264 
265 #endif // defined(QT_CORE_LIB)
266 
267 template<typename P>
268 std::string toString(P* ptr)
269 {
270  return toString((const void*) ptr);
271 }
272 
273 //-------------------------------------------------------------------------------------------------
274 // Configuration file parsing.
275 
276 NX_KIT_API bool parseNameValueFile(
277  const std::string& nameValueFilePath,
278  std::map<std::string, std::string>* nameValueMap,
279  const std::string& errorPrefix,
280  std::ostream* out,
281  bool* isFileEmpty);
282 
284 NX_KIT_API std::string toUpper(const std::string& str);
285 
286 } // namespace utils
287 } // namespace kit
288 } // namespace nx
std::string format(const std::string &formatStr, Args... args)
Definition: utils.h:70
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:141
uint8_t * misalignedPtr(void *data)
Definition: utils.h:152
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:166
Definition: apple_utils.h:6
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:196