Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

csuCommonUtil.h

Go to the documentation of this file.
00001 /*
00002  *  csuUtilities.h
00003  *  GaborGUI
00004  *
00005  *  Created by David  Bolme on Sun Jun 09 2002.
00006  *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
00007  *
00008  */
00009 
00010 #ifndef CSU_COMMON_UTIL_INCLUDED
00011 #define CSU_COMMON_UTIL_INCLUDED
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <math.h>
00017 
00018 /*
00019  *  This file was created to contian simple commonly used macros
00020  *  and functions so that they don't have to be rewritten at every
00021  *  use.
00022  */ 
00023 
00024 /******************************************************************************
00025 *                           GLOBAL VARIABLES AND CONSTANTS                    *
00026 ******************************************************************************/
00027 
00028 #include <assert.h>
00029 
00030 
00031 namespace csu{
00032   
00034 #define FTYPE double
00035   
00037 #define FILE_LINE_LENGTH 512
00038   
00040 #define MAX_FILENAME_LENGTH 2048
00041   
00042 /******************************************************************************
00043 *                           NUMERICAL MACROS                                  *
00044 ******************************************************************************/
00045   
00046 /*************** Commonly used numerical macros ***************/
00047 
00048 #define MIN(v1, v2)             ( ((v1) > (v2)) ? (v2) : (v1) )
00049 #define MAX(v1, v2)             ( ((v1) < (v2)) ? (v2) : (v1) )
00050 #define ABS(v)                  ( ((v) < 0)     ? -(v) : (v)  )
00051 #define SQR(v)                  ( (v) * (v) )
00052 #define ROUND(v)                ( (int) ((v)+0.5) )
00053 #define TRUNC(v)                ( (int) (v) )
00054 #define EQUAL_ZERO(v,tol)       ( ABS(v) < tol )
00055 #define DIST_2D(x1, y1, x2, y2) (sqrt(SQR((x1)-(x2))+SQR((y1)-(y2)))) 
00056 #define FINITE(n) if(!finite(n)) printf("ERROR: Not Finite - %10s:%5f <%10s,%10s,%3d>\n",#n,n,__FILE__,__FUNCTION__,__LINE__);
00057 
00058 #define PI          3.141592654
00059 
00060 #define RANDOM   (fabs(((double)random())/RAND_MAX))
00061 
00062 /*************** Commonly used utility macros ****************/
00063 
00066 #define ALLOCATE( type , number ) ( (type*)csu_allocate(#type, sizeof(type),(number), __FILE__, __LINE__) )
00067 void* csu_allocate(const char*, int sof, int num, const char* file, int line);
00068 
00070 #define NELEM(v) (sizeof(v)/sizeof(v[0]))
00071 
00072 /*************** Commonly used string macros *****************/
00073 /*#define STRING_TO_DOUBLE(s, d) {                               \
00074     char* test;                                                \
00075     (d) = strtod((s),&test);                                   \
00076     if(test == (s)){                                           \
00077         printf("Error converting string to double: %s",(s));   \
00078         exit(1);                                               \
00079     }                                                         \
00080 }*/
00081 
00082 /*#define STRING_TO_INT(s, i) {                                 \
00083     char* test;                                               \
00084     (i) = strtod((s),&test);                                  \
00085     if(test == (s)){                                          \
00086         printf("Error converting string to int: %s",(s));     \
00087         exit(1);                                              \
00088     }                                                         \
00089 }*/
00090 
00091 /******************************************************************************
00092 *                           TOKENIZER OBJECT                                  *
00093 ******************************************************************************/
00094 
00095 typedef int (*TokenizerDataSourceProc) (char *buff, size_t nChars,
00096                                         void **arg);
00097 
00098 typedef struct
00099 {
00100   int state;
00101   char in_buff[512];
00102   char *i_ptr;
00103   char word_buff[100];
00104   char *w_ptr;
00105   int endOfLine;
00106   int endOfFile;
00107   int availChars;
00108   TokenizerDataSourceProc dataSource;
00109   void *dataSrcArg;
00110 }
00111 Tokenizer;
00112 
00116 void tokenizerInit (Tokenizer * tok, TokenizerDataSourceProc source, void *arg);
00117 char *tokenizerGetWord (Tokenizer * tok);
00118 int tokenizerEndOfFile (Tokenizer * tok);
00119 int tokenizerEndOfLine (Tokenizer * tok);
00120 void tokenizerTestSuite (Tokenizer * tok);
00121 
00125 int tokenizerStreamReader (char *buff, size_t nChars, void **arg);
00126 int tokenizerStringReader (char *buff, size_t nChars, void **arg);
00127 
00128 /******************************************************************************
00129 *                           UTILITY FUNCTIONS                                 *
00130 ******************************************************************************/
00131 
00134 void listAccumulate (void **base, void *data, size_t size);
00135 void * listToArray (void **base, size_t size, size_t *nelems);
00136 void * listToNullTerminatedArray (void **base, size_t size, size_t *nelems);
00137 
00140 void writeProgress (char *what, int value, int max);
00141 
00143 int isMachineLittleEndian();
00144 
00145 void byteswap_4(void *data, const int numfourbyteelements);
00146 
00151 char *strconc(const char *s1, const char *s2);
00152 char *strclone(const char *si);
00153 char *strlower(char *x);
00154 char *strupper(char *x);
00155 char *newextlong(char **filename, const char *extension);
00156 
00157 /******************************************************************************
00158 *                            DEBUGGING ROUTINES                               *
00159 ******************************************************************************/
00160 
00161 #include <time.h>
00162 #include <stdio.h>
00163 extern int debuglevel;
00164 extern int quiet;
00165 
00166 #define MESSAGE( message ) \
00167     { \
00168         time_t tttt = time(0); \
00169         if(quiet == 0 || debuglevel != 0) printf(message); \
00170         if(quiet == 0 || debuglevel != 0) printf("\n"); \
00171         if(debuglevel > 0) printf("    [%s]<%s():line %u> %s\n", __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00172         fflush(stdout); \
00173     }
00174 
00175 #define MESSAGE1ARG( message, arg1 ) \
00176     { \
00177         time_t tttt = time(0); \
00178         if(quiet == 0 || debuglevel != 0) printf(message, arg1); \
00179         if(quiet == 0 || debuglevel != 0) printf("\n"); \
00180         if(debuglevel > 0) printf("    [%s]<%s():line %u> %s\n", __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00181         fflush(stdout); \
00182     }
00183 
00184 #define MESSAGE2ARG( message, arg1, arg2 ) \
00185     { \
00186         time_t tttt = time(0); \
00187         if(quiet == 0 || debuglevel != 0) printf(message, arg1, arg2); \
00188         if(quiet == 0 || debuglevel != 0) printf("\n"); \
00189         if(debuglevel > 0) printf("    [%s]<%s():line %u> %s\n", __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00190         fflush(stdout); \
00191     }
00192 
00193 
00194 #define DEBUG( level , message ) \
00195     if( level == -1 || level <= debuglevel ){ \
00196         time_t tttt = time(0); \
00197         printf( "**DEBUG %d** \"%s\"\n             at [%s]<%s():line %u>\n             %s\n", level, message , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00198         fflush(stdout); \
00199     }
00200 
00201 #define DEBUG_FLOAT( level , message , value) \
00202     if( level == -1 || level <= debuglevel ){\
00203         time_t tttt = time(0); \
00204         printf( "**DEBUG %d** \"%s\" %s=%f\n             at [%s]<%s():line %u>\n             %s\n", level, message, #value , value , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00205         fflush(stdout);\
00206     }
00207 
00208 #define DEBUG_DOUBLE( level , message , value) \
00209     if( level == -1 || level <= debuglevel ){\
00210         time_t tttt = time(0); \
00211         printf( "**DEBUG %d** \"%s\" %s=%f\n             at [%s]<%s():line %u>\n             %s\n", level, message, #value , value , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt)); \
00212         fflush(stdout); \
00213     }
00214 
00215 #define DEBUG_INT( level , message , value) \
00216     if( level == -1 || level <= debuglevel ){\
00217         time_t tttt = time(0); \
00218         printf( "**DEBUG %d** \"%s\" %s=%d\n             at [%s]<%s():line %u>\n             %s\n", level, message, #value , value , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt));\
00219         fflush(stdout); \
00220     }
00221 
00222 #define DEBUG_STRING( level , message , value) \
00223     if( level == -1 || level <= debuglevel ){\
00224         time_t tttt = time(0); \
00225         printf( "**DEBUG %d** \"%s\" %s=\"%s\"\n             at [%s]<%s():line %u>\n             %s\n", level, message , #value,value , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt));\
00226         fflush(stdout);\
00227     }
00228 
00229 #define DEBUG_CHECK( condition , message) \
00230 if( !(condition) ){\
00231     time_t tttt = time(0); \
00232         printf( "**DEBUG CHECK** Failed check (%s) \"%s\"\n             at [%s]<%s():line %u>\n             %s\n", #condition, message , __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt));\
00233             fflush(stdout); \
00234                 exit(1);\
00235 }
00236 
00237 #define DEBUG_CHECK_1ARG( condition , message, arg) \
00238 if( !(condition) ){\
00239     time_t tttt = time(0); \
00240         printf( "**DEBUG CHECK** Failed check (%s) \"",#condition);\
00241             printf( message, arg);\
00242                 printf("\"\n             at [%s]<%s():line %u>\n             %s\n", __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt));\
00243                     fflush(stdout); \
00244                         exit(1);\
00245 }
00246 
00247 #define DEBUG_CHECK_2ARG( condition , message, arg1, arg2 ) \
00248 if( !(condition) ){\
00249     time_t tttt = time(0); \
00250     printf( "**DEBUG CHECK** Failed check (%s) \"",#condition);\
00251     printf( message, arg1, arg2);\
00252     printf("\"\n             at [%s]<%s():line %u>\n             %s\n", __FILE__ , __FUNCTION__ , __LINE__, ctime(&tttt));\
00253     fflush(stdout); \
00254     exit(1);\
00255 }
00256 
00257 #define NOT_IMPLEMENTED  DEBUG_CHECK(0,"This function has not yet been implemented");
00258 
00259 }
00260 #endif /*CSU_COMMON_UTIL_INCLUDED*/
00261 

Generated on Wed Apr 23 10:42:33 2003 for BioFilter by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002