C++ Library, mos@ua  0.9
cpplib-mos
utils.h
Go to the documentation of this file.
1 
15 #ifndef UTILS_H
16 #define UTILS_H
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include "dbc.h"
22 
36 #define concat_2str(str1,str2) \
37  ({ \
38  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
39  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
40  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+1); \
41  strcpy(__res__, s1); \
42  strcat(__res__, s2); \
43  __res__; \
44  })
45 
57 #define concat_3str(str1,str2,str3) \
58  ({ \
59  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
60  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
61  char* s3 = (str3) == NULL ? (char*)"" : (char*)(str3); \
62  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+strlen(s3)+1); \
63  strcpy(__res__, s1); \
64  strcat(__res__, s2); \
65  strcat(__res__, s3); \
66  __res__; \
67  })
68 
81 #define concat_4str(str1,str2,str3,str4) \
82  ({ \
83  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
84  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
85  char* s3 = (str3) == NULL ? (char*)"" : (char*)(str3); \
86  char* s4 = (str4) == NULL ? (char*)"" : (char*)(str4); \
87  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+1); \
88  strcpy(__res__, s1); \
89  strcat(__res__, s2); \
90  strcat(__res__, s3); \
91  strcat(__res__, s4); \
92  __res__; \
93  })
94 
108 #define concat_5str(str1,str2,str3,str4,str5) \
109  ({ \
110  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
111  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
112  char* s3 = (str3) == NULL ? (char*)"" : (char*)(str3); \
113  char* s4 = (str4) == NULL ? (char*)"" : (char*)(str4); \
114  char* s5 = (str5) == NULL ? (char*)"" : (char*)(str5); \
115  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+strlen(s5)+1); \
116  strcpy(__res__, s1); \
117  strcat(__res__, s2); \
118  strcat(__res__, s3); \
119  strcat(__res__, s4); \
120  strcat(__res__, s5); \
121  __res__; \
122  })
123 
138 #define concat_6str(str1,str2,str3,str4,str5,str6) \
139  ({ \
140  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
141  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
142  char* s3 = (str3) == NULL ? (char*)"" : (char*)(str3); \
143  char* s4 = (str4) == NULL ? (char*)"" : (char*)(str4); \
144  char* s5 = (str5) == NULL ? (char*)"" : (char*)(str5); \
145  char* s6 = (str6) == NULL ? (char*)"" : (char*)(str6); \
146  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+strlen(s5)+strlen(s6)+1); \
147  strcpy(__res__, s1); \
148  strcat(__res__, s2); \
149  strcat(__res__, s3); \
150  strcat(__res__, s4); \
151  strcat(__res__, s5); \
152  strcat(__res__, s6); \
153  __res__; \
154  })
155 
171 #define concat_7str(str1,str2,str3,str4,str5,str6,str7) \
172  ({ \
173  char* s1 = (str1) == NULL ? (char*)"" : (char*)(str1); \
174  char* s2 = (str2) == NULL ? (char*)"" : (char*)(str2); \
175  char* s3 = (str3) == NULL ? (char*)"" : (char*)(str3); \
176  char* s4 = (str4) == NULL ? (char*)"" : (char*)(str4); \
177  char* s5 = (str5) == NULL ? (char*)"" : (char*)(str5); \
178  char* s6 = (str6) == NULL ? (char*)"" : (char*)(str6); \
179  char* s7 = (str7) == NULL ? (char*)"" : (char*)(str7); \
180  char* __res__ = (char*)alloca(strlen(s1)+strlen(s2)+strlen(s3)+strlen(s4)+strlen(s5)+strlen(s6)+strlen(s7)+1); \
181  strcpy(__res__, s1); \
182  strcat(__res__, s2); \
183  strcat(__res__, s3); \
184  strcat(__res__, s4); \
185  strcat(__res__, s5); \
186  strcat(__res__, s6); \
187  strcat(__res__, s7); \
188  __res__; \
189  })
190 
199 #define int2str(num) \
200  ({ \
201  char* __res__ = (char*)alloca(numDigits((int)num)+1); \
202  sprintf(__res__, "%d", (int)num); \
203  __res__; \
204  })
205 
220 #define int2nstr(num, len) \
221  ({ \
222  require (len > 0, concat_3str("invalid length value (", int2str(len), ")")); \
223  int d = numDigits((int)num); \
224  if (len > d) \
225  d = len; \
226  char* __res__ = (char*)alloca(d+1); \
227  sprintf(__res__, "%0*d", d, (int)num); \
228  __res__; \
229  })
230 
242 #define perc2str(percentage) \
243  ({ \
244  require (percentage >= 0 && percentage <= 100, concat_3str("invalid percentage value (", int2str(percentage), ")")); \
245  char* __res__ = (char*)alloca(4+1); \
246  sprintf(__res__, "%3d%%", (int)percentage); \
247  __res__; \
248  })
249 
257 #define length_vargs_string_list(first) \
258  ({ \
259  int __res__ = 0; \
260  va_list ap; \
261  va_start(ap, first); \
262  char* t = first; \
263  while (t != NULL) \
264  { \
265  __res__ += strlen(t); \
266  t = va_arg(ap, char*); \
267  } \
268  va_end(ap); \
269  __res__; \
270  })
271 
272 #ifdef EXCEPTION_POLICY
273 #define not_null(pnt) \
274  ({ \
275  if ((pnt) == NULL) \
276  throw string_concat(NULL, 0, (char*)"Null pointer error", (char*)", pointer: \"", #pnt, (char*)"\", function: \"", __FUNCTION__, (char*)"\":", int2str(__LINE__), (char*)", file: \"", __FILE__, (char*)"\"\n", NULL); \
277  pnt; \
278  })
279 #else
280 
297 #define not_null(pnt) \
298  ({ \
299  if ((pnt) == NULL) \
300  { \
301  fprintf (stderr, "Null pointer error, pointer: \"%s\", function: \"%s\":%d, file: \"%s\"\n", \
302  #pnt, __FUNCTION__, __LINE__ , __FILE__); \
303  *((int*)0) = 0; \
304  abort (); \
305  } \
306  pnt; \
307  })
308 #endif
309 
328 void* mem_alloc(int size);
329 
339 void mem_free(void* pnt);
340 
354 char* string_clone(char* str);
355 
367 int string_num_lines(char* text);
368 
380 int string_num_columns(char* text);
381 
395 int string_count_char(char* text, char* ch);
396 
410 int string_starts_with(char* text, char* prefix);
411 
425 int string_ends_with(char* text, char* suffix);
426 
448 char* string_concat(char* res, int max_length, char* text, ...);
449 
465 int random_boolean(int trueProb);
466 
482 int random_int(int min, int max);
483 
502 char* random_string(char** list, int* used, int length);
503 
507 void clear_console();
508 
519 void move_cursor(int line,int column);
520 
524 void hide_cursor();
525 
529 void show_cursor();
530 
542 int string_list_length(char** list);
543 
557 char** string_list_clone(char** list);
558 
572 char** string_list_free(char** list);
573 
574 int numDigits(int num);
575 
593 char* int2nstring(char* res, int num, int len);
594 
610 char* percentage2string(char* res, int percentage);
611 
612 #endif
613 
614 /* ************************************************** */
618 /* ************************************************** */
619 
void mem_free(void *pnt)
A replacement for free function.
int string_count_char(char *text, char *ch)
Counts the number of occurrences of an UTF8 character in a text.
char * string_clone(char *str)
Replicates a string.
int string_list_length(char **list)
Number of elements of a NULL terminated list of strings.
char ** string_list_free(char **list)
Frees the memory allocated to a NULL terminated list of strings.
int random_boolean(int trueProb)
Generates a random boolean value.
Design-by-Contract module.
int string_num_lines(char *text)
Number of lines of a string.
int string_ends_with(char *text, char *suffix)
Tests if a string ends with a suffix.
void show_cursor()
Shows the terminal cursor.
char * percentage2string(char *res, int percentage)
Converts an int percentage to a string.
void clear_console()
Clears the terminal.
void move_cursor(int line, int column)
Moves the cursor to a position in terminal.
void * mem_alloc(int size)
A replacement for malloc function.
int string_starts_with(char *text, char *prefix)
Tests if a string starts with a prefix.
char * int2nstring(char *res, int num, int len)
Converts an int value to a string.
char * random_string(char **list, int *used, int length)
Returns a random string from a given string list.
int string_num_columns(char *text)
Maximum number of columns of a string (not counting character `&#39;\n&#39;`).
char ** string_list_clone(char **list)
Replicates a NULL terminated list of strings.
int random_int(int min, int max)
Generates a random integer value within a given interval.
void hide_cursor()
Hides the terminal cursor.
char * string_concat(char *res, int max_length, char *text,...)
Concatenates a NULL terminated list of string arguments.