-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.h
70 lines (49 loc) · 2.09 KB
/
macro.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Simple Educational Collection of Macros for learning C Language */
#ifndef MACRO_H
#define MACRO_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define STDIN 0
#define STDOUT 1
#define STDERR 2
#define IFERROR(s,m) if((s)==-1) {perror(m); exit(errno);}
#define IFERROR2(s,m,c) if((s)==-1) {perror(m); c;}
#define WRITE(m) IFERROR(write(STDOUT,m,strlen(m)), m);
#define WRITELN(m) WRITE(m);WRITE("\n");
/* WHENERROR for critical error */
#define WHENERROR(c) do{ if((c)<0){ printf("Errore in [ %s : %d]\n",__FILE__,__LINE__); }} while(0)
/* SIGERROR for signal related critical error */
#define SIGERROR(c, a) do{ if((c)<0){ printf("Errore in [%s : %d] gestendo ",__FILE__,__LINE__); printf(a);\
kill(0,SIGKILL);}}while(0)
/* for error in a fork */
#define WHENFORK(c) do{ if((c)<0){ printf("Errore in [%s : %d] tentando una fork ",__FILE__,__LINE__);\
kill(0,SIGKILL);}}while(0)
/* TRACE critical error */
#define TRACE(a) do{printf ("TRACE : %s - %u\n", __FILE__, __LINE__); printf (a); fflush (stdout);\
kill(0, SIGKILL);}while(0)
/* Three Array Lenght example */
#define ArrayLen(array) (sizeof(array)/sizeof(array[0]))
#define ArrayLength(array) (sizeof(array) / sizeof (array[0]))
#define ArrayL(array) (sizeof(array) / sizeof(*array))
/* direct memory access $122: addr(0x0122) = 1; */
#define SetAddr(_ind) (*(unsigned char*) (_ind))
/* move an element */
#define MoveTo(i, n) for((i) = 0; (i) < (n); (i)++)
/* Autoesplicative */
#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)>(b)?(a):(b))
#define Square(x) ((x)*(x))
/* a->b <-> !(a Or B) */
#define ARROW(a, b) (!(a) || (b))
#define COMPARE(x, y) (((x) > (y)) - ((x) < (y)))
#define SIGN(x) COMPARE(x, 0)
/* swp parameters SWAP(a,b int) */
#define swap(a, b, Type) do { Type temp = (a); (a) = (b); (b) = temp; } while(0)
/* parametric Sort */
#define SORTYPE(a, b, T) do { if ((a) > (b)) swap((a), (b), T); } while (0)
#define SET_TO(a, b, c) do{ size_t i_, b_; for (b_ = (b), i_ = 0; b_ > 0; --b_, ++i_) (a)[i_] = (c); } while(0)
#define SET_TO_ZERO(a, b) SET_TO(a, b, 0)
#endif