#include #include #include #include #include "common.h" #ifdef _X_WINDOW_ #include "xwindow.h" #endif #ifndef _MSC_VER #include #include #include #include #include #else #include #include #include #endif namespace GtpVisibilityPreprocessor { // global file streams ofstream Debug; int CGlobals::Special=0; // Puts the specified number of spaces out to cout. Used for // indentation during CObject3D::Describe(). void indent(ostream &app, int ind) { int i; for (i = 0; i < ind; i++) app << ' '; } 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(const unsigned int seed) { srand(seed); } 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*mtime.time + 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*mtime.time + 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 int c = strlen(name); int 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 (;i0; 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; } char * strdup(char *a) { if (a) { char *s = new char[strlen(a)+1]; strcpy(s,a); return s; } else return NULL; } bool FileExists(char *filename) { FILE *f; f = fopen(filename,"r"); if (f) { fclose(f); return true; } return false; } #if 0 bool CreateDir(char *dir) { #ifdef _MSC_VER HANDLE fFile; // File Handle WIN32_FIND_DATA fileinfo; // File Information Structure int n = strlen(dir) - 1; if (dir[n] == '\\' || dir[n] == '/') dir[n]=0; fFile = FindFirstFile(dir, &fileinfo); // if the file exists and it is a directory if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { // Directory Exists close file and return FindClose(fFile); return true; } FindClose(fFile); // Now lets cycle through the String Array and create each directory in turn if (CreateDirectory(dir, 0)) { SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL); return true; } else { char *temp = GetPath(dir); if (strcmp(temp, dir)!=0) { CreateDir(temp); if (CreateDirectory(dir, 0)) { SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL); return true; } } delete temp; } Debug << "cannot create directory " << dir << endl; #endif return false; } #endif }