[2746] | 1 | #include <math.h>
|
---|
| 2 | #include <stdio.h>
|
---|
| 3 | #include <stdlib.h>
|
---|
| 4 | #include <time.h>
|
---|
| 5 |
|
---|
| 6 | #include "common.h"
|
---|
| 7 |
|
---|
| 8 | #ifdef _X_WINDOW_
|
---|
| 9 | #include "xwindow.h"
|
---|
| 10 | #endif
|
---|
| 11 |
|
---|
| 12 | #ifndef _MSC_VER
|
---|
| 13 | #include <unistd.h>
|
---|
| 14 | #include <sys/types.h>
|
---|
| 15 | #include <sys/time.h>
|
---|
| 16 | #include <sys/times.h>
|
---|
| 17 | #include <sys/resource.h>
|
---|
| 18 | #else
|
---|
| 19 | #include <windows.h>
|
---|
| 20 | #include <sys/types.h>
|
---|
| 21 | #include <sys/timeb.h>
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | using namespace std;
|
---|
| 26 |
|
---|
| 27 |
|
---|
[2776] | 28 | namespace CHCDemoEngine
|
---|
[2751] | 29 | {
|
---|
[2746] | 30 |
|
---|
[2751] | 31 |
|
---|
[2746] | 32 | Real Limits::Threshold = 1e-6f;
|
---|
| 33 | Real Limits::Small = 0.01f;
|
---|
| 34 | Real Limits::Infinity = 1e20f;
|
---|
| 35 |
|
---|
| 36 |
|
---|
[2756] | 37 |
|
---|
[2746] | 38 | Real Random(Real max)
|
---|
| 39 | {
|
---|
| 40 | #ifdef __UNIX__
|
---|
| 41 | return (random()*max/0x7FFFFFFF);
|
---|
| 42 | #else
|
---|
| 43 | return (((Real)rand()) / RAND_MAX) * max;
|
---|
| 44 | #endif
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | int Random(int max)
|
---|
| 49 | {
|
---|
| 50 | return rand() % max;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | void Randomize()
|
---|
| 55 | {
|
---|
| 56 | time_t t;
|
---|
| 57 | srand((unsigned) time(&t));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[2756] | 60 |
|
---|
[2772] | 61 | void Randomize(unsigned int seed)
|
---|
[2746] | 62 | {
|
---|
[2756] | 63 | srand(seed);
|
---|
[2746] | 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | string
|
---|
| 68 | ReplaceSuffix(const string &filename, const string &a, const string &b)
|
---|
| 69 | {
|
---|
| 70 | string result = filename;
|
---|
| 71 |
|
---|
| 72 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
| 73 | if (pos == filename.size() - a.size())
|
---|
| 74 | {
|
---|
| 75 | result.replace(pos, a.size(), b);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | return result;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | int
|
---|
| 83 | SplitFilenames(const string &str, vector<string> &filenames)
|
---|
| 84 | {
|
---|
| 85 | int pos = 0;
|
---|
| 86 |
|
---|
| 87 | while(1) {
|
---|
| 88 | int npos = (int)str.find(';', pos);
|
---|
| 89 |
|
---|
| 90 | if (npos < 0 || npos - pos < 1)
|
---|
| 91 | break;
|
---|
| 92 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 93 | pos = npos + 1;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
| 97 | return (int)filenames.size();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void GetKey(char *s)
|
---|
| 101 | {
|
---|
| 102 | #ifdef _X_WINDOW_
|
---|
| 103 | XWaitForButton(s);
|
---|
| 104 | #else
|
---|
| 105 | getch();
|
---|
| 106 | #endif
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | #ifdef _MSC_VER
|
---|
| 110 | static bool hasHRTimer = false;
|
---|
| 111 | static LARGE_INTEGER hrFreq;
|
---|
| 112 | #endif
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | void InitTiming()
|
---|
| 116 | {
|
---|
| 117 | #ifdef _MSC_VER
|
---|
| 118 | hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq);
|
---|
| 119 | #endif
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | long GetTime()
|
---|
| 123 | {
|
---|
| 124 | #ifndef _MSC_VER
|
---|
| 125 | static struct rusage r;
|
---|
| 126 | getrusage(RUSAGE_SELF,&r);
|
---|
| 127 | return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec;
|
---|
| 128 | #else
|
---|
| 129 | if (hasHRTimer) {
|
---|
| 130 | LARGE_INTEGER counter;
|
---|
| 131 | QueryPerformanceCounter(&counter);
|
---|
| 132 | // return in usec
|
---|
| 133 | //return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
| 134 | // $$
|
---|
| 135 | // tmp store time in ms
|
---|
| 136 | return (long) (1000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
| 137 | } else {
|
---|
| 138 | static struct _timeb mtime;
|
---|
| 139 | _ftime(&mtime);
|
---|
| 140 |
|
---|
[2784] | 141 | return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
|
---|
[2746] | 142 | }
|
---|
| 143 | #endif
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | long
|
---|
| 148 | GetRealTime()
|
---|
| 149 | {
|
---|
| 150 | #ifndef _MSC_VER
|
---|
| 151 | static struct timeval _tstart;
|
---|
| 152 | static struct timezone tz;
|
---|
| 153 | gettimeofday(&_tstart,&tz);
|
---|
| 154 | return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
|
---|
| 155 | #else
|
---|
| 156 | if (hasHRTimer) {
|
---|
| 157 | LARGE_INTEGER counter;
|
---|
| 158 | QueryPerformanceCounter(&counter);
|
---|
| 159 | // return in usec
|
---|
| 160 | return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
| 161 | } else {
|
---|
| 162 | static struct _timeb mtime;
|
---|
| 163 | _ftime(&mtime);
|
---|
[2784] | 164 | return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
|
---|
[2746] | 165 | }
|
---|
| 166 | #endif
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // return time diff. in ms
|
---|
| 170 | Real TimeDiff(long time1, long time2) // in ms
|
---|
| 171 | {
|
---|
| 172 | //const Real clk=1.0e-3f; // ticks per second
|
---|
| 173 | // $$ tmp store time in ms
|
---|
| 174 | const Real clk=1.0f; // ticks per second
|
---|
| 175 | long t=time2-time1;
|
---|
| 176 |
|
---|
| 177 | return ((t<0)?-t:t)*clk;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[2753] | 180 | char *TimeString()
|
---|
[2746] | 181 | {
|
---|
| 182 | time_t t;
|
---|
| 183 | time(&t);
|
---|
[2753] | 184 |
|
---|
[2746] | 185 | return ctime(&t);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[2753] | 188 |
|
---|
| 189 | char *GetAbsPath(char *name)
|
---|
[2746] | 190 | {
|
---|
| 191 | // char *url = new char[strlen(name)+1];
|
---|
| 192 | // strcpy(url,name);
|
---|
| 193 |
|
---|
| 194 | // get the absolute path
|
---|
[2753] | 195 | size_t c = strlen(name);
|
---|
[2764] | 196 | size_t i = c - 1;
|
---|
[2746] | 197 | bool wasDot = false;
|
---|
[2753] | 198 |
|
---|
[2746] | 199 | // search for double dot
|
---|
[2753] | 200 | for (; i >= 0; -- i)
|
---|
| 201 | {
|
---|
| 202 | if (name[i]=='.')
|
---|
| 203 | {
|
---|
[2746] | 204 | if (wasDot)
|
---|
| 205 | break;
|
---|
| 206 | wasDot = true;
|
---|
[2753] | 207 | }
|
---|
| 208 | else
|
---|
[2746] | 209 | wasDot = false;
|
---|
[2753] | 210 | }
|
---|
[2746] | 211 |
|
---|
[2753] | 212 | if (i>0)
|
---|
| 213 | i+=3;
|
---|
| 214 | if (i<0)
|
---|
| 215 | i=0;
|
---|
[2746] | 216 |
|
---|
[2753] | 217 | char *url = new char[c-i+1];
|
---|
[2746] | 218 |
|
---|
[2753] | 219 | int j=0;
|
---|
| 220 |
|
---|
| 221 | for (;i<c;i++,j++)
|
---|
| 222 | url[j] = name[i];
|
---|
| 223 |
|
---|
| 224 | url[j]=0;
|
---|
| 225 |
|
---|
| 226 | return url;
|
---|
[2746] | 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | void indent(ostream &app, int ind)
|
---|
| 231 | {
|
---|
| 232 | int i;
|
---|
| 233 |
|
---|
| 234 | for (i = 0; i < ind; i++)
|
---|
| 235 | app << ' ';
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | char *GetPath(const char *s)
|
---|
| 240 | {
|
---|
[2753] | 241 | size_t i = strlen(s);
|
---|
| 242 | for (; i > 0; -- i)
|
---|
| 243 | {
|
---|
[2746] | 244 | if (s[i]=='/' || s[i]=='\\')
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | char *path = new char[i+1];
|
---|
| 249 | int j=0;
|
---|
| 250 | for (; j<i; j++)
|
---|
| 251 | path[j] = s[j];
|
---|
| 252 | path[j] = 0;
|
---|
| 253 | return path;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | char *
|
---|
| 258 | StripPath(const char *s)
|
---|
| 259 | {
|
---|
[2764] | 260 | const size_t len = strlen(s);
|
---|
[2746] | 261 |
|
---|
[2764] | 262 | size_t i = len;
|
---|
[2746] | 263 | for (; i>0; i--) {
|
---|
| 264 | if (s[i]=='/' || s[i]=='\\')
|
---|
| 265 | break;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | char *filename = new char[len - i];
|
---|
| 269 | int j = 0;
|
---|
| 270 |
|
---|
| 271 | for (; j < len - i; ++ j)
|
---|
| 272 | filename[j] = s[i + j + 1];
|
---|
| 273 | //filename[j] = 0;
|
---|
| 274 |
|
---|
| 275 | return filename;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | bool
|
---|
| 280 | FileExists(char *filename)
|
---|
| 281 | {
|
---|
| 282 | FILE *f;
|
---|
| 283 |
|
---|
| 284 | f = fopen(filename,"r");
|
---|
| 285 | if (f) {
|
---|
| 286 | fclose(f);
|
---|
| 287 | return true;
|
---|
| 288 | }
|
---|
| 289 | return false;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | }
|
---|