MIDAS  0.75
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
gelib.h
Go to the documentation of this file.
1 #ifndef MIDAS_GENERAL_H
2 #define MIDAS_GENERAL_H
3 
4 #include <stdio.h>
5 #include <stdarg.h>
6 
7 #ifdef _WINDOWS
8 #include <windows.h>
9 #endif
10 
11 
18 namespace midaspace {
19 
20 #define MAX_MSG_LENGTH 2047
21 
22 #ifdef _WINDOWS
23 #define DIRSEPARATOR '\\'
24 #else
25 #define DIRSEPARATOR '/'
26 #endif
27 
28 #ifdef _WINDOWS
29 #define NUM_DIGITS_IN_PRINTED_EXPONENT 3
30 #else
31 #define NUM_DIGITS_IN_PRINTED_EXPONENT 2
32 #endif
33 
34 //#ifdef _MSVS(WIN32) || defined(WIN64)
35 #ifdef _MSVS
36 #define _STRCASECMP _stricmp
37 #else
38 #define _STRCASECMP strcasecmp
39 #endif
40 
41 
42 
47 #ifdef _WINDOWS
48 #define TC_B_BLACK FOREGROUND_GREEN
49 #define TC_D_BLACK FOREGROUND_GREEN
50 #define TC_B_RED FOREGROUND_RED
51 #define TC_D_RED FOREGROUND_RED
52 #define TC_B_GREEN FOREGROUND_GREEN
53 #define TC_D_GREEN FOREGROUND_GREEN
54 #define TC_B_YELLOW FOREGROUND_RED | FOREGROUND_GREEN
55 #define TC_D_YELLOW FOREGROUND_RED | FOREGROUND_GREEN
56 #define TC_B_BLUE FOREGROUND_BLUE
57 #define TC_D_BLUE FOREGROUND_BLUE
58 #define TC_B_VIOLET FOREGROUND_BLUE | FOREGROUND_RED
59 #define TC_D_VIOLET FOREGROUND_BLUE | FOREGROUND_RED
60 #define TC_B_CYAN FOREGROUND_GREEN | FOREGROUND_BLUE
61 #define TC_D_CYAN FOREGROUND_GREEN | FOREGROUND_BLUE
62 #define TC_B_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
63 #define TC_D_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
64 //
65 #define TC_DEFAULT TC_D_WHITE
66 #else
67 // Bright x Dark
68 #define TC_B_BLACK "\033[0;30m"
69 #define TC_D_BLACK "\033[1;30m"
70 #define TC_B_RED "\033[0;31m"
71 #define TC_D_RED "\033[1;31m"
72 #define TC_B_GREEN "\033[0;32m"
73 #define TC_D_GREEN "\033[1;32m"
74 #define TC_B_YELLOW "\033[0;33m"
75 #define TC_D_YELLOW "\033[1;33m"
76 #define TC_B_BLUE "\033[0;34m"
77 #define TC_D_BLUE "\033[1;34m"
78 #define TC_B_VIOLET "\033[0;35m"
79 #define TC_D_VIOLET "\033[1;35m"
80 #define TC_B_CYAN "\033[0;36m"
81 #define TC_D_CYAN "\033[1;36m"
82 #define TC_B_WHITE "\033[0;37m"
83 #define TC_D_WHITE "\033[1;37m"
84 //
85 #define TC_DEFAULT "\033[0m"
86 #endif
87 
89 enum ETCLR {
91 };
92 
93 // conversion
94 #ifdef _WINDOWS
95 inline WORD trmt_clr_int2clr (ETCLR clr)
96 #else
97 inline const char* trmt_clr_int2clr (ETCLR clr)
98 #endif
99 {
100  switch (clr) {
101  case ETC_B_BLACK: return TC_B_BLACK; break;
102  case ETC_D_BLACK: return TC_D_BLACK; break;
103  case ETC_B_RED: return TC_B_RED; break;
104  case ETC_D_RED: return TC_D_RED; break;
105  case ETC_B_GREEN: return TC_B_GREEN; break;
106  case ETC_D_GREEN: return TC_D_GREEN; break;
107  case ETC_B_YELLOW: return TC_B_YELLOW; break;
108  case ETC_D_YELLOW: return TC_D_YELLOW; break;
109  case ETC_B_BLUE: return TC_B_BLUE; break;
110  case ETC_D_BLUE: return TC_D_BLUE; break;
111  case ETC_B_VIOLET: return TC_B_VIOLET; break;
112  case ETC_D_VIOLET: return TC_D_VIOLET; break;
113  case ETC_B_CYAN: return TC_B_CYAN; break;
114  case ETC_D_CYAN: return TC_D_CYAN; break;
115  case ETC_B_WHITE: return TC_B_WHITE; break;
116  case ETC_D_WHITE: return TC_D_WHITE; break;
117  case ETC_DEFAULT: return TC_DEFAULT; break;
118  case ETC_VOID: return NULL; break;
119  }
120  return NULL;
121 }
122 
123 #ifdef _WINDOWS
124 #define CHANGE_CONSOLE_COLOUR(_1,_2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _2)
125 #else
126 #define CHANGE_CONSOLE_COLOUR(_1,_2) fprintf (_1, "%s", _2)
127 #endif
128 
129 inline void change_console_colour (FILE *stream, ETCLR clr)
130 {
132 }
133 
134 
138 void errorr (const char* file, int line, const char *format, ...);
139 void warningg (const char* file, int line, const char *format, ...);
140 
141 
142 #define errol errorr (__FILE__, __LINE__, "za ktery muze autor teto sqele utility, bezte ho vytahat za usi")
143 #define _errorr0 errorr (__FILE__, __LINE__, "za ktery muze autor teto sqele utility, bezte ho vytahat za usi")
144 #define _errorr1(_1) errorr (__FILE__, __LINE__, _1)
145 #define _errorr2(_1,_2) errorr (__FILE__, __LINE__, _1,_2)
146 #define _errorr3(_1,_2,_3) errorr (__FILE__, __LINE__, _1,_2,_3)
147 #define _errorr4(_1,_2,_3,_4) errorr (__FILE__, __LINE__, _1,_2,_3,_4)
148 #define _errorr5(_1,_2,_3,_4,_5) errorr (__FILE__, __LINE__, _1,_2,_3,_4,_5)
149 #define _errorr6(_1,_2,_3,_4,_5,_6) errorr (__FILE__, __LINE__, _1,_2,_3,_4,_5,_6)
150 
151 #define _errorr(_1) _errorr1(_1)
152 
153 
154 #define _warningg(_1) _warningg1(_1)
155 
156 #define _warningg1(_1) warningg (__FILE__, __LINE__, _1)
157 #define _warningg2(_1,_2) warningg (__FILE__, __LINE__, _1,_2)
158 #define _warningg3(_1,_2,_3) warningg (__FILE__, __LINE__, _1,_2,_3)
159 #define _warningg4(_1,_2,_3,_4) warningg (__FILE__, __LINE__, _1,_2,_3,_4)
160 #define _warningg5(_1,_2,_3,_4,_5) warningg (__FILE__, __LINE__, _1,_2,_3,_4,_5)
161 
162 
166 void openFileTest (const FILE *stream, const char *name);
167 
168 FILE* openFileN (const char* File, int Line, const char *mode, const char *key, const char *name);
169 FILE* openFilePN (const char* File, int Line, const char *mode, const char *key, const char *path, const char *name);
170 FILE* openFilePNS (const char* File, int Line, const char *mode, const char *key, const char *path, const char *name, const char *suff);
171 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);
172 
173 #define _openFileN(_1,_2,_3) openFileN (__FILE__, __LINE__, _1,_2,_3)
174 #define _openFilePN(_1,_2,_3,_4) openFilePN (__FILE__, __LINE__, _1,_2,_3,_4)
175 #define _openFilePNS(_1,_2,_3,_4,_5) openFilePNS (__FILE__, __LINE__, _1,_2,_3,_4,_5)
176 #define _openFilePNSS(_1,_2,_3,_4,_5,_6) openFilePNSS(__FILE__, __LINE__, _1,_2,_3,_4,_5,_6)
177 
178 
182 // misto "class" muzu asi psat "typename"
183 template <class ArgType> void allocate (ArgType*& p, long a) { if (a) p = new ArgType [a]; else p = NULL; }
184 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; }
185 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; }
186 
187 
191 // *** DEALLOCATE field *P ***
192 template <class ArgType> void deallocateCheck (ArgType* p, bool check=true)
193 {
194  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
195  else delete [] p;
196 }
197 template <class ArgType> void deallocateCheckUno (ArgType* p, bool check=true)
198 {
199  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
200  else delete p;
201 }
202 
203 // *** DEALLOCATE field **P ***
204 template <class ArgType> void deallocateCheck (ArgType** p, long a, bool check=true)
205 {
206  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
207  else { while (a--) deallocateCheck (p[a], check); delete [] p; }
208 }
209 template <class ArgType> void deallocateCheckUno (ArgType** p, long a, bool check=true)
210 {
211  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
212  else { while (a--) deallocateCheckUno (p[a], check); delete [] p; }
213 }
214 
215 // *** DEALLOCATE field ***P ***
216 template <class ArgType> void deallocateCheck (ArgType*** p, long a, long b, bool check=true)
217 {
218  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
219  else { while (a--) deallocateCheck (p[a], b, check); delete [] p; }
220 }
221 template <class ArgType> void deallocateCheckUno (ArgType*** p, long a, long b, bool check=true)
222 {
223  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
224  else { while (a--) deallocateCheckUno (p[a], b, check); delete [] p; }
225 }
226 template <class ArgType> void deallocateCheck (ArgType*** p, long a, long *b, bool check=true)
227 {
228  if (p == NULL) { if (check) _warningg ("Field for deallocation is NULL"); }
229  else { while (a--) deallocateCheck (p[a], b[a], check); delete [] p; }
230 }
231 
232 
236 //memset (lid,-1,NumDomains*sizeof(long));
237 //
238 template <class ArgType> void fill_all_by (ArgType* p, long a, ArgType value) { while (a--) p[a] = value; }
239 template <class ArgType> void fill_all_by (ArgType** p, long a, long b, ArgType value) { while (a--) fill_all_by (p[a], b, value); }
240 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); }
241 
242 
243 template <class ArgType> void fill_row_by (ArgType** p, long i, long m, ArgType value) { fill_all_by (p[i], m, value); }
244 template <class ArgType> void fill_col_by (ArgType** p, long n, long j, ArgType value) { while (n--) p[n][j] = value; }
245 
246 
251 template <class ArgType> void allocate_another_val (long n, ArgType *&old_field)
252 {
253  if (!n && old_field) _errorr ("allocate_another_val: n == 0 and old_field != NULL - forbidden");
254  ArgType *new_field = new ArgType [n+1];
255  while (n--) new_field[n] = old_field[n];
256  delete [] old_field;
257  old_field = new_field;
258  new_field=NULL;
259 }
260 
262 template <class ArgType> void allocate_another_uno_ptr (long n, ArgType **&old_field, long add=1)
263 {
264  ArgType **new_field = new ArgType* [n+add];
265  while (n--) new_field[n] = old_field[n];
266  delete [] old_field;
267  old_field = new_field;
268  new_field=NULL;
269 }
270 
272 template <class ArgType> void setadd_another_uno_ptr (long &n, ArgType **&field, ArgType *p)
273 {
274  allocate_another_uno_ptr (n, field);
275  field[n++] = p;
276 }
277 
278 
283 template <class ArgType> void shake_down_ptr_array (long &n, ArgType *array)
284 {
285  long i,j;
286 
287  for (j=0; j<n; j++)
288  if (array[j] == NULL) break;
289 
290  for (i=j+1; i<n; i++)
291  if (array[i]) {
292  array[j++] = array[i];
293  array[i] = NULL;
294  }
295 
296  n=j;
297 }
298 
300 template <class ArgType> long arrays_arenot_identical (long n, ArgType *a1, ArgType *a2)
301 {
302  if (a1 && a2) {
303  while (n--) if (a1[n] != a2[n]) break;
304  return n+1;
305  }
306  if (a1 == a2 && !n) return 0;
307  else return -1;
308 }
309 
311 template <class ArgType> long is_member_of_array (ArgType val, long n, const ArgType *array)
312 {
313  while (n--) if (val == array[n]) break;
314  return n+1;
315 }
316 
317 
319 template <class ArgType> bool members_are_unique (long n, const ArgType *array)
320 {
321  long i,j;
322 
323  for (i=0; i<n-1; i++)
324  for (j=i+1; j<n; j++)
325  if (array[i] == array[j]) return false;
326 
327  return true;
328 }
329 
330 
333 template <class ArgType> bool same_array_elements_asym (long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
334 {
335  if (same_dim && nx!=ny) return false;
336 
337  long i,j;
338 
339  for (i=0; i<nx; i++) {
340  for (j=0; j<ny; j++)
341  if (x[i] == y[j]) break;
342 
343  if (j==ny) return false;
344  }
345 
346  return true;
347 }
348 
353 template <class ArgType> bool same_array_elements_sym (long nx, const ArgType *x, long ny, const ArgType *y, bool same_dim=false)
354 {
355  if (! same_array_elements_asym(nx, x, ny, y, same_dim) ) return false;
356  if (! same_array_elements_asym(ny, y, nx, x, same_dim) ) return false;
357  return true;
358 }
359 
360 
361 
362 
364 template <class ArgType> void add_array (long &n1, ArgType *&array1, long n2, const ArgType *array2)
365 {
366  if (n2 == 0) return;
367 
368  ArgType *new_array = new ArgType [n1+n2];
369 
370  long i;
371  for (i=0; i<n1; i++) new_array[ i] = array1[i];
372  for (i=0; i<n2; i++) new_array[n1+i] = array2[i];
373 
374  delete [] array1;
375  array1 = new_array;
376  new_array = NULL;
377  n1 = n1 + n2;
378 }
380 template <class ArgType> void add_array_unique (long &n1, ArgType *&array1, long n2, const ArgType *array2)
381 {
382  long i, n3 = 0;
383  for (i=0; i<n2; i++) if (!is_member_of_array (array2[i], n1, array1)) n3++;
384 
385  if (n3 == 0) return;
386 
387  ArgType *new_array = new ArgType [n1+n3];
388 
389  n3 = 0;
390  for (i=0; i<n1; i++) new_array[n3++] = array1[i];
391  for (i=0; i<n2; i++) if (!is_member_of_array (array2[i], n1, array1)) new_array[n3++] = array2[i];
392 
393  delete [] array1;
394  array1 = new_array;
395  new_array = NULL;
396  n1 = n3;
397 }
398 
400 template <class ArgType> void interchange2 (ArgType &a, ArgType &b)
401 {
402  ArgType aux;
403  aux = b;
404  b = a;
405  a = aux;
406 }
407 
408 
409 } // namespace midaspace
410 
411 #endif // MIDAS_GENERAL_H
void change_console_colour(FILE *stream, ETCLR clr)
Definition: gelib.h:129
void errorr(const char *file, int line, const char *format,...)
*** *** *** *** ERROR FCE *** *** *** ***
Definition: gelib.cpp:10
#define TC_D_BLUE
Definition: gelib.h:77
#define CHANGE_CONSOLE_COLOUR(_1, _2)
Definition: gelib.h:126
#define _warningg(_1)
Definition: gelib.h:154
#define TC_D_RED
Definition: gelib.h:71
void interchange2(ArgType &a, ArgType &b)
Definition: gelib.h:400
void fill_row_by(ArgType **p, long i, long m, ArgType value)
Definition: gelib.h:243
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 'x' is in the array 'y'.
Definition: gelib.h:333
#define TC_D_VIOLET
Definition: gelib.h:79
#define TC_B_CYAN
Definition: gelib.h:80
#define TC_B_YELLOW
Definition: gelib.h:74
long arrays_arenot_identical(long n, ArgType *a1, ArgType *a2)
Definition: gelib.h:300
void deallocateCheckUno(ArgType *p, bool check=true)
Definition: gelib.h:197
#define TC_D_YELLOW
Definition: gelib.h:75
#define TC_B_VIOLET
Definition: gelib.h:78
long is_member_of_array(ArgType val, long n, const ArgType *array)
check out "val" is member of "array"
Definition: gelib.h:311
#define TC_D_BLACK
Definition: gelib.h:69
#define TC_B_RED
Definition: gelib.h:70
#define TC_B_BLUE
Definition: gelib.h:76
void allocate_another_val(long n, ArgType *&old_field)
*** *** *** *** REALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:251
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
FILE * openFileN(const char *File, int Line, const char *mode, const char *key, const char *name)
Definition: gelib.cpp:67
ETCLR
enum
Definition: gelib.h:89
bool members_are_unique(long n, const ArgType *array)
check out all members of array "a" are uniq
Definition: gelib.h:319
void openFileTest(const FILE *stream, const char *name)
*** *** *** *** FILE FCE *** *** *** ***
Definition: gelib.cpp:61
#define TC_D_GREEN
Definition: gelib.h:73
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 'x' is in the array 'y', and symmetrically ever...
Definition: gelib.h:353
#define TC_B_BLACK
*** *** *** *** COLOURS *** *** *** *** Termit Colours
Definition: gelib.h:68
#define TC_B_GREEN
Definition: gelib.h:72
const char * trmt_clr_int2clr(ETCLR clr)
Definition: gelib.h:97
#define TC_D_WHITE
Definition: gelib.h:83
void shake_down_ptr_array(long &n, ArgType *array)
*** *** *** *** VARIOUS TEMPLATES *** *** *** ***
Definition: gelib.h:283
#define _errorr(_1)
Definition: gelib.h:151
#define TC_DEFAULT
Definition: gelib.h:85
#define TC_D_CYAN
Definition: gelib.h:81
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
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_WHITE
Definition: gelib.h:82
void fill_col_by(ArgType **p, long n, long j, ArgType value)
Definition: gelib.h:244
void add_array(long &n1, ArgType *&array1, long n2, const ArgType *array2)
Definition: gelib.h:364
void allocate(ArgType *&p, long a)
*** *** *** *** ALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:183
void setadd_another_uno_ptr(long &n, ArgType **&field, ArgType *p)
Definition: gelib.h:272
void deallocateCheck(ArgType *p, bool check=true)
*** *** *** *** DEALLOCATE TEMPLATES *** *** *** ***
Definition: gelib.h:192
void add_array_unique(long &n1, ArgType *&array1, long n2, const ArgType *array2)
Definition: gelib.h:380
void allocate_another_uno_ptr(long n, ArgType **&old_field, long add=1)
Definition: gelib.h:262
void warningg(const char *file, int line, const char *format,...)
Definition: gelib.cpp:26
void fill_all_by(ArgType *p, long a, ArgType value)
*** *** *** *** FILL TEMPLATES *** *** *** ***
Definition: gelib.h:238