#include "common.h" #ifdef _X_WINDOW_ #include "xwindow.h" #endif #ifdef _CRT_SET #define _CRTDBG_MAP_ALLOC #include #include // redefine new operator #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif #include #include #include #include #ifndef _MSC_VER #include #include #include #include #include #else #include #include #include #endif using namespace std; namespace CHCDemoEngine { Real Limits::Threshold = 1e-6f; Real Limits::Small = 0.01f; Real Limits::Infinity = 1e20f; Real Random(Real max) { #ifdef __UNIX__ return (random()*max/0x7FFFFFFF); #else return (((Real)rand()) / RAND_MAX) * max; #endif } int Random(int max) { return rand() % max; } void Randomize() { time_t t; srand((unsigned) time(&t)); } void Randomize(unsigned int seed) { srand(seed); } string ReplaceSuffix(const string &filename, const string &a, const string &b) { string result = filename; int pos = (int)filename.rfind(a, (int)filename.size() - 1); if (pos == filename.size() - a.size()) { result.replace(pos, a.size(), b); } return result; } int SplitFilenames(const string &str, vector &filenames) { int pos = 0; while(1) { int npos = (int)str.find(';', pos); if (npos < 0 || npos - pos < 1) break; filenames.push_back(string(str, pos, npos - pos)); pos = npos + 1; } filenames.push_back(string(str, pos, str.size() - pos)); return (int)filenames.size(); } void GetKey(char *s) { #ifdef _X_WINDOW_ XWaitForButton(s); #else getch(); #endif } #ifdef _MSC_VER static bool hasHRTimer = false; static LARGE_INTEGER hrFreq; #endif void InitTiming() { #ifdef _MSC_VER hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq); #endif } long GetTime() { #ifndef _MSC_VER static struct rusage r; getrusage(RUSAGE_SELF,&r); return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec; #else if (hasHRTimer) { LARGE_INTEGER counter; QueryPerformanceCounter(&counter); // return in usec //return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart)); // $$ // tmp store time in ms return (long) (1000*counter.QuadPart/(hrFreq.QuadPart)); } else { static struct _timeb mtime; _ftime(&mtime); return 1000*(1000*(long)mtime.time + (long)mtime.millitm); } #endif } long GetRealTime() { #ifndef _MSC_VER static struct timeval _tstart; static struct timezone tz; gettimeofday(&_tstart,&tz); return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec); #else if (hasHRTimer) { LARGE_INTEGER counter; QueryPerformanceCounter(&counter); // return in usec return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart)); } else { static struct _timeb mtime; _ftime(&mtime); return 1000*(1000*(long)mtime.time + (long)mtime.millitm); } #endif } // return time diff. in ms Real TimeDiff(long time1, long time2) // in ms { //const Real clk=1.0e-3f; // ticks per second // $$ tmp store time in ms const Real clk=1.0f; // ticks per second long t=time2-time1; return ((t<0)?-t:t)*clk; } char *TimeString() { time_t t; time(&t); return ctime(&t); } char *GetAbsPath(char *name) { // char *url = new char[strlen(name)+1]; // strcpy(url,name); // get the absolute path size_t c = strlen(name); size_t i = c - 1; bool wasDot = false; // search for double dot for (; i >= 0; -- i) { if (name[i]=='.') { if (wasDot) break; wasDot = true; } else wasDot = false; } if (i>0) i+=3; if (i<0) i=0; char *url = new char[c-i+1]; int j=0; for (;i 0; -- i) { if (s[i]=='/' || s[i]=='\\') break; } char *path = new char[i+1]; int j=0; for (; j0; i--) { if (s[i]=='/' || s[i]=='\\') break; } char *filename = new char[len - i]; int j = 0; for (; j < len - i; ++ j) filename[j] = s[i + j + 1]; //filename[j] = 0; return filename; } bool FileExists(char *filename) { FILE *f; f = fopen(filename,"r"); if (f) { fclose(f); return true; } return false; } }