Menu

[09bd5b]: / src / helper.h  Maximize  Restore  History

Download this file

209 lines (167 with data), 7.2 kB

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* **************************************************************************
* Copyright (C) 2008 by Matthias Ihrke *
* mihrke@uni-goettingen.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**\file helper.h
* \brief \ref status_stable Helper functions.
*/
#ifndef HELPER_H
# define HELPER_H
#include "definitions.h"
#include "escape_codes.h"
#include <stdio.h>
#ifdef HAVE_MATLAB
#ifndef MATLAB_STARTUP_CMD
#define MATLAB_STARTUP_CMD "/usr/local/matlab7/bin/matlab -nosplash"
/* #define MATLAB_STARTUP_CMD "/usr/nld/matlab-17/bin/matlab -nosplash" */
#endif
#define ML(code) code /* this macro deletes all ML() statements if
HAVE_MATLAB is not defined */
#include "engine.h"
#else
#define ML(code)
#endif
/** custom error stuff */
#define ERR_GSL -1
#define ERR_IO -2
#define ERR_PLOT -3
#define ERR_MEM -4
#define ERR_MATLAB -5
#define ERR_ENDIAN -6
#define ERR_PARSEMAT -7
#define ERR_FATAL 1
#define ERR_NOFATAL 0
#define NOSTRING " \b"
#define PROGRESSBAR_NUMCOLS 80 /**< number of columns for progress bar */
#define PROGRESSBAR_INIT 0 /**< initialize flag for progress bar */
#define PROGRESSBAR_CONTINUE_SHORT 1 /**< continue flag for progress bar, minor step */
#define PROGRESSBAR_CONTINUE_LONG 2 /**< continue flag for progress bar, major step */
#define PROGRESSBAR_FINISH 3 /**< cleanup */
/** \brief swap two variables.
*/
#define SWAPT( Type, a, b ) { Type tmp; tmp=(a); (a)=(b); (b)=(tmp); }
/*-----------------------------------------------------------
- Progress Bar
---------------------------------------------------------*/
/** \brief status of progress bar.
This struct is used as an external variable to
keep track of the progress bar. You only need
to be concerned about this, if you want to
write your own progressbar-function.
*/
typedef struct{
ulonglong max_progress;
ulonglong cur_progress;
ulonglong prev_progress;
} ProgressBarStatus;
/** \brief A progressbar-function.
*/
typedef void (*ProgressBarFunction) (int,ulonglong);
/** \brief convenience memory alloc macro.
use as follows:
\code
double *a;
MALLOC( a, 100, double );
double **b;
MALLOC( b, 100, double* );
\endcode
*/
#define MALLOC( ptr, nmemb, type ) \
ptr = (type*)malloc(nmemb*sizeof(type)); \
if( ptr==NULL ){ \
errprintf("Could not allocate memory\n"); \
}
/** debugging macros */
#ifdef DEBUG
/*#define dprintf(...) fprintf(stderr, ## __VA_ARGS__)*/
/** \brief debug-print.
This is only printed if the library is compiled with
the DEBUG flag.
*/
#define dprintf(...) do{ \
fprintf(stderr, ESCAPE_FGYELLOW_STR ESCAPE_BRIGHT_STR "%s (%i), %s(): ", \
__FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, ## __VA_ARGS__); \
fprintf(stderr, NOSTRING ESCAPE_RESET_STR); \
} while(0)
#define massert(x, text, ...) \
do{ \
if(x){ \
fprintf(stderr, ESCAPE_FGRED_STR "Assertion failed: " #text, ## __VA_ARGS__); \
fprintf(stderr, NOSTRING ESCAPE_RESET_STR); \
} \
} while(0)
#else
#define dprintf(...)
#define massert(...)
#endif
/** \brief print error.
Report an Error.
*/
#define errprintf(...) do{ fprintf(stderr, ESCAPE_FGRED_STR ESCAPE_BOLD_STR\
ESCAPE_BGYELLOW_STR "ERROR: %s (%i), %s(): ", \
__FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, ## __VA_ARGS__); \
fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
/** \brief print warning.
*/
#define warnprintf(...) do{ fprintf(stderr, ESCAPE_FGWHITE_STR ESCAPE_BOLD_STR \
ESCAPE_BGBLUE_STR "WARNING: %s (%i), %s(): ", \
__FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, ## __VA_ARGS__); \
fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
#define oprintf(...) do{ fprintf(stderr, ESCAPE_FGCYAN_STR ESCAPE_BRIGHT_STR \
"%s (%i), %s() --> ", \
__FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, ## __VA_ARGS__); \
fprintf(stderr, NOSTRING ESCAPE_RESET_STR); } while(0)
#ifdef __cplusplus
extern "C" {
#endif
/* ----------------------------------------------------------------------------
-- Helper functions (org) --
---------------------------------------------------------------------------- */
void errormsg(int err_no, int fatal);
void swap_bytes(void *ptr, int nmemb);
void wswap(void *ptr, int nmemb, int flag);
int is_little_endian();
bool isin_intarray( const int *a, int n, int val );
void qsort_int_index( int *idx_idx, const int *idx, int n );
int compare_ints (const void *a, const void *b);
int randint( int from, int to );
int strcount( const char *s, char c );
int safer_free( void *p );
char* create_string( const char *string );
double** copy_double_ptrptr(const double **s, int N, int n);
void string_strip_blanks( char *s );
/* ----------------------------------------------------------------------------
-- Helper functions (IO) --
---------------------------------------------------------------------------- */
int vprint_vector(const char* name, double *v, int n);
size_t ffread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t ffwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int stream_count_char( FILE* f, char c );
/* ----------------------------------------------------------------------------
-- Gobals --
---------------------------------------------------------------------------- */
extern ProgressBarStatus progress_status;
void progressbar_rotating( int flag, ulonglong num );
#ifdef __cplusplus
}
#endif
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.