|
C++ Library, mos@ua
0.9
cpplib-mos
|
Useful common functions and macros. More...
Files | |
| file | utils.h |
| Useful common functions and macros. | |
Macros | |
| #define | int2str(num) |
Converts an int value to a stack allocated string. More... | |
| #define | int2nstr(num, len) |
Converts an int value to a stack allocated string. More... | |
| #define | perc2str(percentage) |
Converts an int percentage to a stack allocated string. More... | |
| #define | length_vargs_string_list(first) |
| Determines the length of all strings passed as a NULL terminated variable list of arguments (vargs). More... | |
| #define | not_null(pnt) |
Checks if address is not NULL, before its application. More... | |
Functions | |
| void * | mem_alloc (int size) |
A replacement for malloc function. More... | |
| void | mem_free (void *pnt) |
A replacement for free function. More... | |
| char * | string_clone (char *str) |
| Replicates a string. More... | |
| int | string_num_lines (char *text) |
| Number of lines of a string. More... | |
| int | string_num_columns (char *text) |
| Maximum number of columns of a string (not counting character `'\n'`). More... | |
| int | string_count_char (char *text, char *ch) |
| Counts the number of occurrences of an UTF8 character in a text. More... | |
| int | string_starts_with (char *text, char *prefix) |
| Tests if a string starts with a prefix. More... | |
| int | string_ends_with (char *text, char *suffix) |
| Tests if a string ends with a suffix. More... | |
| char * | string_concat (char *res, int max_length, char *text,...) |
Concatenates a NULL terminated list of string arguments. More... | |
| int | random_boolean (int trueProb) |
| Generates a random boolean value. More... | |
| int | random_int (int min, int max) |
| Generates a random integer value within a given interval. More... | |
| char * | random_string (char **list, int *used, int length) |
| Returns a random string from a given string list. More... | |
| void | clear_console () |
| Clears the terminal. | |
| void | move_cursor (int line, int column) |
| Moves the cursor to a position in terminal. More... | |
| void | hide_cursor () |
| Hides the terminal cursor. | |
| void | show_cursor () |
| Shows the terminal cursor. | |
| int | string_list_length (char **list) |
Number of elements of a NULL terminated list of strings. More... | |
| char ** | string_list_clone (char **list) |
Replicates a NULL terminated list of strings. More... | |
| char ** | string_list_free (char **list) |
Frees the memory allocated to a NULL terminated list of strings. More... | |
| char * | int2nstring (char *res, int num, int len) |
Converts an int value to a string. More... | |
| char * | percentage2string (char *res, int percentage) |
Converts an int percentage to a string. More... | |
String concatenation in stack memory | |
| #define | concat_2str(str1, str2) |
| Concatenates two strings in the stack memory (thus it cannot be implemented in a function). More... | |
| #define | concat_3str(str1, str2, str3) |
| Concatenates three strings in the stack memory (thus it cannot be implemented in a function). More... | |
| #define | concat_4str(str1, str2, str3, str4) |
| Concatenates four strings in the stack memory (thus it cannot be implemented in a function). More... | |
| #define | concat_5str(str1, str2, str3, str4, str5) |
| Concatenates five strings in the stack memory (thus it cannot be implemented in a function). More... | |
| #define | concat_6str(str1, str2, str3, str4, str5, str6) |
| Concatenates six strings in the stack memory (thus it cannot be implemented in a function). More... | |
| #define | concat_7str(str1, str2, str3, str4, str5, str6, str7) |
| Concatenates seven strings in the stack memory (thus it cannot be implemented in a function). More... | |
Useful common functions and macros.
| #define concat_2str | ( | str1, | |
| str2 | |||
| ) |
Concatenates two strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| #define concat_3str | ( | str1, | |
| str2, | |||
| str3 | |||
| ) |
Concatenates three strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| [in] | str3 | string 3 |
| #define concat_4str | ( | str1, | |
| str2, | |||
| str3, | |||
| str4 | |||
| ) |
Concatenates four strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| [in] | str3 | string 3 |
| [in] | str4 | string 4 |
| #define concat_5str | ( | str1, | |
| str2, | |||
| str3, | |||
| str4, | |||
| str5 | |||
| ) |
Concatenates five strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| [in] | str3 | string 3 |
| [in] | str4 | string 4 |
| [in] | str5 | string 5 |
| #define concat_6str | ( | str1, | |
| str2, | |||
| str3, | |||
| str4, | |||
| str5, | |||
| str6 | |||
| ) |
Concatenates six strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| [in] | str3 | string 3 |
| [in] | str4 | string 4 |
| [in] | str5 | string 5 |
| [in] | str6 | string 6 |
| #define concat_7str | ( | str1, | |
| str2, | |||
| str3, | |||
| str4, | |||
| str5, | |||
| str6, | |||
| str7 | |||
| ) |
Concatenates seven strings in the stack memory (thus it cannot be implemented in a function).
A NULL reference is treated as an empty string.
| [in] | str1 | string 1 |
| [in] | str2 | string 2 |
| [in] | str3 | string 3 |
| [in] | str4 | string 4 |
| [in] | str5 | string 5 |
| [in] | str6 | string 6 |
| [in] | str7 | string 7 |
| #define int2str | ( | num | ) |
Converts an int value to a stack allocated string.
| [in] | num | integer number |
| #define int2nstr | ( | num, | |
| len | |||
| ) |
Converts an int value to a stack allocated string.
If necessary, fills the result string with left zeros.
| [in] | num | integer number |
| [in] | len | minimum length of result string |
len > 0 | #define perc2str | ( | percentage | ) |
Converts an int percentage to a stack allocated string.
| [in] | percentage | an integer number with a percentage value |
percentage >= 0 && percentage <= 100 | #define length_vargs_string_list | ( | first | ) |
Determines the length of all strings passed as a NULL terminated variable list of arguments (vargs).
| [in] | first | the argument that precedes the vargs argument list. |
| #define not_null | ( | pnt | ) |
Checks if address is not NULL, before its application.
This macro is a non-defensive implementation of a null pointer verification. It implements two error handling policies:
stderr (with the identification and the precise location of the failure), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution;char* exception with the description of the failure.| [in] | pnt | memory address |
| void* mem_alloc | ( | int | size | ) |
A replacement for malloc function.
This function is a non-defensive implementation of malloc error verification. It implements two error handling policies:
stderr (with the identification and the precise location of the failure), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution;char* exception with the description of the failure.| [in] | size | number of bytes to be allocated |
size >= 0 | void mem_free | ( | void * | pnt | ) |
A replacement for free function.
| [in] | pnt | memory address to be freed |
pnt != NULL | char* string_clone | ( | char * | str | ) |
Replicates a string.
The memory is allocated in the heap (using mem_alloc()).
| [in] | str | string to be replicated |
str != NULL | int string_num_lines | ( | char * | text | ) |
Number of lines of a string.
| [in] | text | string text to process |
text != NULL | int string_num_columns | ( | char * | text | ) |
Maximum number of columns of a string (not counting character `'\n'`).
| [in] | text | string text to process |
text != NULL | int string_count_char | ( | char * | text, |
| char * | ch | ||
| ) |
Counts the number of occurrences of an UTF8 character in a text.
| [in] | text | string text to process |
| [in] | ch | string containing the sequence of bytes (one or more) of an UTF8 single character |
text != NULLch != NULL && num_chars_utf8(ch) == 1 | int string_starts_with | ( | char * | text, |
| char * | prefix | ||
| ) |
Tests if a string starts with a prefix.
| [in] | text | string text to process |
| [in] | prefix | string to test if is a prefix |
text != NULLprefix != NULL !=0) if prefix starts text, false (0) otherwise | int string_ends_with | ( | char * | text, |
| char * | suffix | ||
| ) |
Tests if a string ends with a suffix.
| [in] | text | string text to process |
| [in] | prefix | string to test if is a suffix |
text != NULLsuffix != NULL !=0) if suffix ends text, false (0) otherwise | char* string_concat | ( | char * | res, |
| int | max_length, | ||
| char * | text, | ||
| ... | |||
| ) |
Concatenates a NULL terminated list of string arguments.
This function treats all vargs arguments as strings (char*), and requires that the list is terminated with a NULL argument.
It allocates memory in heap (using mem_alloc()) if NULL is passed in the res argument.
| [in,out] | res | address of result string (if not NULL) |
| [in] | max_length | res input argument maximum length (not counting terminator `'\0'), only applies if(res != NULL)` |
| [in] | text | the first string to be used in concatenation |
text != NULLres == NULL || (max_length > 0 && length_vargs_string_list(text) <= max_length) | int random_boolean | ( | int | trueProb | ) |
Generates a random boolean value.
This function generates boolean values with defined probabilities for true (!=0) and false (0) values.
| [in] | trueProb | probability (in interval [0;100]). |
trueProb >= 0 && trueProb <= 100 | int random_int | ( | int | min, |
| int | max | ||
| ) |
Generates a random integer value within a given interval.
This function generates integer values in the interval [min;max]with an uniform distribution for all values.
| [in] | min | lower value of interval |
| [in] | max | higher value of interval |
max >= min | char* random_string | ( | char ** | list, |
| int * | used, | ||
| int | length | ||
| ) |
Returns a random string from a given string list.
This function stores the previous randomly selected string's (using the integer indexes of the string list) to disallow its repeated generation.
| [in] | list | list of strings to be selected (NULL terminated) |
| [in,out] | used | indexes of strings already selected |
| [in] | length | number of elements of lists list (not counting NULL entry) and used |
list != NULLused != NULL | void move_cursor | ( | int | line, |
| int | column | ||
| ) |
Moves the cursor to a position in terminal.
| [in] | line | position in the terminal |
| [in] | column | position in the terminal |
line >= 0 && column >= 0 | int string_list_length | ( | char ** | list | ) |
Number of elements of a NULL terminated list of strings.
| [in] | list | NULL terminated array of strings |
list != NULL NULL) | char** string_list_clone | ( | char ** | list | ) |
Replicates a NULL terminated list of strings.
The memory is allocated in the heap (using mem_alloc()).
| [in] | list | NULL terminated array of strings |
list != NULL | char** string_list_free | ( | char ** | list | ) |
Frees the memory allocated to a NULL terminated list of strings.
This function is totally compatible with string_list_clone(), and always returns NULL.
| [in] | list | NULL terminated array of strings |
list != NULL NULL | char* int2nstring | ( | char * | res, |
| int | num, | ||
| int | len | ||
| ) |
Converts an int value to a string.
If necessary, fills the result string with left zeros. It allocates memory in heap (using mem_alloc()) if NULL is passed in the res argument.
| [in,out] | res | address of result string (if not NULL) |
| [in] | num | integer number |
| [in] | len | minimum length of result string (not counting terminator `'\0'`) |
len >= numDigits(num) | char* percentage2string | ( | char * | res, |
| int | percentage | ||
| ) |
Converts an int percentage to a string.
It allocates memory in heap (using mem_alloc()) if NULL is passed in the res argument.
| [in,out] | res | address of result string (if not NULL) |
| [in] | percentage | an integer number with a percentage value |
percentage >= 0 && percentage <= 100 NULL, it returns res)