// (c) by Stefan Roettger, licensed under GPL 2+ #ifndef CODEBASE_H #define CODEBASE_H #include #include #include #include #include #include #include #include #if !defined WINOS && !defined MACOSX && !defined LINUX #if defined _WIN32 # define WINOS #elif defined __APPLE__ # define MACOSX #else # define LINUX #endif #endif #if defined(IRIX) || defined(LINUX) || defined(MACOSX) #define UNIX #endif #include #ifdef UNIX #include #endif #ifdef WINOS #include #include #endif #ifdef UNIX #include #include #endif #define BOOLINT char #ifndef TRUE #define TRUE (1) #endif #ifndef FALSE #define FALSE (0) #endif enum VR_ERROR { VR_ERROR_NONFATAL=0, VR_ERROR_FATAL=1, VR_ERROR_MEM=2, VR_ERROR_IO=3, VR_ERROR_CODE=4 }; #define ERRORMSG() vrerrormsg(__FILE__,__LINE__,VR_ERROR_FATAL) #define MEMERROR() vrerrormsg(__FILE__,__LINE__,VR_ERROR_MEM) #define IOERROR() vrerrormsg(__FILE__,__LINE__,VR_ERROR_IO) #define CODEERROR() vrerrormsg(__FILE__,__LINE__,VR_ERROR_CODE) #define WARNMSG(msg) vrerrormsg(__FILE__,__LINE__,VR_ERROR_NONFATAL,msg) inline void vrerrormsg(const char *file,int line,int fatal,const char *msg=NULL) { if (fatal==VR_ERROR_NONFATAL) fprintf(stderr,"warning"); else if (fatal==VR_ERROR_MEM) fprintf(stderr,"insufficient memory"); else if (fatal==VR_ERROR_IO) fprintf(stderr,"io error"); else if (fatal==VR_ERROR_CODE) fprintf(stderr,"unimplemented code"); else fprintf(stderr,"fatal error"); fprintf(stderr," in <%s> at line %d!\n",file,line); if (msg!=NULL) fprintf(stderr,"description: %s\n",msg); } #define PI (3.141593f) #define RAD (PI/180.0f) #ifndef MAXFLOAT #define MAXFLOAT (FLT_MAX) #endif #undef ffloor #define ffloor(x) floor((double)(x)) #undef fceil #define fceil(x) ceil((double)(x)) #define ftrc(x) (int)ffloor(x) inline double FABS(const double x) {return((x<0.0)?-x:x);} #define fabs(x) FABS(x) #ifndef min inline int min(const int a,const int b) {return((ab)?a:b);} #endif inline double FMAX(const double a,const double b) {return((a>b)?a:b);} #define fmax(a,b) FMAX(a,b) inline int sqr(const int x) {return(x*x);} inline double fsqr(const double x) {return(x*x);} #undef fsqrt #define fsqrt(x) sqrt((double)(x)) #undef fsin #define fsin(x) sin((double)(x)) #undef fcos #define fcos(x) cos((double)(x)) #undef ftan #define ftan(x) tan((double)(x)) #undef fasin #define fasin(x) asin((double)(x)) #undef facos #define facos(x) acos((double)(x)) #undef fatan #define fatan(x) atan((double)(x)) #undef fexp #define fexp(x) exp((double)(x)) #undef flog #define flog(x) log((double)(x)) #undef fpow #define fpow(x,y) pow((double)(x),(double)(y)) #ifdef UNIX #define GETRANDOM() drand48() #endif #ifdef WINOS #define GETRANDOM() ((double)rand()/RAND_MAX) #endif inline double GETTIME() { #ifdef UNIX struct timeval t; gettimeofday(&t,NULL); return(t.tv_sec+t.tv_usec/1.0E6); #endif #ifdef WINOS static int cpus=0; if (cpus==0) { SYSTEM_INFO SystemInfo; GetSystemInfo(&SystemInfo); cpus=SystemInfo.dwNumberOfProcessors; } if (cpus==1) { LARGE_INTEGER freq,count; if (QueryPerformanceFrequency(&freq)==0) ERRORMSG(); QueryPerformanceCounter(&count); return((double)count.QuadPart/freq.QuadPart); } return((double)clock()/CLOCKS_PER_SEC); #endif } inline double gettime() { static double time; static BOOLINT settime=FALSE; if (!settime) { time=GETTIME(); settime=TRUE; } return(GETTIME()-time); } inline void waitfor(double secs) { if (secs<=0.0) return; #ifdef UNIX struct timespec dt,rt; dt.tv_sec=ftrc(secs); dt.tv_nsec=ftrc(1.0E9*(secs-ftrc(secs))); while (nanosleep(&dt,&rt)!=0) dt=rt; #else double time=gettime()+secs; while (gettime() #include inline int removefile(const char *file) {return(remove(file));} inline int removedir(const char *dir) {return(_rmdir(dir));} #else #include #include inline int removefile(const char *file) {return(remove(file));} inline int removedir(const char *dir) {return(rmdir(dir));} #endif #endif