#ifndef UTILS_H #define UTILS_H /* o---------------------------------------------------------------------o | | Numdiff | | Copyright (c) 2012+ laurent.deniau@cern.ch | Gnu General Public License | o---------------------------------------------------------------------o Purpose: provides utilities o---------------------------------------------------------------------o */ #include #include #include // macros #if defined(_WIN32) || defined(NOCOLORS) #define CSTR_RED(s) s #define CSTR_GREEN(s) s #else #define CSTR_RED(s) "\033[31m" s "\033[0m" #define CSTR_GREEN(s) "\033[32m" s "\033[0m" #endif // extern functions FILE* open_indexedFile(const char* str, int idx, const char *ext, int optext, int required); void accum_summary(int total, int failed, long lines, long numbers); // inline functions #if !__STDC__ || __STDC_VERSION__ < 199901L static inline int isblank(int c) { return c == ' ' || c == '\t'; } static inline double fmin(double a, double b) { return a < b ? a : b; } static inline double fmax(double a, double b) { return a > b ? a : b; } #endif static inline int imin (int a, int b) { return ab ? a : b; } static inline double pow10(int i) { extern const double *const pow10_table99; return -100 < i && i < 100 ? pow10_table99[i] : pow(10, i); } // ----- public (read helpers) static inline int skipSpace (FILE *fp, int *i_) { int c = 0, i = 0; while ((c = getc(fp)) != EOF) { if (!isspace(c)) break; i++; } if (i_) *i_ = i; return c; } static inline int skipLine (FILE *fp, int *i_) { int c = 0, i = 0; while ((c = getc(fp)) != EOF) { if (c == '\n') break; // \n : Unix, Linux, MacOSX if (c == '\r') { if ((c = getc(fp)) != '\n') // \r\n : Windows ungetc(c, fp); // \r : Mac (old) c = '\n'; break; } i++; } if (i_) *i_ = i; return c; } static inline int readLine (FILE *fp, char *buf, int n, int *i_) { int c = 0, i = 0; while (i < n-1 && (c = getc(fp)) != EOF) { if (c == '\n') break; // \n : Unix, Linux, MacOSX if (c == '\r') { if ((c = getc(fp)) != '\n') // \r\n : Windows ungetc(c, fp); // \r : Mac (old) c = '\n'; break; } buf[i++] = c; } buf[i] = 0; if (i_) *i_ = i; return c; } #endif