Robotran C Documentation
Macros | Functions
useful_functions.c File Reference
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "useful_functions.h"
#include "mbs_define.h"
#include "mbs_message.h"
#include "mbs_errors_names.h"

Macros

#define MSG_ERR   ">> USEFUL>> "
 
#define MSG_PRE   " >> "
 

Functions

void * mbs_free (void *__ptr)
 Free the pointor ptr and set it to NULL. More...
 
void mbs_log (char *msg,...)
 Function for logging messages in a file called "log" where the program is executed. More...
 
double pow_int (double basis, int expo)
 compute efficiently pow with an integer exponent More...
 
int expo_ten_inf (double nb)
 get log with base 10 More...
 
double sign (double a)
 sign function More...
 
char * get_time_machine ()
 returns the current time as a String More...
 
char * get_char_vec (int size)
 create (with memory allocation) a vector (length l_v) of char More...
 
char ** get_char_tab (int nb_r, int nb_c)
 create (with memory allocation) a [nb_r times nb_c] matrix of chars WARNING: only the first dimension (nb_r) is allocated, pointing to NULL ! More...
 
void free_char_vec (char *vec)
 release memory for a vector of char More...
 
char * get_char_cpy_varg (const char *fmt, va_list argptr)
 
char * get_char_cpy (const char *fmt,...)
 Copy the provided formatted string into a newly allocated memory. More...
 
int set_char_cpy (char **dest, const char *fmt,...)
 Set a copy the provided formatted string into a newly allocated memory. More...
 
int set_um_string (char **um_str, const char *new_txt)
 Redirect the memory of the string user model to a newly allocated char array. More...
 
void free_char_tab (char **mat)
 release memory for a matrix of chars More...
 
char * str_replace_char (char *orig, const char old_char, const char *new_str, int free_orig)
 Create a copy of a string with a specific char replaced by a string. More...
 
int mbs_is_char_in (const char *text, const char **list, int nb_elem)
 Return the index of the provided text in a list of text. More...
 
int mbs_is_char_in_concat (const char *text, const char *list)
 Check if the provided text is in a concatenated list of text separated by ";". More...
 
int mbs_is_char_i_in_concat (const char *text, const char *list)
 Check if the provided text (not case sensitive) is in a concatenated list of text separated by ";". More...
 
int mbs_is_char_in_concat_full (const char *text, const char *list, char sep, int case_sens)
 Check if the provided text is in a concatenated list of text. More...
 
double *** get_d3Darray_1 (int x, int y, int z)
 create (with memory allocation) a [x times y times z] 3D array of doubles, starting at index 1 More...
 
double *** get_d3Darray_0 (int x, int y, int z)
 create (with memory allocation) a [x times y times z] 3D array of doubles, starting at index 0 More...
 
void free_d3Darray_1 (double ***array3D)
 
void free_d3Darray_0 (double ***array3D)
 release memory for a 3D array of doubles, starting at index 0 More...
 
int mbs_strncmp (const char *__s1, const char *__s2, size_t n)
 Compares only the first (at most) n bytes of of two chars. More...
 
int mbs_strnicmp (const char *__s1, const char *__s2, size_t n)
 Compares only the first (at most) n bytes of of two chars ignoring the case. More...
 
int mbs_strcmp (const char *__s1, const char *__s2)
 Compare two char with strcmp. More...
 
int mbs_stricmp (const char *__s1, const char *__s2)
 Compare two char with _stricmp (convert strings to lower case, ignoring case sensitive). More...
 
char * mbs_strndup (const char *input_char, size_t char_size)
 (taken from strdup definition in string.h since C23) Returns a pointer to a null-terminated byte string, which contains copies of at most size bytes from the string pointed to by src. The space for the new string is obtained as if malloc was called. If the null terminator is not encountered in the first size bytes, it is appended to the duplicated string. More...
 

Macro Definition Documentation

◆ MSG_ERR

#define MSG_ERR   ">> USEFUL>> "

◆ MSG_PRE

#define MSG_PRE   " >> "

Function Documentation

◆ expo_ten_inf()

int expo_ten_inf ( double  nb)

get log with base 10

Parameters
[in]nbnumber to compute the log
Returns
the exponent (base 10) -> log_10 (this is the inferior exponent)

◆ free_char_tab()

void free_char_tab ( char **  mat)

release memory for a matrix of chars

Parameters
[in]matmatrix of chars

◆ free_char_vec()

void free_char_vec ( char *  vec)

release memory for a vector of char

Parameters
[in]vecvector of chars

◆ free_d3Darray_0()

void free_d3Darray_0 ( double ***  array3D)

release memory for a 3D array of doubles, starting at index 0

release memory for a 3D array of doubles, starting at index 1

Parameters
[in,out]array3D3D array of doubles

◆ free_d3Darray_1()

void free_d3Darray_1 ( double ***  array3D)

◆ get_char_cpy()

char* get_char_cpy ( const char *  fmt,
  ... 
)

Copy the provided formatted string into a newly allocated memory.

The caller is responsible to release the memory.

Parameters
fmtThe string to be formated and stored. Providing NULL returns NULL.
Returns
A pointer to the memory storing the formatted string.

◆ get_char_cpy_varg()

char* get_char_cpy_varg ( const char *  fmt,
va_list  argptr 
)

◆ get_char_tab()

char** get_char_tab ( int  nb_r,
int  nb_c 
)

create (with memory allocation) a [nb_r times nb_c] matrix of chars WARNING: only the first dimension (nb_r) is allocated, pointing to NULL !

Parameters
[in]nb_r= nb of rows
[in]nb_c= nb of columns
Returns
requested matrix of chars

◆ get_char_vec()

char* get_char_vec ( int  l_v)

create (with memory allocation) a vector (length l_v) of char

Parameters
[in]l_v= size of the vector
Returns
requested vector of char

◆ get_d3Darray_0()

double*** get_d3Darray_0 ( int  nb_r,
int  nb_c,
int  z 
)

create (with memory allocation) a [x times y times z] 3D array of doubles, starting at index 0

Parameters
[in]nb_rnb of first dimension
[in]nb_cnb of second dimension
[in]znb of third dimension
Returns
requested 3D array of doubles

◆ get_d3Darray_1()

double*** get_d3Darray_1 ( int  x,
int  y,
int  z 
)

create (with memory allocation) a [x times y times z] 3D array of doubles, starting at index 1

Parameters
[in]xnb of first dimension
[in]ynb of second dimension
[in]znb of third dimension
Returns
requested 3D array of doubles

◆ get_time_machine()

char* get_time_machine ( )

returns the current time as a String

source: http://en.wikipedia.org/wiki/C_date_and_time_functions

◆ mbs_free()

void* mbs_free ( void *  __ptr)

Free the pointor ptr and set it to NULL.

◆ mbs_is_char_i_in_concat()

int mbs_is_char_i_in_concat ( const char *  text,
const char *  list 
)

Check if the provided text (not case sensitive) is in a concatenated list of text separated by ";".

Parameters
[in]textPointer to the text to be found.
[in]listPointer to contatenated texts to be looked into. The separator must be ";".
Returns
Either:
  • 0: text FOUND in the list;
  • 1: text NOT found in the list;
  • an error code for invalid argument (_MBS_ERR_LOW_INVALID);

◆ mbs_is_char_in()

int mbs_is_char_in ( const char *  text,
const char **  list,
int  nb_elem 
)

Return the index of the provided text in a list of text.

Parameters
[in]textPointer to the text to be found.
[in]listArray of pointer to text to be looked into.
[in]nb_elemThe number of element in list.
Returns
Either:
  • the index of the text in the list (>=0);
  • a code indicate the text was not in the list (-1);
  • an error code for invalid argument (_MBS_ERR_LOW_INVALID);

◆ mbs_is_char_in_concat()

int mbs_is_char_in_concat ( const char *  text,
const char *  list 
)

Check if the provided text is in a concatenated list of text separated by ";".

Parameters
[in]textPointer to the text to be found.
[in]listPointer to contatenated texts to be looked into. The separator must be ";".
Returns
Either:
  • 0: text FOUND in the list;
  • 1: text NOT found in the list;
  • an error code for invalid argument (_MBS_ERR_LOW_INVALID);

◆ mbs_is_char_in_concat_full()

int mbs_is_char_in_concat_full ( const char *  text,
const char *  list,
char  sep,
int  case_sens 
)

Check if the provided text is in a concatenated list of text.

Parameters
[in]textPointer to the text to be found.
[in]listPointer to contatenated texts to be looked into. Concatenated text are separated by the provided separator.
[in]speThe separator used in the list to separate the different texts.
[in]case_sensIf true (non-zero) the comparison is case-sensitive. If false (zero), the comparison ignore the case.
Returns
Either:
  • 0: text FOUND in the list;
  • 1: text NOT found in the list;
  • an error code for invalid argument (_MBS_ERR_LOW_INVALID);

◆ mbs_log()

void mbs_log ( char *  msg,
  ... 
)

Function for logging messages in a file called "log" where the program is executed.

It is useful for debugging when the printf messages do not appear in the console (for instance in Simulink).

◆ mbs_strcmp()

int mbs_strcmp ( const char *  __s1,
const char *  __s2 
)

Compare two char with strcmp.

Parameters
[in]__s1The first char to compare.
[in]__s2The second char to compare.
Returns
Negative number in case of error, 0 otherwhise.

◆ mbs_stricmp()

int mbs_stricmp ( const char *  __s1,
const char *  __s2 
)

Compare two char with _stricmp (convert strings to lower case, ignoring case sensitive).

Parameters
[in]__s1The first char to compare.
[in]__s2The second char to compare.
Returns
Negative number in case of error, 0 otherwhise.

◆ mbs_strncmp()

int mbs_strncmp ( const char *  __s1,
const char *  __s2,
size_t  n 
)

Compares only the first (at most) n bytes of of two chars.

This is a wrapping of the standard function strncmp() but catching undefined behaviors when at least one pointer is NULL:

  • If both pointers are NULL: 0
  • If bytes must be compare (n is not zero): 1
  • If not byte must be compared (n is zero): 0

The later has been implemented as the standard declare an undefined behavior if one of the pointer is NULL but ensure a 0 returns if the count (n) is equal to 0. Catching the mix of them (zero and NULL) avoids surprises.

Parameters
[in]__s1The first char to compare.
[in]__s2The second char to compare.
[in]nThe number of bytes to compare.
Returns
The comparison output:
  • 0, if the first n bytes of s1 and s2 are equal, or if n is zero;
  • 0, if both pointers are NULL;
  • 1, if only one pointer is NULL (and n is not zero);
  • the return of strncmp(__s1, __s2, n) otherwhise.

◆ mbs_strndup()

char* mbs_strndup ( const char *  input_char,
size_t  char_size 
)

(taken from strdup definition in string.h since C23) Returns a pointer to a null-terminated byte string, which contains copies of at most size bytes from the string pointed to by src. The space for the new string is obtained as if malloc was called. If the null terminator is not encountered in the first size bytes, it is appended to the duplicated string.

The returned pointer must be passed to free to avoid a memory leak. ! User is responsible to free the memory ! If an error occurs, a null pointer is returned and errno might be set.

Parameters
[in]input_char(const char*) The char to .
[in]char_size(size_t) max number of bytes to copy from src.
Returns
NULL pointor in case of error, char pointor containing copy of input_char.

◆ mbs_strnicmp()

int mbs_strnicmp ( const char *  __s1,
const char *  __s2,
size_t  n 
)

Compares only the first (at most) n bytes of of two chars ignoring the case.

This is a wrapping of the standard function strnicmp() but catching undefined behaviors when at least one pointer is NULL:

  • If both pointers are NULL: 0
  • If bytes must be compare (n is not zero): 1
  • If not byte must be compared (n is zero): 0

The later has been implemented as the standard declare an undefined behavior if one of the pointer is NULL but ensure a 0 returns if the count (n) is equal to 0. Catching the mix of them (zero and NULL) avoids surprises.

Parameters
[in]__s1The first char to compare.
[in]__s2The second char to compare.
[in]nThe number of bytes to compare.
Returns
The comparison output:
  • 0, if the first n bytes of s1 and s2 are equal, or if n is zero;
  • 0, if both pointers are NULL;
  • 1, if only one pointer is NULL (and n is not zero);
  • the return of strncmp(__s1, __s2, n) otherwhise.

◆ pow_int()

double pow_int ( double  basis,
int  expo 
)

compute efficiently pow with an integer exponent

Parameters
[in]a= basis (double)
[in]b= exponent (integer)
Returns
a^b (same result as pow(a,b), except that b is a integer -> more efficient)

◆ set_char_cpy()

int set_char_cpy ( char **  dest,
const char *  fmt,
  ... 
)

Set a copy the provided formatted string into a newly allocated memory.

If the destination adress is not NULL, it is FREED to avoid memory leaks. Ensure to only provide dynamic allocated memory adress or NULL.

The caller is responsible to release the memory.

Parameters
[in,out]destDestination where the copy must be set.
[in]fmtThe string to be formated and stored. Providing NULL sets NULL.
Returns
MBS_INFO_SUCCESS or MBS_INFO_FAILURE

◆ set_um_string()

int set_um_string ( char **  um_str,
const char *  new_txt 
)

Redirect the memory of the string user model to a newly allocated char array.

The memory of the existing user model is freed.

A copy of the new string to set in the user model is created.

This function can be used to any pointer to char*, even if it has been created for string user model.

Parameters
um_strPointer to the pointer of the string to be replaced, cannot be NULL.
new_txtThe new text to be set in the newly allocated memory pointed by um_str.
Returns
Negative number in case of error, 0 otherwhise.

◆ sign()

double sign ( double  a)

sign function

Parameters
[in]ainput
Returns
1 if positive (or 0), -1 if negative

◆ str_replace_char()

char* str_replace_char ( char *  orig,
const char  old_char,
const char *  new_str,
int  free_orig 
)

Create a copy of a string with a specific char replaced by a string.

If the char is not present in the input string, a copy is returned.

Only 1-byte characters from UTF-8 can be replaced from the string (globally excludes accentuated letters).

Parameters
[in,out]origPointer to the original string.
[in]old_charCharacter (unique) to be replaced from the original string.
[in]new_strPointer to the characters to be inserted instead of the old_char. A NULL pointer removes the old_char.
[in]free_strFlag to free the original_str on replacement success. This allow to call the function as ‘txt = str_replace(txt, ’\t', " ", 1);` without memory leak.
Returns
A pointer to the modified string, or NULL in case of error.