Collection of generic utility functions for mkd64 and modules. More...
Typedefs | |
typedef void(* | FileFoundCallback )(void *caller, const char *filename) |
Callback for the findFilesInDir() function. |
Functions | |
int | randomNum (int min, int max) |
Get random integer in a given range. | |
int | tryParseInt (const char *str, int *result) |
Try parsing an integer from a given string. | |
int | tryParseIntHex (const char *str, unsigned int *result) |
Try parsing an integer from a given hexadecimal string. | |
int | checkArgAndWarn (char opt, const char *arg, int isFileOpt, int argExpected, const char *modid) |
Check for presence of a argument and emit warning message. | |
char * | copyString (const char *s) |
Take a copy of an existing string. | |
int | stringEndsWith (const char *s, const char *expectedEnd, int ignoreCase) |
Check whether a string ends with an expected content. | |
void * | mkd64Alloc (size_t size) |
Allocate memory and fail instantly on error. | |
void | findFilesInDir (const char *dir, const char *pattern, void *caller, FileFoundCallback found) |
Find files matching a pattern in a given directory. |
Collection of generic utility functions for mkd64 and modules.
This file contains utility functions that do not directly belong to one of mkd64's classes.
typedef void(* FileFoundCallback)(void *caller, const char *filename) |
Callback for the findFilesInDir() function.
This is called by findFilesInDir() function for every file found.
caller | the pointer to the caller object, given to findFilesInDir() |
filename | the name of the file found |
int checkArgAndWarn | ( | char | opt, |
const char * | arg, | ||
int | isFileOpt, | ||
int | argExpected, | ||
const char * | modid | ||
) |
Check for presence of a argument and emit warning message.
This is a convenience function that checks whether an option has an argument and directly prints a warning message, if this is not the expected case. For missing arguments, the message will warn that the option will be ignored, for extra arguments, the message will read that the argument will be ignored. This is the recommended behavior for mkd64 modules.
opt | the option |
arg | the actual argument (or, 0) |
isFileOpt | 1 if this is a file option, 0 if it is a global option |
argExpected | 1 if the option needs an argument, 0 if not |
modid | the id string of the module, or 0 if the caller is mkd64 |
char* copyString | ( | const char * | s | ) |
Take a copy of an existing string.
Use this instead of POSIX strdup() for C standard compliance
void findFilesInDir | ( | const char * | dir, |
const char * | pattern, | ||
void * | caller, | ||
FileFoundCallback | found | ||
) |
Find files matching a pattern in a given directory.
This will search a given directory for files matching a pattern non-recursively and call a given callback for each file found. ATTENTION: The exact pattern syntax depends on the platform-specific implementation. An asterisk (*) should normally work as expected.
dir | the directory to search files in |
pattern | the pattern the files should match |
caller | pointer to the calling object |
found | callback to call for files found |
void* mkd64Alloc | ( | size_t | size | ) |
Allocate memory and fail instantly on error.
Memory allocation should only fail in out-of-memory conditions. In this case, the safest thing to do for mkd64 is to fail instantly and exit with an appropriate message.
int randomNum | ( | int | min, |
int | max | ||
) |
Get random integer in a given range.
The random number generator is initialized on the first call
min | minimum number to return |
max | maximum number to return |
int stringEndsWith | ( | const char * | s, |
const char * | expectedEnd, | ||
int | ignoreCase | ||
) |
Check whether a string ends with an expected content.
s | the string to check |
expectedEnd | the content to check for at the end of the string |
ignoreCase | 0 to compare case-sensitive, 1 to ignore case |
int tryParseInt | ( | const char * | str, |
int * | result | ||
) |
Try parsing an integer from a given string.
For tryParseInt, the string must contain a number represented only by decimal digits and an optional minus ('-') at the beginning. Otherwise it will return false and the content of the result is undefined.
str | the string to parse |
result | pointer to an integer for placing the result |
int tryParseIntHex | ( | const char * | str, |
unsigned int * | result | ||
) |
Try parsing an integer from a given hexadecimal string.
For tryParseIntHex, the string must contain a number represented only by hexadecimal digits (0-9, a-z). Otherwise it will return false and the content of the result is undefined.
str | the string to parse |
result | pointer to an unsigned integer for placing the result |