• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/helper.c

Go to the documentation of this file.
00001 /* helper.c
00002  *
00003  */
00004 #include "helper.h"
00005 
00006 #include <stdarg.h>
00007 #include <string.h> /* memcpy */
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <gsl/gsl_statistics.h>
00012 #include <gsl/gsl_sort.h>
00013 #include <limits.h>
00014 #include <float.h>
00015 #include <stdint.h>
00016 #include <ctype.h>
00017 ProgressBarStatus progress_status;
00018 /* ---------------------------------------------------------------------------- 
00019    -- Helper functions                                                       -- 
00020    ---------------------------------------------------------------------------- */
00021 char*    create_string( const char *string ){
00022   char *r;
00023   r = (char*) malloc( (strlen( string )+1)*sizeof(char) );
00024   strcpy( r, string );
00025   return r;
00026 }
00027 
00030 bool  isin_intarray( const int *a, int n, int val ){
00031   int i;
00032   for( i=0; i<n; i++ ){
00033      if( val==a[i] )
00034         return TRUE;
00035   }
00036   return FALSE;
00037 }
00038 
00042 int safer_free( void *p ){
00043   if( p ){
00044      free( p );
00045      return 0;
00046   } else {
00047      return 1;
00048   }
00049 }
00050 
00053 int randint( int from, int to ){
00054   return ((int)(drand48() * to))+from;
00055 }
00056 
00057 
00060 double** copy_double_ptrptr(const double **s, int N, int n){
00061   double **r;
00062   int i, j;
00063 
00064   r = (double**)malloc(N*sizeof(double*));
00065   for(i=0; i<N; i++){
00066     r[i] = (double*)malloc(n*sizeof(double));
00067      for(j=0; j<n; j++){
00068         r[i][j] = s[i][j];
00069      }
00070   }
00071   return r;
00072 }
00073 
00077 int stream_count_char( FILE* f, char c ){
00078   long curpos;
00079   int numchar=0;
00080   curpos = ftell( f );
00081   while( (c=fgetc( f ))!=EOF ){
00082      if( c=='\n' ) numchar++;
00083   }
00084 
00085   fseek( f, curpos, SEEK_SET );
00086 
00087   return numchar;
00088 }
00089 
00092 size_t  ffread(void *ptr, size_t size, size_t nmemb, FILE *stream){
00093   if( fread(ptr, size, nmemb, stream) != nmemb )
00094      return 0;
00095   else 
00096      return 1;
00097 }
00098 
00099 size_t ffwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
00100   size_t bwrite;
00101   /*  dprintf("size=%i, nmemb=%i\n", size, nmemb);*/
00102   bwrite=fwrite(ptr, size, nmemb, stream);
00103   /*  dprintf("feof=%i, ferror=%i\n", feof(stream), ferror(stream));
00104         dprintf("bread=%i, exp=%i\n", bread, size*nmemb);*/
00105   if(bwrite<nmemb){
00106      errprintf("Could not write to output file!\n");
00107   }
00108   return bwrite;
00109 }
00110 
00111 
00112 void    qsort_int_index( int *idx_idx, const int *idx, int n ){
00113   int i,j;
00114   int *tmp;
00115   tmp = (int*)malloc( n*sizeof( int ) );
00116   memcpy( tmp, idx, n*sizeof( int ) );
00117 
00118   qsort( tmp, n, sizeof( int ), compare_ints);
00119   for(i=0; i<n; i++){
00120      for(j=0; j<n; j++){
00121         if( idx[i] == tmp[j] ){
00122           idx_idx[i]=j;
00123           tmp[j]=INT_MIN;
00124           break;
00125         }
00126      }
00127   }
00128   free(tmp);
00129 }
00130 
00131 
00132 /* well tested */
00133 void swap_bytes(void *ptr, int nmemb){
00134   uint8_t tmp;
00135   uint8_t *nptr=(uint8_t*)ptr;
00136   int i;
00137 
00138   for(i=0; i<nmemb/2; i++){
00139      tmp = nptr[i];
00140      nptr[i]=nptr[nmemb-i-1];
00141      nptr[nmemb-i-1]=tmp;
00142   }
00143 }
00144 
00145 int is_little_endian(){
00146   long l=1; 
00147   void *ptr=&l; 
00148   uint8_t t =*(uint8_t*)ptr;
00149   if(t==1){
00150      return 0;
00151   } else if(t==0){ 
00152      return 1;
00153   } else {
00154      errormsg(ERR_ENDIAN, ERR_FATAL);
00155   }
00156   return 0;
00157 }
00158 
00159 int compare_ints (const void *a, const void *b) {
00160   int *i1, *i2;
00161   i1=(int*)a;
00162   i2=(int*)b;
00163   if (*i1 > *i2)
00164      return 1;
00165   else if (*i1 < *i2)
00166      return -1;
00167   else
00168      return 0;
00169 }
00170 
00172 void wswap(void *ptr, int nmemb, int flag){
00173   if(flag)
00174      swap_bytes(ptr, nmemb);
00175 }
00176 int vprint_vector(const char* name, double *v, int n){
00177   int i;
00178   fprintf(stderr, "%s = \n", name);
00179   for(i=0; i<n; i++)
00180     fprintf(stderr, " %2.2f ", v[i]);
00181   fprintf(stderr, "\n");
00182   return 0;
00183 }
00184 
00185 
00186 void   errormsg(int err_no, int fatal){
00187   switch(err_no){
00188   case ERR_IO:
00189     errprintf("IO Error\n");
00190     break;
00191   case ERR_GSL:
00192     errprintf("Error in the GSL-library\n");
00193     break;
00194   case ERR_PLOT:
00195     errprintf("Error in the Plot-library\n");
00196     break;
00197   case ERR_ENDIAN:
00198     errprintf( "Error in the Endianness\n");
00199     break;
00200   case ERR_PARSEMAT:
00201     errprintf("Error while parsing .mat-file! Continue at your own risk...\n");
00202     break;
00203   default:
00204     errprintf("Unknown Error number\n");
00205   }
00206   if(fatal){
00207     errprintf("... Fatal\n");
00208     exit(err_no);
00209   } else errprintf("\n");
00210 }
00211 
00212 
00213 
00219 int strcount( const char *s, char c ){
00220   int i, count=0;
00221   for( i=0; i<strlen(s); i++ ){
00222      if( s[i]==c ) count++;
00223   }
00224   return count;
00225 }
00226 
00227 
00236 void progressbar_rotating( int flag, ulonglong num ){
00237   ulonglong c,i;
00238   FILE *out;
00239 
00240   out = stderr; 
00241   switch(flag){
00242   case PROGRESSBAR_INIT:
00243      progress_status.max_progress = num;
00244      progress_status.cur_progress = 0;
00245      progress_status.prev_progress= 0;
00246      fprintf( out, "[ " );
00247      for( i=0; i<PROGRESSBAR_NUMCOLS; i++ ){
00248         fprintf( out, " " );
00249      }
00250      fprintf( out, " ]" );
00251      for( i=0; i<PROGRESSBAR_NUMCOLS+2; i++ ){
00252         fprintf( out, "\b" );
00253      }
00254      break;
00255   case PROGRESSBAR_CONTINUE_LONG:
00256      c=PROGRESSBAR_NUMCOLS*((double)num/(double)progress_status.max_progress);
00257      while( c>progress_status.cur_progress ){
00258         fprintf( out, "#" );
00259         progress_status.cur_progress++;
00260      }
00261      break;
00262   case PROGRESSBAR_CONTINUE_SHORT:
00263      c = (progress_status.prev_progress++ % 4);
00264      switch(c){
00265      case 0: c = '/'; break;
00266      case 1: c = '-'; break;
00267      case 2: c = '\\'; break;
00268      case 3: c = '|'; break;
00269      }
00270      fprintf( out, "%c", c);
00271      fprintf( out, "\b" );
00272      break;
00273   case PROGRESSBAR_FINISH:
00274      fprintf( out, "\n");
00275      break;
00276   } /* switch */
00277 }
00278 
00279 
00280 
00284 void     string_strip_blanks( char *s ){
00285   int i, j, n;
00286 
00287   n = strlen(s);
00288   for( i=0; i<n; i++ ){       /* n+1 because of '\0' byte */
00289      if( isspace( s[i] ) ){
00290         for( j=i; j<n; j++ ){
00291           s[j]=s[j+1];
00292         }
00293         i--;
00294      }
00295 
00296   }
00297 }

Generated on Fri Jun 25 2010 14:10:18 for libeegtools by  doxygen 1.7.0