Mkd64 module API  1.3b
API for creating own mkd64 modules
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
util.h File Reference

Collection of generic utility functions for mkd64 and modules. More...

#include <mkd64/common.h>
#include <stdlib.h>

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.

Detailed Description

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 Documentation

typedef void(* FileFoundCallback)(void *caller, const char *filename)

Callback for the findFilesInDir() function.

This is called by findFilesInDir() function for every file found.

Parameters
callerthe pointer to the caller object, given to findFilesInDir()
filenamethe name of the file found

Function Documentation

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.

Parameters
optthe option
argthe actual argument (or, 0)
isFileOpt1 if this is a file option, 0 if it is a global option
argExpected1 if the option needs an argument, 0 if not
modidthe id string of the module, or 0 if the caller is mkd64
Returns
1 if expectation is met, 0 if not (and warning was printed)
char* copyString ( const char *  s)

Take a copy of an existing string.

Use this instead of POSIX strdup() for C standard compliance

  • s the string to copy
    Returns
    a copy, must be free()d by the caller
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.

Parameters
dirthe directory to search files in
patternthe pattern the files should match
callerpointer to the calling object
foundcallback 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.

  • size the size of the memory block to allocate
    Returns
    the allocated memory
int randomNum ( int  min,
int  max 
)

Get random integer in a given range.

The random number generator is initialized on the first call

Parameters
minminimum number to return
maxmaximum number to return
Returns
random number
int stringEndsWith ( const char *  s,
const char *  expectedEnd,
int  ignoreCase 
)

Check whether a string ends with an expected content.

Parameters
sthe string to check
expectedEndthe content to check for at the end of the string
ignoreCase0 to compare case-sensitive, 1 to ignore case
Returns
1 if the string ends with expectedEnd, 0 otherwise
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.

Parameters
strthe string to parse
resultpointer to an integer for placing the result
Returns
1 (true) if parsed correctly, 0 (false) otherwise
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.

Parameters
strthe string to parse
resultpointer to an unsigned integer for placing the result
Returns
1 (true) if parsed correctly, 0 (false) otherwise