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