DSL
Classes | Macros | Functions
General Functions Library (GenLib)

Classes

class  StrTokenizer
 

Macros

#define sstrcpy(x, y)   strlcpy(x,y,sizeof(x))
 
#define sstrcat(x, y)   strlcat(x,y,sizeof(x))
 
#define TRIM_LEFT   0x01
 
#define TRIM_RIGHT   0x02
 
#define TRIM_BOTH   (TRIM_LEFT|TRIM_RIGHT)
 
#define str_replace   str_replaceA
 
#define tchar2char   tchar2charA
 
#define tcstombs   tcstombsA
 
#define tcstowcs   tcstowcsA
 
#define mbstotcs   mbstotcsA
 
#define wcstotcs   wcstotcsA
 
#define nopath   nopathA
 
#define GetUserConfigFolder   GetUserConfigFolderA
 
#define GetUserConfigFile   GetUserConfigFileA
 
#define GetUserDocumentsFolder   GetUserDocumentsFolderA
 

Functions

DSL_API_CLASS const char *DSL_CC nopathA (const char *filename)
 Returns just the file portion of the full path and filename.
 
DSL_API_CLASS const wchar_t *DSL_CC nopathW (const wchar_t *filename)
 Returns just the file portion of the full path and filename.
 
DSL_API_CLASS char *DSL_CC nopathA (char *filename)
 Returns just the file portion of the full path and filename.
 
DSL_API_CLASS wchar_t *DSL_CC nopathW (wchar_t *filename)
 Returns just the file portion of the full path and filename.
 
DSL_API_CLASS char *DSL_CC bin2hex (const uint8_t *data, size_t datalen, char *out, size_t outsize)
 
DSL_API_CLASS string DSL_CC bin2hex (const uint8_t *data, size_t datalen)
 
DSL_API_CLASS bool DSL_CC hex2bin (const char *instr, size_t inlen, uint8 *out, size_t outsize)
 
DSL_API_CLASS bool DSL_CC hex2bin (const char *instr, uint8 *out, size_t outsize)
 
DSL_API_CLASS bool DSL_CC hex2bin (const string instr, vector< uint8_t > &out)
 
DSL_API void DSL_CC PrintData (FILE *fp, const uint8 *ptr, size_t len)
 Prints binary data in a pretty format.
 
DSL_API char *DSL_CC strlwr (char *str)
 Linux version of strlwr.
 
DSL_API uint32 DSL_CC GetTickCount ()
 Linux version of GetTickCount.
 
DSL_API uint64 DSL_CC GetTickCount64 ()
 Linux version of GetTickCount64.
 
DSL_API bool DSL_CC dsl_mkdir_r (const char *p, int mode)
 Cross-platform recursive mkdir()
 
DSL_API bool DSL_CC strempty (const char *p)
 Returns true if a string is empty (p == NULL || *p == 0)
 
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)
 
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.
 
DSL_API_CLASS string DSL_CC escapeshellarg (const string &str)
 Escapes an argument for passing to shell functions, only escapes quotes atm so keep that in mind.
 
DSL_API size_t DSL_CC strlcpy (char *dst, const char *src, size_t siz)
 
DSL_API size_t DSL_CC strlcat (char *dst, const char *src, size_t siz)
 
DSL_API char *DSL_CC strtrim (char *buf, const char *trim="\r\n\t ", uint8 sides=TRIM_BOTH)
 
DSL_API int DSL_CC wildcmp (const char *wild, const char *string)
 
DSL_API int DSL_CC wildicmp (const char *wild, const char *string)
 
DSL_API int DSL_CC wildicmp_multi (const char *wild, const char *string, const char *sep="|")
 
DSL_API int64 DSL_CC fseek64 (FILE *fp, int64 offset, int whence)
 Cross-platform 64-bit fseek.
 
DSL_API int64 DSL_CC ftell64 (FILE *fp)
 Cross-platform 64-bit ftell.
 
DSL_API int64 DSL_CC filesize (const char *fn)
 Get the size in bytes of a file.
 
DSL_API_CLASS bool DSL_CC file_get_contents (const string &fn, vector< uint8 > &data, int64 maxSize=INT32_MAX)
 
DSL_API_CLASS bool DSL_CC file_get_contents (const string &fn, string &data, int64 maxSize=INT32_MAX)
 
DSL_API_CLASS bool DSL_CC file_get_contents (const string &fn, uint8 **data, int64 &fileSize, int64 maxSize=INT32_MAX)
 free data with dsl_free() More...
 
DSL_API_CLASS bool DSL_CC file_put_contents (const string &fn, const vector< uint8 > &data, bool append=false)
 
DSL_API_CLASS bool DSL_CC file_put_contents (const string &fn, const string &data, bool append=false)
 
DSL_API_CLASS bool DSL_CC file_put_contents (const string &fn, const uint8 *data, size_t fileSize, bool append=false)
 
DSL_API int64 DSL_CC copy_file (const string &src, const string &dest, bool allow_overwrite=false)
 
DSL_API_CLASS int DSL_CC str_replaceA (char *Str, size_t BufSize, const char *FindStr, const char *ReplStr)
 Simple string replacement.
 
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_CLASS string DSL_CC str_replaceA (const string &str, const string &FindStr, const string &ReplStr)
 Simple string replacement.
 
DSL_API_CLASS wstring DSL_CC str_replaceW (const wstring &str, const wstring &FindStr, const wstring &ReplStr)
 Simple string replacement.
 
DSL_API char *DSL_CC tchar2charA (char *str)
 
DSL_API char *DSL_CC tchar2charW (wchar_t *str)
 
DSL_API_CLASS char *DSL_CC GetUserConfigFolderA (const char *name)
 
DSL_API_CLASS string DSL_CC GetUserConfigFolderA (const string &name)
 
DSL_API_CLASS char *DSL_CC GetUserConfigFileA (const char *name, const char *fn)
 
DSL_API_CLASS string DSL_CC GetUserConfigFileA (const string &name, const string &fn)
 
DSL_API wchar_t *DSL_CC GetUserConfigFolderW (const wchar_t *name)
 Unicode version of... More...
 
DSL_API wchar_t *DSL_CC GetUserConfigFileW (const wchar_t *name, const wchar_t *fn)
 Unicode version of... More...
 
DSL_API char *DSL_CC GetUserDocumentsFolderA (const char *name)
 
DSL_API char *DSL_CC tcstombsA (const char *str)
 
DSL_API char *DSL_CC tcstombsW (const wchar_t *str)
 
DSL_API wchar_t *DSL_CC tcstowcsW (const wchar_t *str)
 
DSL_API wchar_t *DSL_CC tcstowcsA (const char *str)
 
DSL_API char *DSL_CC mbstotcsA (const char *str)
 
DSL_API char *DSL_CC wcstotcsA (const wchar_t *str)
 
DSL_API wchar_t *DSL_CC mbstotcsW (const char *str)
 
DSL_API wchar_t *DSL_CC wcstotcsW (const wchar_t *str)
 
DSL_API size_t DSL_CC wcscnt (const wchar_t *ptr)
 
template<typename T >
scale_ranges (T value, T srcmin, T srcmax, T destmin, T destmax)
 
template<typename T >
clamp (T v, T vMin, T vMax)
 
DSL_API bool DSL_CC IsValidUTF8 (const char *s)
 Checks if the string is valid UTF-8 (no invalid UTF-8 sequences, etc.)
 
DSL_API const char *DSL_CC FirstInvalidUTF8 (const char *s)
 

Detailed Description

Macro Definition Documentation

◆ sstrcat

#define sstrcat (   x,
 
)    strlcat(x,y,sizeof(x))

Shorthand strlcat so you don't have to use sizeof() on your char array.

Definition at line 106 of file GenLib.h.

◆ sstrcpy

#define sstrcpy (   x,
 
)    strlcpy(x,y,sizeof(x))

Shorthand strlcpy so you don't have to use sizeof() on your char array.

Definition at line 102 of file GenLib.h.

Function Documentation

◆ bin2hex() [1/2]

DSL_API_CLASS string DSL_CC bin2hex ( const uint8_t *  data,
size_t  datalen 
)

This function takes binary data and converts it to a hex string.

Parameters
dataa pointer to the binary data
datalenthe length of the binary data

Definition at line 162 of file GenLib.cpp.

◆ bin2hex() [2/2]

DSL_API_CLASS char* DSL_CC bin2hex ( const uint8_t *  data,
size_t  datalen,
char *  out,
size_t  outsize 
)

This function takes binary data and converts it to a hex string.

Parameters
dataa pointer to the binary data
datalenthe length of the binary data
outpointer to a char buffer to put the hex string in.
outsizeThe size of the buffer pointed to by out. It should have a minimum size of (datalen*2)+1

Definition at line 129 of file GenLib.cpp.

◆ clamp()

template<typename T >
T clamp ( v,
vMin,
vMax 
)

Clamps a given value to the range of vMin to vMax (inclusive)
Example: clamp<uint16>(10, 0, 15) to clamp a value of 10 to a range of 0-15.

Definition at line 271 of file GenLib.h.

◆ copy_file()

DSL_API int64 DSL_CC copy_file ( const string &  src,
const string &  dest,
bool  allow_overwrite = false 
)

Copy a file from src to dest

Parameters
allow_overwriteDon't overwrite dest if it exists
Returns
The number of bytes copied (which can be 0 for a 0 length file), -1 on error.

Definition at line 1050 of file GenLib.cpp.

References filesize().

◆ file_get_contents() [1/3]

DSL_API_CLASS bool DSL_CC file_get_contents ( const string &  fn,
string &  data,
int64  maxSize = INT32_MAX 
)

Read a file into memory

Parameters
fnThe filename to read
dataThe buffer to read in to
maxSizeThe maximum size of file to read, if the file is larger than this we will return false and read nothing

Definition at line 985 of file GenLib.cpp.

◆ file_get_contents() [2/3]

DSL_API_CLASS bool DSL_CC file_get_contents ( const string &  fn,
uint8 **  data,
int64 &  fileSize,
int64  maxSize = INT32_MAX 
)

free data with dsl_free()

Read a file into memory

Parameters
fnThe filename to read
dataPointer to a pointer for a uint8 buffer we will allocate if the function returns true. Free with @dsl_free
fileSizeReference to an int64 to receive the size of the data read
maxSizeThe maximum size of file to read, if the file is larger than this we will return false and read nothing

Definition at line 998 of file GenLib.cpp.

◆ file_get_contents() [3/3]

DSL_API_CLASS bool DSL_CC file_get_contents ( const string &  fn,
vector< uint8 > &  data,
int64  maxSize = INT32_MAX 
)

Read a file into memory

Parameters
fnThe filename to read
dataThe buffer to read in to
maxSizeThe maximum size of file to read, if the file is larger than this we will return false and read nothing

Definition at line 972 of file GenLib.cpp.

◆ GetUserConfigFileA()

DSL_API_CLASS char* DSL_CC GetUserConfigFileA ( const char *  name,
const char *  fn 
)

Gets an appropriate filename to store config files/data for the current user. The directory will be created for you.
Windows: APPDATA%\name\fn
Linux: /home/username/.name/fn

Returns
The path of the filename. Free with dsl_free
See also
dsl_free

Definition at line 702 of file GenLib.cpp.

References dsl_free(), dsl_mprintf(), and GetUserConfigFolderA().

◆ GetUserConfigFileW()

DSL_API wchar_t* DSL_CC GetUserConfigFileW ( const wchar_t *  name,
const wchar_t *  fn 
)

Unicode version of...

See also
GetUserConfigFileW

Definition at line 749 of file GenLib.cpp.

References dsl_free(), dsl_wmprintf(), and GetUserConfigFolderW().

◆ GetUserConfigFolderA()

DSL_API_CLASS char* DSL_CC GetUserConfigFolderA ( const char *  name)

Gets an appropriate directory to store config files/data for the current user. The directory will be created for you.
Windows: APPDATA%\name<br> Linux: /home/username/.name/

Returns
The path including trailing path separater. Free with dsl_free
See also
dsl_free

Definition at line 656 of file GenLib.cpp.

References sstrcpy.

Referenced by GetUserConfigFileA().

◆ GetUserConfigFolderW()

DSL_API wchar_t* DSL_CC GetUserConfigFolderW ( const wchar_t *  name)

Unicode version of...

See also
GetUserConfigFolderA

Definition at line 716 of file GenLib.cpp.

Referenced by GetUserConfigFileW().

◆ GetUserDocumentsFolderA()

DSL_API char* DSL_CC GetUserDocumentsFolderA ( const char *  name)

Gets an appropriate directory to store user documents for the current user. The directory will be created for you.
Windows: USERPROFILE%\Documents\name
Linux: /home/username/name/

Returns
The path including trailing path separater. Free with dsl_free
See also
dsl_free

Definition at line 756 of file GenLib.cpp.

References strlcpy().

◆ hex2bin() [1/3]

DSL_API_CLASS bool DSL_CC hex2bin ( const char *  instr,
size_t  inlen,
uint8 *  out,
size_t  outsize 
)

This function takes a hex string and converts it to binary data.

Parameters
instra pointer to the hex-encoded data (should be a multiple of 2)
inlenlength of string instr
outsizethe length of the output buffer, should be (inlen/2)
outpointer to a buffer to put the binary data in string in.

Definition at line 184 of file GenLib.cpp.

Referenced by hex2bin().

◆ hex2bin() [2/3]

DSL_API_CLASS bool DSL_CC hex2bin ( const char *  instr,
uint8 *  out,
size_t  outsize 
)

This function takes a hex string and converts it to binary data.

Parameters
instra pointer to the hex-encoded data (should be a multiple of 2). Length is determined by doing a strlen() on the string.
outsizethe length of the output buffer, should be (inlen/2)
outpointer to a buffer to put the binary data in string in.

Definition at line 202 of file GenLib.cpp.

References hex2bin().

◆ hex2bin() [3/3]

DSL_API_CLASS bool DSL_CC hex2bin ( const string  instr,
vector< uint8_t > &  out 
)

This function takes a hex string and converts it to binary data, putting it into a vector.

Parameters
instra pointer to the hex-encoded data (should be a multiple of 2).

Definition at line 207 of file GenLib.cpp.

References hex2bin().

◆ scale_ranges()

template<typename T >
T scale_ranges ( value,
srcmin,
srcmax,
destmin,
destmax 
)

Cross multiplication Example: scale_ranges<uint16>(10, 0, 100, 0, 200) to adjust 10 from a range of 0-100 to a range of 0-200 which would be 20.

Definition at line 258 of file GenLib.h.

◆ strlcat()

DSL_API size_t DSL_CC strlcat ( char *  dst,
const char *  src,
size_t  siz 
)

Like strncat but always null terminates and doesn't fill the whole buffer with zeros needlessly. See https://linux.die.net/man/3/strlcpy for details

Definition at line 879 of file GenLib.cpp.

Referenced by escapeshellarg().

◆ strlcpy()

DSL_API size_t DSL_CC strlcpy ( char *  dst,
const char *  src,
size_t  siz 
)

Like strncpy but always null terminates and doesn't fill the whole buffer with zeros needlessly. See https://linux.die.net/man/3/strlcpy for details

Definition at line 905 of file GenLib.cpp.

Referenced by DSL_Sockets3_Base::BindToAddr(), escapeshellarg(), GetUserDocumentsFolderA(), and Directory::Read().

◆ strtrim()

DSL_API char* DSL_CC strtrim ( char *  buf,
const char *  trim = "\r\n\t ",
uint8  sides = TRIM_BOTH 
)

Trims unwanted characters from the beginning and/or end of a string.

Parameters
trimThe characters to trim. Default: CR, LF, tabs, spaces.
sidesThe sides to trim. Default: Both
See also
TRIM_BOTH
TRIM_LEFT
TRIM_RIGHT

Definition at line 294 of file GenLib.cpp.

◆ wildcmp()

DSL_API int DSL_CC wildcmp ( const char *  wild,
const char *  string 
)

Case-sensitive wildcard string comparison.

Returns
0 for no match, other for match

Definition at line 433 of file GenLib.cpp.

◆ wildicmp()

DSL_API int DSL_CC wildicmp ( const char *  wild,
const char *  string 
)

Case-insensitive wildcard string comparison.

Returns
0 for no match, other for match

Definition at line 469 of file GenLib.cpp.

◆ wildicmp_multi()

DSL_API int DSL_CC wildicmp_multi ( const char *  wild,
const char *  string,
const char *  sep = "|" 
)

Case-insensitive wildcard string comparison with multiple patterns.

Parameters
sepThe characters the patterns are delimited with (ala strtok). Default: |
Returns
0 for no match, other for match

Definition at line 505 of file GenLib.cpp.