Menu

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

Download this file

298 lines (263 with data), 6.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* helper.c
*
*/
#include "helper.h"
#include <stdarg.h>
#include <string.h> /* memcpy */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_statistics.h>
#include <gsl/gsl_sort.h>
#include <limits.h>
#include <float.h>
#include <stdint.h>
#include <ctype.h>
ProgressBarStatus progress_status;
/* ----------------------------------------------------------------------------
-- Helper functions --
---------------------------------------------------------------------------- */
char* create_string( const char *string ){
char *r;
r = (char*) malloc( (strlen( string )+1)*sizeof(char) );
strcpy( r, string );
return r;
}
/** checks whether val is in a.
*/
bool isin_intarray( const int *a, int n, int val ){
int i;
for( i=0; i<n; i++ ){
if( val==a[i] )
return TRUE;
}
return FALSE;
}
/** checks whether p is the NULL pointer before freeing.
\return 0 memory was freed, 1 no memory was freed
*/
int safer_free( void *p ){
if( p ){
free( p );
return 0;
} else {
return 1;
}
}
/** returns a random integer in the range [from, from+1, ..., to]
*/
int randint( int from, int to ){
return ((int)(drand48() * to))+from;
}
/** deep copy of double ptrptr
*/
double** copy_double_ptrptr(const double **s, int N, int n){
double **r;
int i, j;
r = (double**)malloc(N*sizeof(double*));
for(i=0; i<N; i++){
r[i] = (double*)malloc(n*sizeof(double));
for(j=0; j<n; j++){
r[i][j] = s[i][j];
}
}
return r;
}
/** return number of occurences of c in f.
f is rewound to current position
*/
int stream_count_char( FILE* f, char c ){
long curpos;
int numchar=0;
curpos = ftell( f );
while( (c=fgetc( f ))!=EOF ){
if( c=='\n' ) numchar++;
}
fseek( f, curpos, SEEK_SET );
return numchar;
}
/** \brief same as fread, just returning success or failure.
*/
size_t ffread(void *ptr, size_t size, size_t nmemb, FILE *stream){
if( fread(ptr, size, nmemb, stream) != nmemb )
return 0;
else
return 1;
}
size_t ffwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
size_t bwrite;
/* dprintf("size=%i, nmemb=%i\n", size, nmemb);*/
bwrite=fwrite(ptr, size, nmemb, stream);
/* dprintf("feof=%i, ferror=%i\n", feof(stream), ferror(stream));
dprintf("bread=%i, exp=%i\n", bread, size*nmemb);*/
if(bwrite<nmemb){
errprintf("Could not write to output file!\n");
}
return bwrite;
}
void qsort_int_index( int *idx_idx, const int *idx, int n ){
int i,j;
int *tmp;
tmp = (int*)malloc( n*sizeof( int ) );
memcpy( tmp, idx, n*sizeof( int ) );
qsort( tmp, n, sizeof( int ), compare_ints);
for(i=0; i<n; i++){
for(j=0; j<n; j++){
if( idx[i] == tmp[j] ){
idx_idx[i]=j;
tmp[j]=INT_MIN;
break;
}
}
}
free(tmp);
}
/* well tested */
void swap_bytes(void *ptr, int nmemb){
uint8_t tmp;
uint8_t *nptr=(uint8_t*)ptr;
int i;
for(i=0; i<nmemb/2; i++){
tmp = nptr[i];
nptr[i]=nptr[nmemb-i-1];
nptr[nmemb-i-1]=tmp;
}
}
int is_little_endian(){
long l=1;
void *ptr=&l;
uint8_t t =*(uint8_t*)ptr;
if(t==1){
return 0;
} else if(t==0){
return 1;
} else {
errormsg(ERR_ENDIAN, ERR_FATAL);
}
return 0;
}
int compare_ints (const void *a, const void *b) {
int *i1, *i2;
i1=(int*)a;
i2=(int*)b;
if (*i1 > *i2)
return 1;
else if (*i1 < *i2)
return -1;
else
return 0;
}
/** wrapper */
void wswap(void *ptr, int nmemb, int flag){
if(flag)
swap_bytes(ptr, nmemb);
}
int vprint_vector(const char* name, double *v, int n){
int i;
fprintf(stderr, "%s = \n", name);
for(i=0; i<n; i++)
fprintf(stderr, " %2.2f ", v[i]);
fprintf(stderr, "\n");
return 0;
}
void errormsg(int err_no, int fatal){
switch(err_no){
case ERR_IO:
errprintf("IO Error\n");
break;
case ERR_GSL:
errprintf("Error in the GSL-library\n");
break;
case ERR_PLOT:
errprintf("Error in the Plot-library\n");
break;
case ERR_ENDIAN:
errprintf( "Error in the Endianness\n");
break;
case ERR_PARSEMAT:
errprintf("Error while parsing .mat-file! Continue at your own risk...\n");
break;
default:
errprintf("Unknown Error number\n");
}
if(fatal){
errprintf("... Fatal\n");
exit(err_no);
} else errprintf("\n");
}
/** count the number of occurences of c in s
\param s - the string (\0 terminated)
\param c - the character
\return count( s==c )
*/
int strcount( const char *s, char c ){
int i, count=0;
for( i=0; i<strlen(s); i++ ){
if( s[i]==c ) count++;
}
return count;
}
/** displays a progress-bar like this
\code
[ #####\ ]
\endcode
\param flag one of PROGRESSBAR_INIT, PROGRESSBAR_CONTINUE_LONG,
PROGRESSBAR_CONTINUE_SHORT
\param num meaning depends on flag
*/
void progressbar_rotating( int flag, ulonglong num ){
ulonglong c,i;
FILE *out;
out = stderr;
switch(flag){
case PROGRESSBAR_INIT:
progress_status.max_progress = num;
progress_status.cur_progress = 0;
progress_status.prev_progress= 0;
fprintf( out, "[ " );
for( i=0; i<PROGRESSBAR_NUMCOLS; i++ ){
fprintf( out, " " );
}
fprintf( out, " ]" );
for( i=0; i<PROGRESSBAR_NUMCOLS+2; i++ ){
fprintf( out, "\b" );
}
break;
case PROGRESSBAR_CONTINUE_LONG:
c=PROGRESSBAR_NUMCOLS*((double)num/(double)progress_status.max_progress);
while( c>progress_status.cur_progress ){
fprintf( out, "#" );
progress_status.cur_progress++;
}
break;
case PROGRESSBAR_CONTINUE_SHORT:
c = (progress_status.prev_progress++ % 4);
switch(c){
case 0: c = '/'; break;
case 1: c = '-'; break;
case 2: c = '\\'; break;
case 3: c = '|'; break;
}
fprintf( out, "%c", c);
fprintf( out, "\b" );
break;
case PROGRESSBAR_FINISH:
fprintf( out, "\n");
break;
} /* switch */
}
/** remove spaces from a string by "compressing" it.
for example, " this is a bla nk" becomes "thisisablank"
*/
void string_strip_blanks( char *s ){
int i, j, n;
n = strlen(s);
for( i=0; i<n; i++ ){ /* n+1 because of '\0' byte */
if( isspace( s[i] ) ){
for( j=i; j<n; j++ ){
s[j]=s[j+1];
}
i--;
}
}
}
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.