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