Ripples 1.0
High Performant Software Architecture For Transaction Processing
Loading...
Searching...
No Matches
Utilities

Detailed Description

This is a small collection of general purpose utilities.

Macros

#define DEBUG_TEST   0
 
#define TIME_RFC3339_STRLEN   31
 
#define debug()
 
#define debug_print(str)
 
#define debug_printf(fmt, ...)
 
#define ARRAY_COUNT(a)   sizeof(a) / sizeof(a[0])
 
#define CHECK_MALLOC(a)
 
#define INCREMENT(a)   a += 1
 
#define DECREMENT(a)   a -= 1
 

Functions

int utl_ip_port_from_ss (char *ip, size_t ip_len, uint16_t *port, struct sockaddr_storage *ss)
 
double utl_diff_timespec_as_double (const struct timespec *t1, const struct timespec *t2)
 
void utl_diff_timespec (struct timespec *dst, const struct timespec *t1, const struct timespec *t2)
 
void char_to_lc (char *c)
 
void str_to_lc (uint8_t *str, size_t str_len)
 
bool str_is_numeric (uint8_t *str, size_t str_len)
 
int str_to_unsigned_long (unsigned long *dst, char *str)
 
int str_to_bool (bool *dst, char *str)
 
int parse_csv_to_ul_array (size_t *ul_array, size_t ul_array_len, char *str)
 
int sockaddr_storage_to_string (char *buf, size_t buf_len, struct sockaddr_storage *ss)
 
int utl_readall (int fd, size_t size, void **buf, char *err, size_t err_len)
 
int utl_writeall (int fd, void *buf, size_t buf_len, char *err, size_t err_len)
 
int utl_timespec_to_rfc3339nano (struct timespec *ts, char *buf)
 
void utl_clock_gettime_rt_fatal (struct timespec *tp)
 

Macro Definition Documentation

◆ ARRAY_COUNT

#define ARRAY_COUNT (   a)    sizeof(a) / sizeof(a[0])

Macro to calculate array size.

◆ CHECK_MALLOC

#define CHECK_MALLOC (   a)
Value:
do { \
if((a) == NULL) { \
assert(0); \
} } while (0)

Macro to check result of memory allocation, asserts on memory allocation failure.

◆ debug

#define debug ( )
Value:
do { if (DEBUG_TEST) fprintf(stderr, "%s:%d:%s(): \n", __FILE__, \
__LINE__, __func__); } while (0)
#define DEBUG_TEST
Definition utils.h:55

Macro to print out debug message if DEBUG is set at compile time. This prints out filename, line in file, and function it was called in.

◆ debug_print

#define debug_print (   str)
Value:
do { if (DEBUG_TEST) fprintf(stderr, "%s:%d:%s(): " str "\n", __FILE__, \
__LINE__, __func__); } while (0)

Macro to print out debug message if DEBUG is set at compile time. This prints out everything macro debug does plus a string passed in as argument.

Parameters
strString to print out.

◆ debug_printf

#define debug_printf (   fmt,
  ... 
)
Value:
do { if (DEBUG_TEST) fprintf(stderr, "%s:%d:%s(): " fmt "\n", __FILE__, \
__LINE__, __func__, __VA_ARGS__); } while (0)

Macro to print out debug message if DEBUG is set at compile time. This prints out everything macro debug does plus a string passed in as argument. String can be formatted.

Note
Only use this function to output a formatted string as "fmt" + variable list is REQUIRED. To output string with no format use debug_print macro.
Parameters
fmtString format followed by variable list.

◆ DEBUG_TEST

#define DEBUG_TEST   0

Constant enables printing of debug messages to stderr. Otherwise if not set the compiler optimizes out (removes) all of the debug message code.

◆ DECREMENT

#define DECREMENT (   a)    a -= 1

Macro to decrement a variable by one.

◆ INCREMENT

#define INCREMENT (   a)    a += 1

Macro to increment a variable by one.

◆ TIME_RFC3339_STRLEN

#define TIME_RFC3339_STRLEN   31

Length of buffer needed to store time as rfc399nano formatted string

Function Documentation

◆ char_to_lc()

void char_to_lc ( char *  c)
inline

Convert ascii character to lower case. If character is not alphabet [A-Z] or is already in lower case, this is a no-op.

Parameters
cCharacter to convert to lower case.
Here is the caller graph for this function:

◆ parse_csv_to_ul_array()

int parse_csv_to_ul_array ( size_t *  ul_array,
size_t  ul_array_len,
char *  str 
)

Parse comma separated values into array of unsigend long integers.

strtok() is not used as it skips empty entries (i.e. ',,').

Parameters
ul_arrayArray to parse entries into.
ul_array_lenNumber of slots in array.
strString to parse CSV from.
Returns
0 on success, otherwise an error occurred.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sockaddr_storage_to_string()

int sockaddr_storage_to_string ( char *  buf,
size_t  buf_len,
struct sockaddr_storage *  ss 
)

Convert IPv4 or IPv6 addresses and port from binary to text form as "ip:port".

Parameters
bufBuffer to store string into.
buf_lenLength of buffer, must be large enough to account for IPv6, ":", port (5 characters), plus a '\0' string terminator.
ssstruct_sockaddr to extract IP and port from.
Returns
0 on success, otherwise error occurred. -1 indicates that buf_len was too small, any other value is errno set by inet_ntop().
Here is the call graph for this function:

◆ str_is_numeric()

bool str_is_numeric ( uint8_t *  str,
size_t  str_len 
)

Check if ascii string is solely composed of numeric [0-9] characters.

Parameters
strString to check.
str_lenLength of string.
Returns
Returns tue if ascii string is numeric, otherwise returns false.
Here is the caller graph for this function:

◆ str_to_bool()

int str_to_bool ( bool *  dst,
char *  str 
)

Convert strings "true" and "false" to boolean and store result in dst. Input strings can be a mix of upper and lower case characters.

Parameters
dstDestination to store value to
strString to convert to bool.
Returns
0 on success, otherwise an error occurred, meaning string does not represent "true" or "false".
Here is the call graph for this function:
Here is the caller graph for this function:

◆ str_to_lc()

void str_to_lc ( uint8_t *  str,
size_t  str_len 
)

Convert string ascii characters to lower case.

Parameters
strString to convert to all lower case ASCII.
str_lenLength of string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ str_to_unsigned_long()

int str_to_unsigned_long ( unsigned long *  dst,
char *  str 
)

Convert string to unsigned long and store result in dst. This function only handles base 10 numbers.

Parameters
dstDestination to store value to
strString to convert to unsigned long.
Returns
0 on success, otherwise an error occurred.
Here is the caller graph for this function:

◆ utl_clock_gettime_rt_fatal()

void utl_clock_gettime_rt_fatal ( struct timespec *  tp)

Get current time from clock CLOCK_REALTIME (see man -3 time) and store it in tp.

Note
This function is fatal on error. Error can only occur if there is something majorly wrong with underlying OS. Regardless, not being able to get current time impacts critical application function.
Parameters
tpWhere to store current time.
Here is the caller graph for this function:

◆ utl_diff_timespec()

void utl_diff_timespec ( struct timespec *  dst,
const struct timespec *  t1,
const struct timespec *  t2 
)

Get a difference between two timespec stuctures t1 - t2.

Note
t1 MUST be larger than t2.
Parameters
dstWhere to store result.
t1Time to subtract from.
t2Time to subtract.
Here is the caller graph for this function:

◆ utl_diff_timespec_as_double()

double utl_diff_timespec_as_double ( const struct timespec *  t1,
const struct timespec *  t2 
)

Get a difference between two timespec stuctures t1 - t2 and return the result as type double.

Parameters
t1Time to subtract from.
t2Time to subtract.
Returns
Time difference as double.
Here is the caller graph for this function:

◆ utl_ip_port_from_ss()

int utl_ip_port_from_ss ( char *  ip,
size_t  ip_len,
uint16_t *  port,
struct sockaddr_storage *  ss 
)

From sockaddr_storage extract IP and port into strings.

Parameters
ipBuffer where to store IP address.
ip_lenLength of space available in ip buffer.
portBuffer where to store TCP/UDP port.
sssockaddr_storage to extract IP and port from.
Returns
0 on success, otherwise error occurred.
Here is the caller graph for this function:

◆ utl_readall()

int utl_readall ( int  fd,
size_t  size,
void **  buf,
char *  err,
size_t  err_len 
)

Read size specified from file descriptor into memory.

Note
Per Linux manual read() will return at most 0x7ffff000 (2,147,479,552) bytes, hence a need for this function to keep reading until the full size specified is read into memory buffer.
Parameters
fdOpen file descriptor to read from, seek position MUST be at the begining of the file.
sizeNumber of bytes to read in.
bufPointer where to store buffer data is read into.
errBuffer where to store error string if error occurred. Error string is guaranteed to end with '\0' so i.e. a followup call to strlen() is a valid action.
err_lenSize of err buffer.
Returns
On success returns 0, otherwise error occurred in which case *buf is set to NULL and error message is populated.
Here is the caller graph for this function:

◆ utl_timespec_to_rfc3339nano()

int utl_timespec_to_rfc3339nano ( struct timespec *  ts,
char *  buf 
)

Store time in ts as string in GMT rfc3339nano format.

Parameters
tsTime to store as string.
bufBuffer to store times string at. This buffer MUST have minimum length of TIME_RFC3339_STRLEN.
Here is the caller graph for this function:

◆ utl_writeall()

int utl_writeall ( int  fd,
void *  buf,
size_t  buf_len,
char *  err,
size_t  err_len 
)

Write data from buffer to open file descriptor fd until all data was written.

Parameters
fdOpen file descriptor to write to.
bufBuffer to write data from.
buf_lenLength of data in buff to write.
errBuffer where to store error message if one is encountered.
err_lenSize of err buffer available to store error message.
Returns
On success returns 0. Otherwise error occurred and error message was populated. If error occurred it is possible that only part of the data was written.
Here is the caller graph for this function: