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 |
|
---|
28 | namespace CHCDemoEngine
|
---|
29 | {
|
---|
30 |
|
---|
31 |
|
---|
32 | Real Limits::Threshold = 1e-6f;
|
---|
33 | Real Limits::Small = 0.01f;
|
---|
34 | Real Limits::Infinity = 1e20f;
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
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 |
|
---|
60 |
|
---|
61 | void Randomize(unsigned int seed)
|
---|
62 | {
|
---|
63 | srand(seed);
|
---|
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 |
|
---|
141 | return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
|
---|
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);
|
---|
164 | return 1000*(1000*(long)mtime.time + (long)mtime.millitm);
|
---|
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 |
|
---|
180 | char *TimeString()
|
---|
181 | {
|
---|
182 | time_t t;
|
---|
183 | time(&t);
|
---|
184 |
|
---|
185 | return ctime(&t);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | char *GetAbsPath(char *name)
|
---|
190 | {
|
---|
191 | // char *url = new char[strlen(name)+1];
|
---|
192 | // strcpy(url,name);
|
---|
193 |
|
---|
194 | // get the absolute path
|
---|
195 | size_t c = strlen(name);
|
---|
196 | size_t i = c - 1;
|
---|
197 | bool wasDot = false;
|
---|
198 |
|
---|
199 | // search for double dot
|
---|
200 | for (; i >= 0; -- i)
|
---|
201 | {
|
---|
202 | if (name[i]=='.')
|
---|
203 | {
|
---|
204 | if (wasDot)
|
---|
205 | break;
|
---|
206 | wasDot = true;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | wasDot = false;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (i>0)
|
---|
213 | i+=3;
|
---|
214 | if (i<0)
|
---|
215 | i=0;
|
---|
216 |
|
---|
217 | char *url = new char[c-i+1];
|
---|
218 |
|
---|
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;
|
---|
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 | {
|
---|
241 | size_t i = strlen(s);
|
---|
242 | for (; i > 0; -- i)
|
---|
243 | {
|
---|
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 | {
|
---|
260 | const size_t len = strlen(s);
|
---|
261 |
|
---|
262 | size_t i = len;
|
---|
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 | char *
|
---|
279 | strdup(char *a)
|
---|
280 | {
|
---|
281 | if (a) {
|
---|
282 | char *s = new char[strlen(a)+1];
|
---|
283 | strcpy(s, a);
|
---|
284 | return s;
|
---|
285 | } else
|
---|
286 | return NULL;
|
---|
287 | }
|
---|
288 |
|
---|
289 | bool
|
---|
290 | FileExists(char *filename)
|
---|
291 | {
|
---|
292 | FILE *f;
|
---|
293 |
|
---|
294 | f = fopen(filename,"r");
|
---|
295 | if (f) {
|
---|
296 | fclose(f);
|
---|
297 | return true;
|
---|
298 | }
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 |
|
---|
302 | }
|
---|