My library
Library for common tasks
Functions
utility.h File Reference

Common tasks such as comparing variables, allocate memory. More...

#include "types.h"
Include dependency graph for utility.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int chooseCmp (const spec_t spec, const void *a, const void *b)
 Compare two values. More...
 
int charCmp (const void *a, const void *b)
 Compare two chars. More...
 
int byteCmp (const void *a, const void *b)
 Compare two bytes. More...
 
int intCmp (const void *a, const void *b)
 Compare two ints. More...
 
int floatCmp (const void *a, const void *b)
 Compare two floats. More...
 
int doubleCmp (const void *a, const void *b)
 Compare two doubles. More...
 
int ptrCmp (const void *a, const void *b)
 Compare two pointers. More...
 
void * saferMalloc (unsigned int bytes)
 Return a pointer to a space in memory of specified size. More...
 
void * saferRealloc (void *pointer, unsigned int bytes)
 Reallocate a space in memory. More...
 

Detailed Description

Common tasks such as comparing variables, allocate memory.

Author
Pietro Firpo (pietr.nosp@m.o.fi.nosp@m.rpo@p.nosp@m.m.me)

Function Documentation

◆ byteCmp()

int byteCmp ( const void *  a,
const void *  b 
)

Compare two bytes.

Equivalent to charCmp(a, b). Refer to charCmp().

◆ charCmp()

int charCmp ( const void *  a,
const void *  b 
)

Compare two chars.

Equivalent to chooseCmp("%c", a, b). Refer to chooseCmp()

◆ chooseCmp()

int chooseCmp ( const spec_t  spec,
const void *  a,
const void *  b 
)

Compare two values.

Parameters
specType specifier of the values to be compared. Refer to spec_t for supported types.
aPointer to the first element to be compared
bPointer to the second element to be compared
Returns
Constant for the corresponding comparation result
Return values
GREATERFirst element is grater than the second
EQUALFirst element is equal to the second
SMALLERFirst element is smaller than the second

◆ doubleCmp()

int doubleCmp ( const void *  a,
const void *  b 
)

Compare two doubles.

Equivalent to chooseCmp("%lf", a, b). Refer to chooseCmp()

◆ floatCmp()

int floatCmp ( const void *  a,
const void *  b 
)

Compare two floats.

Equivalent to chooseCmp("%f", a, b). Refer to chooseCmp()

◆ intCmp()

int intCmp ( const void *  a,
const void *  b 
)

Compare two ints.

Equivalent to chooseCmp("%i", a, b). Refer to chooseCmp()

◆ ptrCmp()

int ptrCmp ( const void *  a,
const void *  b 
)

Compare two pointers.

Equivalent to chooseCmp("%p", a, b). Refer to chooseCmp()

◆ saferMalloc()

void * saferMalloc ( unsigned int  bytes)

Return a pointer to a space in memory of specified size.

Calls malloc(bytes) for a maximum of 10 times until it returns a not null pointer. If in 10 calls does not manage to obtain a not null pointer makes the program terminate

Parameters
bytesNumber of bytes to allocate
Returns
A pointer to the allocated memory

◆ saferRealloc()

void * saferRealloc ( void *  pointer,
unsigned int  bytes 
)

Reallocate a space in memory.

Calls realloc(pointer, bytes) for a maximum of 10 times until it returns a not null pointer. If in 10 calls does not manage to obtain a not null pointer makes the program terminate

Parameters
pointerPointer to the memory to be reallocated
bytesNumber of bytes to allocate
Returns
A pointer to the allocated memory