DSL
GenLib.h
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
19 #ifndef _DSL_GENLIB_H_
20 #define _DSL_GENLIB_H_
21 
22 DSL_API_CLASS const char * DSL_CC nopathA(const char * filename);
23 DSL_API_CLASS const wchar_t * DSL_CC nopathW(const wchar_t * filename);
24 DSL_API_CLASS char * DSL_CC nopathA(char * filename);
25 DSL_API_CLASS wchar_t * DSL_CC nopathW(wchar_t * filename);
26 
34 DSL_API_CLASS char * DSL_CC bin2hex(const uint8_t * data, size_t datalen, char * out, size_t outsize);
40 DSL_API_CLASS string DSL_CC bin2hex(const uint8_t * data, size_t datalen);
41 
49 DSL_API_CLASS bool DSL_CC hex2bin(const char * instr, size_t inlen, uint8 * out, size_t outsize);
56 DSL_API_CLASS bool DSL_CC hex2bin(const char * instr, uint8 * out, size_t outsize);
61 DSL_API_CLASS bool DSL_CC hex2bin(const string instr, vector<uint8_t>& out);
62 
63 DSL_API void DSL_CC PrintData(FILE * fp, const uint8 * ptr, size_t len);
64 
65 #if defined(WIN32) || defined(DOXYGEN_SKIP)
66  DSL_API_CLASS const char * DSL_CC stristr(const char * haystack, const char * needle);
67  DSL_API_CLASS char * DSL_CC stristr(char * haystack, const char * needle);
68  DSL_API char * DSL_CC strtok_r(char *newstring, const char *delimiters, char ** save_ptr);
69  time_t filetime_2_time_t(FILETIME ft);
70  DSL_API struct tm * DSL_CC localtime_r(const time_t * tme, struct tm * out);
71  DSL_API struct tm * DSL_CC gmtime_r(const time_t * tme, struct tm * out);
72 #else
73  DSL_API char * DSL_CC strlwr(char * str);
74  DSL_API uint32 DSL_CC GetTickCount();
75  DSL_API uint64 DSL_CC GetTickCount64();
76 #endif
77 
78 DSL_API bool DSL_CC dsl_mkdir_r(const char * p, int mode);
79 DSL_API bool DSL_CC strempty(const char * p);
80 DSL_API int64 DSL_CC dsl_clamp(int64 value, int64 vMin, int64 vMax);
81 
82 DSL_API_CLASS char * DSL_CC escapeshellarg(const char * p, char * out, size_t outSize);
83 DSL_API_CLASS string DSL_CC escapeshellarg(const string& str);
84 
85 #if defined(OPENBSD) || defined(FREEBSD)
86 #define HAVE_STRLCPY
87 #endif
88 
89 #if !defined(HAVE_STRLCPY)
93 DSL_API size_t DSL_CC strlcpy(char * dst, const char * src, size_t siz);
97 DSL_API size_t DSL_CC strlcat(char * dst, const char * src, size_t siz);
98 #endif
102 #define sstrcpy(x,y) strlcpy(x,y,sizeof(x))
106 #define sstrcat(x,y) strlcat(x,y,sizeof(x))
107 
108 
109 #define TRIM_LEFT 0x01
110 #define TRIM_RIGHT 0x02
111 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
120 DSL_API char * DSL_CC strtrim(char *buf, const char * trim = "\r\n\t ", uint8 sides = TRIM_BOTH);
121 
126 DSL_API int DSL_CC wildcmp(const char *wild, const char *string);
131 DSL_API int DSL_CC wildicmp(const char *wild, const char *string);
137 DSL_API int DSL_CC wildicmp_multi(const char *wild, const char *string, const char *sep = "|");
138 
139 DSL_API int64 DSL_CC fseek64(FILE * fp, int64 offset, int whence);
140 DSL_API int64 DSL_CC ftell64(FILE * fp);
141 DSL_API int64 DSL_CC filesize(const char * fn);
142 #if defined(WIN32)
143 DSL_API int DSL_CC truncate(const char * fn, int64 size);
144 #endif
145 
152 DSL_API_CLASS bool DSL_CC file_get_contents(const string& fn, vector<uint8>& data, int64 maxSize=INT32_MAX);
159 DSL_API_CLASS bool DSL_CC file_get_contents(const string& fn, string& data, int64 maxSize=INT32_MAX);
167 DSL_API_CLASS bool DSL_CC file_get_contents(const string& fn, uint8 ** data, int64& fileSize, int64 maxSize=INT32_MAX);
168 DSL_API_CLASS bool DSL_CC file_put_contents(const string& fn, const vector<uint8>& data, bool append = false);
169 DSL_API_CLASS bool DSL_CC file_put_contents(const string& fn, const string& data, bool append = false);
170 DSL_API_CLASS bool DSL_CC file_put_contents(const string& fn, const uint8 * data, size_t fileSize, bool append = false);
176 DSL_API int64 DSL_CC copy_file(const string& src, const string& dest, bool allow_overwrite = false);
177 
178 DSL_API_CLASS int DSL_CC str_replaceA(char *Str, size_t BufSize, const char *FindStr, const char *ReplStr);
179 DSL_API_CLASS int DSL_CC str_replaceW(wchar_t *Str, size_t BufSize, const wchar_t * FindStr, const wchar_t * ReplStr);
180 
181 DSL_API_CLASS string DSL_CC str_replaceA(const string& str, const string& FindStr, const string& ReplStr);
182 DSL_API_CLASS wstring DSL_CC str_replaceW(const wstring& str, const wstring& FindStr, const wstring& ReplStr);
183 
184 DSL_API char * DSL_CC tchar2charA(char * str);
185 DSL_API char * DSL_CC tchar2charW(wchar_t * str);
186 
194 DSL_API_CLASS char * DSL_CC GetUserConfigFolderA(const char * name);
195 DSL_API_CLASS string DSL_CC GetUserConfigFolderA(const string& name);
203 DSL_API_CLASS char * DSL_CC GetUserConfigFileA(const char * name, const char * fn);
204 DSL_API_CLASS string DSL_CC GetUserConfigFileA(const string& name, const string& fn);
205 DSL_API wchar_t * DSL_CC GetUserConfigFolderW(const wchar_t * name);
206 DSL_API wchar_t * DSL_CC GetUserConfigFileW(const wchar_t * name, const wchar_t * fn);
207 
215 DSL_API char * DSL_CC GetUserDocumentsFolderA(const char * name);
216 
217 DSL_API char * DSL_CC tcstombsA(const char * str);
218 DSL_API char * DSL_CC tcstombsW(const wchar_t * str);
219 DSL_API wchar_t * DSL_CC tcstowcsW(const wchar_t * str);
220 DSL_API wchar_t * DSL_CC tcstowcsA(const char * str);
221 DSL_API char * DSL_CC mbstotcsA(const char * str);
222 DSL_API char * DSL_CC wcstotcsA(const wchar_t * str);
223 DSL_API wchar_t * DSL_CC mbstotcsW(const char * str);
224 DSL_API wchar_t * DSL_CC wcstotcsW(const wchar_t * str);
225 DSL_API size_t DSL_CC wcscnt(const wchar_t * ptr); // returns number of bytes taken up by string, not including NULL terminator
226 
227 
228 #ifdef UNICODE
229 #define str_replace str_replaceW
230 #define tchar2char tchar2charW
231 #define tcstombs tcstombsW
232 #define tcstowcs tcstowcsW
233 #define mbstotcs mbstotcsW
234 #define wcstotcs wcstotcsW
235 #define nopath nopathW
236 #define GetUserConfigFolder GetUserConfigFolderW
237 #define GetUserConfigFile GetUserConfigFileW
238 #define GetUserDocumentsFolder #error Not implemented
239 #else
240 #define str_replace str_replaceA
241 #define tchar2char tchar2charA
242 #define tcstombs tcstombsA
243 #define tcstowcs tcstowcsA
244 #define mbstotcs mbstotcsA
245 #define wcstotcs wcstotcsA
246 #define nopath nopathA
247 #define GetUserConfigFolder GetUserConfigFolderA
248 #define GetUserConfigFile GetUserConfigFileA
249 #define GetUserDocumentsFolder GetUserDocumentsFolderA
250 #endif
251 
252 
257 template <typename T>
258 T scale_ranges(T value, T srcmin, T srcmax, T destmin, T destmax) {
259  T in = value - srcmin;
260  T indiff = srcmax - srcmin;
261  T outdiff = destmax - destmin;
262  return ((in * outdiff) / indiff) + destmin;
263 }
264 
269 #if defined(__cplusplus) && __cplusplus < 201703L
270 template <typename T>
271 T clamp(T v, T vMin, T vMax) {
272  if (v < vMin) { return vMin; }
273  if (v > vMax) { return vMax; }
274  return v;
275 }
276 #endif
277 
281 class DSL_API_CLASS StrTokenizer {
282 private:
283  bool b_strdup = false;
284  char * string = NULL;
285  char sep = 0;
286  char sep_str[2] = { 0,0 };
287 
288  size_t num_tokens = 0;
289  char ** tokens = NULL;
290 public:
296  StrTokenizer(char * str, char separater, bool do_strdup=true);
297  ~StrTokenizer();
298 
299  size_t NumTok();
300 
301  char * GetTok(size_t first, size_t last);
302  char * GetSingleTok(size_t num);
303  void FreeString(char * buf);
304 
305  std::string stdGetTok(size_t first, size_t last);
306  std::string stdGetSingleTok(size_t num);
307 };
308 
309 /* Remaining functions past this line were borrowed from PhysicsFS */
310 
311 DSL_API bool DSL_CC IsValidUTF8(const char *s);
312 DSL_API const char * DSL_CC FirstInvalidUTF8(const char *s);
313 
326 DSL_API uint16 DSL_CC ByteSwap16(uint16 X);
327 DSL_API uint32 DSL_CC ByteSwap32(uint32 X);
328 DSL_API uint64 DSL_CC ByteSwap64(uint64 val);
329 
330 DSL_API uint16 DSL_CC Get_ULE16(uint16 x);
331 DSL_API int16 DSL_CC Get_SLE16(int16 x);
332 DSL_API uint32 DSL_CC Get_ULE32(uint32 x);
333 DSL_API int32 DSL_CC Get_SLE32(int32 x);
334 DSL_API uint64 DSL_CC Get_ULE64(uint64 x);
335 DSL_API int64 DSL_CC Get_SLE64(int64 x);
336 
337 DSL_API uint16 DSL_CC Get_UBE16(uint16 x);
338 DSL_API int16 DSL_CC Get_SBE16(int16 x);
339 DSL_API uint32 DSL_CC Get_UBE32(uint32 x);
340 DSL_API int32 DSL_CC Get_SBE32(int32 x);
341 DSL_API uint64 DSL_CC Get_UBE64(uint64 x);
342 DSL_API int64 DSL_CC Get_SBE64(int64 x);
343 
346 #endif // _DSL_GENLIB_H_
DSL_API uint64 DSL_CC Get_ULE64(uint64 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1138
DSL_API int64 DSL_CC Get_SBE64(int64 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1146
DSL_API uint16 DSL_CC ByteSwap16(uint16 X)
Byte swap a 16-bit unsigned integer.
Definition: GenLib.cpp:1101
DSL_API int32 DSL_CC Get_SBE32(int32 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1144
DSL_API int32 DSL_CC Get_SLE32(int32 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1137
DSL_API uint16 DSL_CC Get_UBE16(uint16 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1141
DSL_API uint64 DSL_CC ByteSwap64(uint64 val)
Byte swap a 64-bit unsigned integer.
Definition: GenLib.cpp:1109
DSL_API int16 DSL_CC Get_SBE16(int16 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1142
DSL_API uint32 DSL_CC Get_ULE32(uint32 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1136
DSL_API int16 DSL_CC Get_SLE16(int16 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1135
DSL_API uint32 DSL_CC Get_UBE32(uint32 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1143
DSL_API uint32 DSL_CC ByteSwap32(uint32 X)
Byte swap a 32-bit unsigned integer.
Definition: GenLib.cpp:1105
DSL_API uint64 DSL_CC Get_UBE64(uint64 x)
Convert from the native endianness to Big Endian.
Definition: GenLib.cpp:1145
DSL_API uint16 DSL_CC Get_ULE16(uint16 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1134
DSL_API int64 DSL_CC Get_SLE64(int64 x)
Convert from the native endianness to Little Endian.
Definition: GenLib.cpp:1139
DSL_API char *DSL_CC strlwr(char *str)
Linux version of strlwr.
Definition: GenLib.cpp:371
DSL_API uint64 DSL_CC GetTickCount64()
Linux version of GetTickCount64.
Definition: GenLib.cpp:795
DSL_API bool DSL_CC IsValidUTF8(const char *s)
Checks if the string is valid UTF-8 (no invalid UTF-8 sequences, etc.)
Definition: GenLib.cpp:1149
DSL_API int64 DSL_CC copy_file(const string &src, const string &dest, bool allow_overwrite=false)
Definition: GenLib.cpp:1050
DSL_API size_t DSL_CC strlcpy(char *dst, const char *src, size_t siz)
Definition: GenLib.cpp:905
DSL_API_CLASS char *DSL_CC bin2hex(const uint8_t *data, size_t datalen, char *out, size_t outsize)
Definition: GenLib.cpp:129
DSL_API_CLASS bool DSL_CC hex2bin(const char *instr, size_t inlen, uint8 *out, size_t outsize)
Definition: GenLib.cpp:184
DSL_API bool DSL_CC dsl_mkdir_r(const char *p, int mode)
Cross-platform recursive mkdir()
Definition: GenLib.cpp:380
DSL_API wchar_t *DSL_CC GetUserConfigFolderW(const wchar_t *name)
Unicode version of...
Definition: GenLib.cpp:716
DSL_API bool DSL_CC strempty(const char *p)
Returns true if a string is empty (p == NULL || *p == 0)
Definition: GenLib.cpp:424
DSL_API_CLASS int DSL_CC str_replaceA(char *Str, size_t BufSize, const char *FindStr, const char *ReplStr)
Simple string replacement.
Definition: GenLib.cpp:592
DSL_API void DSL_CC PrintData(FILE *fp, const uint8 *ptr, size_t len)
Prints binary data in a pretty format.
Definition: GenLib.cpp:216
DSL_API_CLASS const char *DSL_CC nopathA(const char *filename)
Returns just the file portion of the full path and filename.
Definition: GenLib.cpp:246
T clamp(T v, T vMin, T vMax)
Definition: GenLib.h:271
DSL_API int64 DSL_CC ftell64(FILE *fp)
Cross-platform 64-bit ftell.
Definition: GenLib.cpp:527
DSL_API_CLASS char *DSL_CC GetUserConfigFileA(const char *name, const char *fn)
Definition: GenLib.cpp:702
DSL_API_CLASS char *DSL_CC GetUserConfigFolderA(const char *name)
Definition: GenLib.cpp:656
T scale_ranges(T value, T srcmin, T srcmax, T destmin, T destmax)
Definition: GenLib.h:258
DSL_API size_t DSL_CC strlcat(char *dst, const char *src, size_t siz)
Definition: GenLib.cpp:879
DSL_API_CLASS const wchar_t *DSL_CC nopathW(const wchar_t *filename)
Returns just the file portion of the full path and filename.
Definition: GenLib.cpp:270
DSL_API int DSL_CC wildicmp(const char *wild, const char *string)
Definition: GenLib.cpp:469
DSL_API int DSL_CC wildcmp(const char *wild, const char *string)
Definition: GenLib.cpp:433
DSL_API char *DSL_CC GetUserDocumentsFolderA(const char *name)
Definition: GenLib.cpp:756
DSL_API int64 DSL_CC filesize(const char *fn)
Get the size in bytes of a file.
Definition: GenLib.cpp:535
DSL_API int64 DSL_CC fseek64(FILE *fp, int64 offset, int whence)
Cross-platform 64-bit fseek.
Definition: GenLib.cpp:519
DSL_API char *DSL_CC strtrim(char *buf, const char *trim="\r\n\t ", uint8 sides=TRIM_BOTH)
Definition: GenLib.cpp:294
DSL_API_CLASS int DSL_CC str_replaceW(wchar_t *Str, size_t BufSize, const wchar_t *FindStr, const wchar_t *ReplStr)
Simple string replacement.
DSL_API int DSL_CC wildicmp_multi(const char *wild, const char *string, const char *sep="|")
Definition: GenLib.cpp:505
DSL_API wchar_t *DSL_CC GetUserConfigFileW(const wchar_t *name, const wchar_t *fn)
Unicode version of...
Definition: GenLib.cpp:749
DSL_API_CLASS char *DSL_CC escapeshellarg(const char *p, char *out, size_t outSize)
Escapes an argument for passing to shell functions, only escapes quotes atm so keep that in mind.
Definition: GenLib.cpp:555
DSL_API_CLASS bool DSL_CC file_get_contents(const string &fn, vector< uint8 > &data, int64 maxSize=INT32_MAX)
Definition: GenLib.cpp:972
DSL_API uint32 DSL_CC GetTickCount()
Linux version of GetTickCount.
Definition: GenLib.cpp:791
DSL_API int64 DSL_CC dsl_clamp(int64 value, int64 vMin, int64 vMax)
Clamps a given value to the range of vMin to vMax (inclusive)
Definition: GenLib.cpp:429