1 | #ifndef UTILS_H |
---|
2 | #define UTILS_H |
---|
3 | |
---|
4 | /* |
---|
5 | o---------------------------------------------------------------------o |
---|
6 | | |
---|
7 | | Numdiff |
---|
8 | | |
---|
9 | | Copyright (c) 2012+ laurent.deniau@cern.ch |
---|
10 | | Gnu General Public License |
---|
11 | | |
---|
12 | o---------------------------------------------------------------------o |
---|
13 | |
---|
14 | Purpose: |
---|
15 | provides utilities |
---|
16 | |
---|
17 | o---------------------------------------------------------------------o |
---|
18 | */ |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <ctype.h> |
---|
22 | #include <math.h> |
---|
23 | |
---|
24 | // macros |
---|
25 | |
---|
26 | #if defined(_WIN32) || defined(NOCOLORS) |
---|
27 | #define CSTR_RED(s) s |
---|
28 | #define CSTR_GREEN(s) s |
---|
29 | #else |
---|
30 | #define CSTR_RED(s) "\033[31m" s "\033[0m" |
---|
31 | #define CSTR_GREEN(s) "\033[32m" s "\033[0m" |
---|
32 | #endif |
---|
33 | |
---|
34 | // extern functions |
---|
35 | |
---|
36 | FILE* open_indexedFile(const char* str, int idx, const char *ext, int optext, int required); |
---|
37 | void accum_summary(int total, int failed, long lines, long numbers); |
---|
38 | |
---|
39 | // inline functions |
---|
40 | |
---|
41 | #if !__STDC__ || __STDC_VERSION__ < 199901L |
---|
42 | static inline int |
---|
43 | isblank(int c) |
---|
44 | { |
---|
45 | return c == ' ' || c == '\t'; |
---|
46 | } |
---|
47 | |
---|
48 | static inline double |
---|
49 | fmin(double a, double b) |
---|
50 | { |
---|
51 | return a < b ? a : b; |
---|
52 | } |
---|
53 | |
---|
54 | static inline double |
---|
55 | fmax(double a, double b) |
---|
56 | { |
---|
57 | return a > b ? a : b; |
---|
58 | } |
---|
59 | #endif |
---|
60 | |
---|
61 | static inline int |
---|
62 | imin (int a, int b) |
---|
63 | { |
---|
64 | return a<b ? a : b; |
---|
65 | } |
---|
66 | |
---|
67 | static inline int |
---|
68 | imax (int a, int b) |
---|
69 | { |
---|
70 | return a>b ? a : b; |
---|
71 | } |
---|
72 | |
---|
73 | static inline double |
---|
74 | pow10(int i) |
---|
75 | { |
---|
76 | extern const double *const pow10_table99; |
---|
77 | return -100 < i && i < 100 ? pow10_table99[i] : pow(10, i); |
---|
78 | } |
---|
79 | |
---|
80 | // ----- public (read helpers) |
---|
81 | |
---|
82 | static inline int |
---|
83 | skipSpace (FILE *fp, int *i_) |
---|
84 | { |
---|
85 | int c = 0, i = 0; |
---|
86 | |
---|
87 | while ((c = getc(fp)) != EOF) { |
---|
88 | if (!isspace(c)) break; |
---|
89 | i++; |
---|
90 | } |
---|
91 | |
---|
92 | if (i_) *i_ = i; |
---|
93 | |
---|
94 | return c; |
---|
95 | } |
---|
96 | |
---|
97 | static inline int |
---|
98 | skipLine (FILE *fp, int *i_) |
---|
99 | { |
---|
100 | int c = 0, i = 0; |
---|
101 | |
---|
102 | while ((c = getc(fp)) != EOF) { |
---|
103 | if (c == '\n') break; // \n : Unix, Linux, MacOSX |
---|
104 | if (c == '\r') { |
---|
105 | if ((c = getc(fp)) != '\n') // \r\n : Windows |
---|
106 | ungetc(c, fp); // \r : Mac (old) |
---|
107 | c = '\n'; break; |
---|
108 | } |
---|
109 | i++; |
---|
110 | } |
---|
111 | |
---|
112 | if (i_) *i_ = i; |
---|
113 | |
---|
114 | return c; |
---|
115 | } |
---|
116 | |
---|
117 | static inline int |
---|
118 | readLine (FILE *fp, char *buf, int n, int *i_) |
---|
119 | { |
---|
120 | int c = 0, i = 0; |
---|
121 | |
---|
122 | while (i < n-1 && (c = getc(fp)) != EOF) { |
---|
123 | if (c == '\n') break; // \n : Unix, Linux, MacOSX |
---|
124 | if (c == '\r') { |
---|
125 | if ((c = getc(fp)) != '\n') // \r\n : Windows |
---|
126 | ungetc(c, fp); // \r : Mac (old) |
---|
127 | c = '\n'; break; |
---|
128 | } |
---|
129 | buf[i++] = c; |
---|
130 | } |
---|
131 | buf[i] = 0; |
---|
132 | |
---|
133 | if (i_) *i_ = i; |
---|
134 | |
---|
135 | return c; |
---|
136 | } |
---|
137 | |
---|
138 | #endif |
---|