muMECH  1.0
gelib.h
Go to the documentation of this file.
1 #ifndef GELIB_GENERAL_H
2 #define GELIB_GENERAL_H
3 
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <sys/stat.h>
7 
8 
9 //#ifdef _MSVS(WIN32) || defined(WIN64)
10 #ifndef _WINDOWS
11 #if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
12 #define _WINDOWS
13 #endif
14 #endif
15 
16 
17 #ifdef _WINDOWS
18 #include <windows.h>
19 #endif
20 
21 
28 namespace gelibspace {
29 
30 #define MAX_MSG_LENGTH 2047
31 
32 #ifdef _WINDOWS
33 #define DIRSEPARATOR '\\'
34 #else
35 #define DIRSEPARATOR '/'
36 #endif
37 
38 #ifdef _WINDOWS
39 #define NUM_DIGITS_IN_PRINTED_EXPONENT 3
40 #else
41 #define NUM_DIGITS_IN_PRINTED_EXPONENT 2
42 #endif
43 
44 #ifdef _WINDOWS
45 #define _STRCASECMP _stricmp
46 #else
47 #define _STRCASECMP strcasecmp
48 #endif
49 
50 
51 
56 #ifdef _WINDOWS
57 #define TC_B_BLACK FOREGROUND_GREEN
58 #define TC_D_BLACK FOREGROUND_GREEN
59 #define TC_B_RED FOREGROUND_RED
60 #define TC_D_RED FOREGROUND_RED
61 #define TC_B_GREEN FOREGROUND_GREEN
62 #define TC_D_GREEN FOREGROUND_GREEN
63 #define TC_B_YELLOW FOREGROUND_RED | FOREGROUND_GREEN
64 #define TC_D_YELLOW FOREGROUND_RED | FOREGROUND_GREEN
65 #define TC_B_BLUE FOREGROUND_BLUE
66 #define TC_D_BLUE FOREGROUND_BLUE
67 #define TC_B_VIOLET FOREGROUND_BLUE | FOREGROUND_RED
68 #define TC_D_VIOLET FOREGROUND_BLUE | FOREGROUND_RED
69 #define TC_B_CYAN FOREGROUND_GREEN | FOREGROUND_BLUE
70 #define TC_D_CYAN FOREGROUND_GREEN | FOREGROUND_BLUE
71 #define TC_B_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
72 #define TC_D_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
73 //
74 #define TC_DEFAULT TC_D_WHITE
75 #else
76 // Bright x Dark
77 #define TC_B_BLACK "\033[0;30m"
78 #define TC_D_BLACK "\033[1;30m"
79 #define TC_B_RED "\033[0;31m"
80 #define TC_D_RED "\033[1;31m"
81 #define TC_B_GREEN "\033[0;32m"
82 #define TC_D_GREEN "\033[1;32m"
83 #define TC_B_YELLOW "\033[0;33m"
84 #define TC_D_YELLOW "\033[1;33m"
85 #define TC_B_BLUE "\033[0;34m"
86 #define TC_D_BLUE "\033[1;34m"
87 #define TC_B_VIOLET "\033[0;35m"
88 #define TC_D_VIOLET "\033[1;35m"
89 #define TC_B_CYAN "\033[0;36m"
90 #define TC_D_CYAN "\033[1;36m"
91 #define TC_B_WHITE "\033[0;37m"
92 #define TC_D_WHITE "\033[1;37m"
93 //
94 #define TC_DEFAULT "\033[0m"
95 #endif
96 
98 enum ETCLR {
100 };
101 
102 // conversion
103 #ifdef _WINDOWS
104 inline WORD trmt_clr_int2clr (ETCLR clr)
105 #else
106 inline const char* trmt_clr_int2clr (ETCLR clr)
107 #endif
108 {
109  switch (clr) {
110  case ETC_B_BLACK: return TC_B_BLACK; break;
111  case ETC_D_BLACK: return TC_D_BLACK; break;
112  case ETC_B_RED: return TC_B_RED; break;
113  case ETC_D_RED: return TC_D_RED; break;
114  case ETC_B_GREEN: return TC_B_GREEN; break;
115  case ETC_D_GREEN: return TC_D_GREEN; break;
116  case ETC_B_YELLOW: return TC_B_YELLOW; break;
117  case ETC_D_YELLOW: return TC_D_YELLOW; break;
118  case ETC_B_BLUE: return TC_B_BLUE; break;
119  case ETC_D_BLUE: return TC_D_BLUE; break;
120  case ETC_B_VIOLET: return TC_B_VIOLET; break;
121  case ETC_D_VIOLET: return TC_D_VIOLET; break;
122  case ETC_B_CYAN: return TC_B_CYAN; break;
123  case ETC_D_CYAN: return TC_D_CYAN; break;
124  case ETC_B_WHITE: return TC_B_WHITE; break;
125  case ETC_D_WHITE: return TC_D_WHITE; break;
126  case ETC_DEFAULT: return TC_DEFAULT; break;
127  case ETC_VOID: return NULL; break;
128  }
129  return NULL;
130 }
131 
132 #ifdef _WINDOWS
133 #define CHANGE_CONSOLE_COLOUR(_1,_2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _2)
134 #else
135 #define CHANGE_CONSOLE_COLOUR(_1,_2) fprintf (_1, "%s", _2)
136 #endif
137 
138 inline void change_console_colour (FILE *stream, ETCLR clr)
139 {
141 }
142 
143 
147 void errorr (const char* file, int line, const char *format, ...);
148 void warningg (const char* file, int line, const char *format, ...);
149 
150 
151 #define errol errorr (__FILE__, __LINE__, "za ktery muze autor teto sqele utility, bezte ho vytahat za usi")
152 #define _errorr0 errorr (__FILE__, __LINE__, "za ktery muze autor teto sqele utility, bezte ho vytahat za usi")
153 #define _errorr1(_1) errorr (__FILE__, __LINE__, _1)
154 #define _errorr2(_1,_2) errorr (__FILE__, __LINE__, _1,_2)
155 #define _errorr3(_1,_2,_3) errorr (__FILE__, __LINE__, _1,_2,_3)
156 #define _errorr4(_1,_2,_3,_4) errorr (__FILE__, __LINE__, _1,_2,_3,_4)
157 #define _errorr5(_1,_2,_3,_4,_5) errorr (__FILE__, __LINE__, _1,_2,_3,_4,_5)
158 #define _errorr6(_1,_2,_3,_4,_5,_6) errorr (__FILE__, __LINE__, _1,_2,_3,_4,_5,_6)
159 
160 #define _errorr(_1) _errorr1(_1)
161 
162 
163 #define _warningg(_1) _warningg1(_1)
164 
165 #define _warningg1(_1) warningg (__FILE__, __LINE__, _1)
166 #define _warningg2(_1,_2) warningg (__FILE__, __LINE__, _1,_2)
167 #define _warningg3(_1,_2,_3) warningg (__FILE__, __LINE__, _1,_2,_3)
168 #define _warningg4(_1,_2,_3,_4) warningg (__FILE__, __LINE__, _1,_2,_3,_4)
169 #define _warningg5(_1,_2,_3,_4,_5) warningg (__FILE__, __LINE__, _1,_2,_3,_4,_5)
170 
171 
175 void openFileTest (const FILE *stream, const char *name);
176 
177 FILE* openFileN (const char* File, int Line, const char *mode, const char *key, const char *name);
178 FILE* openFilePN (const char* File, int Line, const char *mode, const char *key, const char *path, const char *name);
179 FILE* openFilePNS (const char* File, int Line, const char *mode, const char *key, const char *path, const char *name, const char *suff);
180 FILE* openFilePNSS (const char* File, int Line, const char *mode, const char *key, const char *path, const char *name, const char *suff, const char *suff2);
181 
182 #define _openFileN(_1,_2,_3) openFileN (__FILE__, __LINE__, _1,_2,_3)
183 #define _openFilePN(_1,_2,_3,_4) openFilePN (__FILE__, __LINE__, _1,_2,_3,_4)
184 #define _openFilePNS(_1,_2,_3,_4,_5) openFilePNS (__FILE__, __LINE__, _1,_2,_3,_4,_5)
185 #define _openFilePNSS(_1,_2,_3,_4,_5,_6) openFilePNSS(__FILE__, __LINE__, _1,_2,_3,_4,_5,_6)
186 
187 
191 // misto "class" muzu asi psat "typename"
192 template <class ArgType> void allocate (ArgType*& p, long a) { if (a) p = new ArgType [a]; else p = NULL; }
193 template <class ArgType> void allocate (ArgType**& p, long a, long b) { if (a) { p = new ArgType* [a]; while (a--) allocate (p[a], b); } else p = NULL; }
194 template <class ArgType> void allocate (ArgType***& p, long a, long b, long c) { if (a) { p = new ArgType** [a]; while (a--) allocate (p[a], b, c); } else p = NULL; }
195 
196 
200 // *** DEALLOCATE field *P ***
201 template <class ArgType> void deallocateCheck (ArgType* p, bool check=true)
202 {
203  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
204  else delete [] p;
205 }
206 template <class ArgType> void deallocateCheckUno (ArgType* p, bool check=true)
207 {
208  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
209  else delete p;
210 }
211 
212 // *** DEALLOCATE field **P ***
213 template <class ArgType> void deallocateCheck (ArgType** p, long a, bool check=true)
214 {
215  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
216  else { while (a--) deallocateCheck (p[a], check); delete [] p; }
217 }
218 template <class ArgType> void deallocateCheckUno (ArgType** p, long a, bool check=true)
219 {
220  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
221  else { while (a--) deallocateCheckUno (p[a], check); delete [] p; }
222 }
223 
224 // *** DEALLOCATE field ***P ***
225 template <class ArgType> void deallocateCheck (ArgType*** p, long a, long b, bool check=true)
226 {
227  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
228  else { while (a--) deallocateCheck (p[a], b, check); delete [] p; }
229 }
230 template <class ArgType> void deallocateCheckUno (ArgType*** p, long a, long b, bool check=true)
231 {
232  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
233  else { while (a--) deallocateCheckUno (p[a], b, check); delete [] p; }
234 }
235 template <class ArgType> void deallocateCheck (ArgType*** p, long a, long *b, bool check=true)
236 {
237  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
238  else { while (a--) deallocateCheck (p[a], b[a], check); delete [] p; }
239 }
240 
241 
245 //memset (lid,-1,NumDomains*sizeof(long));
246 //
247 template <class ArgType> void fill_all_by (ArgType* p, long a, ArgType value) { while (a--) p[a] = value; }
248 template <class ArgType> void fill_all_by (ArgType** p, long a, long b, ArgType value) { while (a--) fill_all_by (p[a], b, value); }
249 template <class ArgType> void fill_all_by (ArgType*** p, long a, long b, long c, ArgType value) { while (a--) fill_all_by (p[a], b, c, value); }
250 
251 
252 template <class ArgType> void fill_row_by (ArgType** p, long i, long m, ArgType value) { fill_all_by (p[i], m, value); }
253 template <class ArgType> void fill_col_by (ArgType** p, long n, long j, ArgType value) { while (n--) p[n][j] = value; }
254 
255 
260 template <class ArgType> void allocate_another_val (long n, ArgType *&old_field)
261 {
262  if (!n && old_field) _errorr ("allocate_another_val: n == 0 and old_field != NULL - forbidden");
263  ArgType *new_field = new ArgType [n+1];
264  while (n--) new_field[n] = old_field[n];
265  delete [] old_field;
266  old_field = new_field;
267  new_field=NULL;
268 }
269 
271 template <class ArgType> void allocate_another_uno_ptr (long n, ArgType **&old_field, long add=1)
272 {
273  ArgType **new_field = new ArgType* [n+add];
274  while (n--) new_field[n] = old_field[n];
275  delete [] old_field;
276  old_field = new_field;
277  new_field=NULL;
278 }
279 
281 template <class ArgType> void setadd_another_uno_ptr (long &n, ArgType **&field, ArgType *p)
282 {
283  allocate_another_uno_ptr (n, field);
284  field[n++] = p;
285 }
286 
287 
292 template <class ArgType> void shake_down_ptr_array (long &n, ArgType *array)
293 {
294  long i,j;
295 
296  for (j=0; j<n; j++)
297  if (array[j] == NULL) break;
298 
299  for (i=j+1; i<n; i++)
300  if (array[i]) {
301  array[j++] = array[i];
302  array[i] = NULL;
303  }
304 
305  n=j;
306 }
307 
309 template <class ArgType> long arrays_arenot_identical (long n, ArgType *a1, ArgType *a2)
310 {
311  if (a1 && a2) {
312  while (n--) if (a1[n] != a2[n]) break;
313  return n+1;
314  }
315  if (a1 == a2 && !n) return 0;
316  else return -1;
317 }
318 
320 template <class ArgType> long is_member_of_array (ArgType val, long n, const ArgType *array)
321 {
322  while (n--) if (val == array[n]) break;
323  return n+1;
324 }
325 
326 
328 template <class ArgType> bool members_are_unique (long n, const ArgType *array)
329 {
330  long i,j;
331 
332  for (i=0; i<n-1; i++)
333  for (j=i+1; j<n; j++)
334  if (array[i] == array[j]) return false;
335 
336  return true;
337 }
338 
339 
342 template <class ArgType> bool same_array_elements_asym (long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
343 {
344  if (same_dim && nx!=ny) return false;
345 
346  long i,j;
347 
348  for (i=0; i<nx; i++) {
349  for (j=0; j<ny; j++)
350  if (x[i] == y[j]) break;
351 
352  if (j==ny) return false;
353  }
354 
355  return true;
356 }
357 
362 template <class ArgType> bool same_array_elements_sym (long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
363 {
364  if (! same_array_elements_asym(nx, x, ny, y, same_dim) ) return false;
365  if (! same_array_elements_asym(ny, y, nx, x, same_dim) ) return false;
366  return true;
367 }
368 
369 
370 
371 
373 template <class ArgType> void add_array (long &n1, ArgType *&array1, long n2, const ArgType *array2)
374 {
375  if (n2 == 0) return;
376 
377  ArgType *new_array = new ArgType [n1+n2];
378 
379  long i;
380  for (i=0; i<n1; i++) new_array[ i] = array1[i];
381  for (i=0; i<n2; i++) new_array[n1+i] = array2[i];
382 
383  delete [] array1;
384  array1 = new_array;
385  new_array = NULL;
386  n1 = n1 + n2;
387 }
389 template <class ArgType> void add_array_unique (long &n1, ArgType *&array1, long n2, const ArgType *array2)
390 {
391  long i, n3 = 0;
392  for (i=0; i<n2; i++) if (!is_member_of_array (array2[i], n1, array1)) n3++;
393 
394  if (n3 == 0) return;
395 
396  ArgType *new_array = new ArgType [n1+n3];
397 
398  n3 = 0;
399  for (i=0; i<n1; i++) new_array[n3++] = array1[i];
400  for (i=0; i<n2; i++) if (!is_member_of_array (array2[i], n1, array1)) new_array[n3++] = array2[i];
401 
402  delete [] array1;
403  array1 = new_array;
404  new_array = NULL;
405  n1 = n3;
406 }
407 
409 template <class ArgType> void interchange2 (ArgType &a, ArgType &b)
410 {
411  ArgType aux;
412  aux = b;
413  b = a;
414  a = aux;
415 }
416 
417 
418 inline bool file_exist (const char *name)
419 {
420  struct stat buffer;
421  return (stat (name, &buffer) == 0);
422 }
423 
424 
425 // return false - file exists, was not deleted
426 // return true - file does not exist or was deleted
427 inline bool file_delete_if_exist (const char *name)
428 {
429  if (file_exist(name))
430  if (remove(name) != 0)
431  return false;
432 
433  return true;
434 }
435 // return false - file exists, was not deleted
436 // return true - file does not exist or was deleted
437 inline void file_delete_if_exist_exit (const char *name)
438 {
439  if (file_delete_if_exist(name) == false)
440  _errorr2("Error deleting file %s", name);
441 }
442 
443 } // namespace gelibspace
444 
445 #endif // GELIB_GENERAL_H
void allocate(ArgType *&p, long a)
*** *** *** *** ALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:192
void add_array(long &n1, ArgType *&array1, long n2, const ArgType *array2)
Definition: gelib.h:373
FILE * openFileN(const char *File, int Line, const char *mode, const char *key, const char *name)
Definition: gelib.cpp:67
#define TC_D_BLUE
Definition: gelib.h:86
#define CHANGE_CONSOLE_COLOUR(_1, _2)
Definition: gelib.h:135
#define _warningg(_1)
Definition: gelib.h:163
#define TC_D_RED
Definition: gelib.h:80
void fill_col_by(ArgType **p, long n, long j, ArgType value)
Definition: gelib.h:253
void change_console_colour(FILE *stream, ETCLR clr)
Definition: gelib.h:138
void openFileTest(const FILE *stream, const char *name)
*** *** *** *** FILE FCE *** *** *** ***
Definition: gelib.cpp:61
bool members_are_unique(long n, const ArgType *array)
check out all members of array "a" are uniq
Definition: gelib.h:328
void shake_down_ptr_array(long &n, ArgType *array)
*** *** *** *** VARIOUS TEMPLATES *** *** *** ***
Definition: gelib.h:292
#define TC_D_VIOLET
Definition: gelib.h:88
#define TC_B_CYAN
Definition: gelib.h:89
#define TC_B_YELLOW
Definition: gelib.h:83
#define array(MAT, ROWS, COLS, ROW, COL)
Definition: meso3d.cpp:139
#define TC_D_YELLOW
Definition: gelib.h:84
#define TC_B_VIOLET
Definition: gelib.h:87
#define TC_D_BLACK
Definition: gelib.h:78
#define TC_B_RED
Definition: gelib.h:79
#define TC_B_BLUE
Definition: gelib.h:85
const char * trmt_clr_int2clr(ETCLR clr)
Definition: gelib.h:106
void interchange2(ArgType &a, ArgType &b)
Definition: gelib.h:409
void warningg(const char *file, int line, const char *format,...)
Definition: gelib.cpp:26
#define _errorr2(_1, _2)
Definition: gelib.h:154
void fill_row_by(ArgType **p, long i, long m, ArgType value)
Definition: gelib.h:252
void setadd_another_uno_ptr(long &n, ArgType **&field, ArgType *p)
Definition: gelib.h:281
void add_array_unique(long &n1, ArgType *&array1, long n2, const ArgType *array2)
Definition: gelib.h:389
long is_member_of_array(ArgType val, long n, const ArgType *array)
check out "val" is member of "array"
Definition: gelib.h:320
ETCLR
enum
Definition: gelib.h:98
#define TC_D_GREEN
Definition: gelib.h:82
#define TC_B_BLACK
*** *** *** *** COLOURS *** *** *** *** Termit Colours
Definition: gelib.h:77
FILE * openFilePN(const char *File, int Line, const char *mode, const char *key, const char *path, const char *name)
Definition: gelib.cpp:77
#define TC_B_GREEN
Definition: gelib.h:81
#define TC_D_WHITE
Definition: gelib.h:92
FILE * openFilePNS(const char *File, int Line, const char *mode, const char *key, const char *path, const char *name, const char *suff)
Definition: gelib.cpp:86
#define _errorr(_1)
Definition: gelib.h:160
void allocate_another_uno_ptr(long n, ArgType **&old_field, long add=1)
Definition: gelib.h:271
#define TC_DEFAULT
Definition: gelib.h:94
bool file_delete_if_exist(const char *name)
Definition: gelib.h:427
bool file_exist(const char *name)
Definition: gelib.h:418
void allocate_another_val(long n, ArgType *&old_field)
*** *** *** *** REALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:260
#define TC_D_CYAN
Definition: gelib.h:90
bool same_array_elements_sym(long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
Function finds out whether every element of the array &#39;x&#39; is in the array &#39;y&#39;, and symmetrically ever...
Definition: gelib.h:362
#define TC_B_WHITE
Definition: gelib.h:91
void fill_all_by(ArgType *p, long a, ArgType value)
*** *** *** *** FILL TEMPLATES *** *** *** ***
Definition: gelib.h:247
void file_delete_if_exist_exit(const char *name)
Definition: gelib.h:437
void errorr(const char *file, int line, const char *format,...)
*** *** *** *** ERROR FCE *** *** *** ***
Definition: gelib.cpp:10
bool same_array_elements_asym(long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
Function finds out whether every element of the array &#39;x&#39; is in the array &#39;y&#39;.
Definition: gelib.h:342
long arrays_arenot_identical(long n, ArgType *a1, ArgType *a2)
Definition: gelib.h:309
FILE * openFilePNSS(const char *File, int Line, const char *mode, const char *key, const char *path, const char *name, const char *suff, const char *suff2)
Definition: gelib.cpp:95
void deallocateCheck(ArgType *p, bool check=true)
*** *** *** *** DEALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:201
void deallocateCheckUno(ArgType *p, bool check=true)
Definition: gelib.h:206