00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024 #ifndef HELPER_H
00025 # define HELPER_H
00026
00027 #include "definitions.h"
00028 #include "escape_codes.h"
00029 #include <stdio.h>
00030
00031
00032 #ifdef HAVE_MATLAB
00033 #ifndef MATLAB_STARTUP_CMD
00034 #define MATLAB_STARTUP_CMD "/usr/local/matlab7/bin/matlab -nosplash"
00035
00036 #endif
00037 #define ML(code) code
00038
00039 #include "engine.h"
00040 #else
00041 #define ML(code)
00042 #endif
00043
00045 #define ERR_GSL -1
00046 #define ERR_IO -2
00047 #define ERR_PLOT -3
00048 #define ERR_MEM -4
00049 #define ERR_MATLAB -5
00050 #define ERR_ENDIAN -6
00051 #define ERR_PARSEMAT -7
00052 #define ERR_FATAL 1
00053 #define ERR_NOFATAL 0
00054
00055 #define NOSTRING " \b"
00056
00057 #define PROGRESSBAR_NUMCOLS 80
00058 #define PROGRESSBAR_INIT 0
00059 #define PROGRESSBAR_CONTINUE_SHORT 1
00060 #define PROGRESSBAR_CONTINUE_LONG 2
00061 #define PROGRESSBAR_FINISH 3
00065 #define SWAPT( Type, a, b ) { Type tmp; tmp=(a); (a)=(b); (b)=(tmp); }
00066
00067
00068
00069
00070
00071
00078 typedef struct{
00079 ulonglong max_progress;
00080 ulonglong cur_progress;
00081 ulonglong prev_progress;
00082 } ProgressBarStatus;
00083
00086 typedef void (*ProgressBarFunction) (int,ulonglong);
00087
00098 #define MALLOC( ptr, nmemb, type ) \
00099 ptr = (type*)malloc(nmemb*sizeof(type)); \
00100 if( ptr==NULL ){ \
00101 errprintf("Could not allocate memory\n"); \
00102 }
00103
00105 #ifdef DEBUG
00106
00107
00113 #define dprintf(...) do{ \
00114 fprintf(stderr, ESCAPE_FGYELLOW_STR ESCAPE_BRIGHT_STR "%s (%i), %s(): ", \
00115 __FILE__, __LINE__, __FUNCTION__); \
00116 fprintf(stderr, ## __VA_ARGS__); \
00117 fprintf(stderr, NOSTRING ESCAPE_RESET_STR); \
00118 } while(0)
00119
00120 #define massert(x, text, ...) \
00121 do{ \
00122 if(x){ \
00123 fprintf(stderr, ESCAPE_FGRED_STR "Assertion failed: " #text, ## __VA_ARGS__); \
00124 fprintf(stderr, NOSTRING ESCAPE_RESET_STR); \
00125 } \
00126 } while(0)
00127 #else
00128 #define dprintf(...)
00129 #define massert(...)
00130 #endif
00131
00136 #define errprintf(...) do{ fprintf(stderr, ESCAPE_FGRED_STR ESCAPE_BOLD_STR\
00137 ESCAPE_BGYELLOW_STR "ERROR: %s (%i), %s(): ", \
00138 __FILE__, __LINE__, __FUNCTION__); \
00139 fprintf(stderr, ## __VA_ARGS__); \
00140 fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
00141
00143 #define warnprintf(...) do{ fprintf(stderr, ESCAPE_FGWHITE_STR ESCAPE_BOLD_STR \
00144 ESCAPE_BGBLUE_STR "WARNING: %s (%i), %s(): ", \
00145 __FILE__, __LINE__, __FUNCTION__); \
00146 fprintf(stderr, ## __VA_ARGS__); \
00147 fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
00148
00149 #define oprintf(...) do{ fprintf(stderr, ESCAPE_FGCYAN_STR ESCAPE_BRIGHT_STR \
00150 "%s (%i), %s() --> ", \
00151 __FILE__, __LINE__, __FUNCTION__); \
00152 fprintf(stderr, ## __VA_ARGS__); \
00153 fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
00154
00155 #ifdef __cplusplus
00156 extern "C" {
00157 #endif
00158
00159
00160
00161
00162 void errormsg(int err_no, int fatal);
00163
00164 void swap_bytes(void *ptr, int nmemb);
00165 void wswap(void *ptr, int nmemb, int flag);
00166 int is_little_endian();
00167
00168 bool isin_intarray( const int *a, int n, int val );
00169
00170 void qsort_int_index( int *idx_idx, const int *idx, int n );
00171 int compare_ints (const void *a, const void *b);
00172
00173 int randint( int from, int to );
00174
00175 int strcount( const char *s, char c );
00176
00177 int safer_free( void *p );
00178
00179 char* create_string( const char *string );
00180
00181 double** copy_double_ptrptr(const double **s, int N, int n);
00182 void string_strip_blanks( char *s );
00183
00184
00185
00186
00187 int vprint_vector(const char* name, double *v, int n);
00188
00189 size_t ffread(void *ptr, size_t size, size_t nmemb, FILE *stream);
00190 size_t ffwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
00191
00192 int stream_count_char( FILE* f, char c );
00193
00194
00195
00196
00197
00198
00199
00200
00201 extern ProgressBarStatus progress_status;
00202 void progressbar_rotating( int flag, ulonglong num );
00203
00204 #ifdef __cplusplus
00205 }
00206 #endif
00207
00208 #endif