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