source: GTP/trunk/App/Demos/Vis/CHC_revisited/common.cpp @ 2753

Revision 2753, 5.6 KB checked in by mattausch, 16 years ago (diff)
Line 
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
25using namespace std;
26
27
28namespace CHCDemo
29{
30
31
32Real Limits::Threshold = 1e-6f;
33Real Limits::Small = 0.01f;
34Real Limits::Infinity = 1e20f;
35
36
37Real Random(Real max)
38{
39#ifdef __UNIX__
40  return (random()*max/0x7FFFFFFF);
41#else
42  return (((Real)rand()) / RAND_MAX) * max;
43#endif
44}
45
46
47int Random(int max)
48{
49        return rand() % max;
50}
51
52
53void Randomize()
54{
55        time_t t;
56        srand((unsigned) time(&t));
57}
58
59void
60Randomize(const unsigned int seed)
61{
62  srand(seed);
63}
64
65
66string
67ReplaceSuffix(const string &filename, const string &a, const string &b)
68{
69        string result = filename;
70
71        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
72        if (pos == filename.size() - a.size())
73        {
74                result.replace(pos, a.size(), b);
75        }
76
77        return result;
78}
79
80
81int
82SplitFilenames(const string &str, vector<string> &filenames)
83{
84        int pos = 0;
85
86        while(1) {
87                int npos = (int)str.find(';', pos);
88               
89                if (npos < 0 || npos - pos < 1)
90                        break;
91                filenames.push_back(string(str, pos, npos - pos));
92                pos = npos + 1;
93        }
94       
95        filenames.push_back(string(str, pos, str.size() - pos));
96        return (int)filenames.size();
97}
98
99void GetKey(char *s)
100{
101#ifdef _X_WINDOW_
102XWaitForButton(s);
103#else
104getch();
105#endif
106}
107
108#ifdef  _MSC_VER
109static bool hasHRTimer = false;
110static LARGE_INTEGER hrFreq;
111#endif
112
113
114void InitTiming()
115{
116#ifdef  _MSC_VER
117        hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq);
118#endif
119}
120
121long GetTime()
122{
123#ifndef  _MSC_VER
124        static struct rusage r;
125        getrusage(RUSAGE_SELF,&r);
126        return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec;
127#else
128        if (hasHRTimer) {
129                LARGE_INTEGER counter;
130                QueryPerformanceCounter(&counter);
131                // return in usec
132                //return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
133                // $$
134                // tmp store time in ms
135                return (long) (1000*counter.QuadPart/(hrFreq.QuadPart));
136        } else {
137                static struct _timeb mtime;
138                _ftime(&mtime);
139
140                return 1000*(1000*mtime.time + mtime.millitm);
141        }
142#endif
143}
144
145
146long
147GetRealTime()
148{
149#ifndef  _MSC_VER
150        static struct timeval _tstart;
151        static struct timezone tz;
152        gettimeofday(&_tstart,&tz);
153        return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
154#else
155        if (hasHRTimer) {
156                LARGE_INTEGER counter;
157                QueryPerformanceCounter(&counter);
158                // return in usec
159                return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
160        } else {
161                static struct _timeb mtime;
162                _ftime(&mtime);
163                return 1000*(1000*mtime.time + mtime.millitm);
164        }
165#endif
166}
167
168// return time diff. in ms
169Real TimeDiff(long time1, long time2) // in ms
170{
171        //const Real clk=1.0e-3f; // ticks per second
172        // $$ tmp store time in ms
173        const Real clk=1.0f; // ticks per second
174        long t=time2-time1;
175
176        return ((t<0)?-t:t)*clk;
177}
178
179char *TimeString()
180{
181        time_t t;
182        time(&t);
183
184        return ctime(&t);
185}
186
187
188char *GetAbsPath(char *name)
189{
190        //  char *url = new char[strlen(name)+1];
191        //  strcpy(url,name);
192
193        // get the absolute path
194        size_t c = strlen(name);
195        int i = c - 1;
196        bool wasDot = false;
197
198        // search for double dot
199        for (; i >= 0; -- i)
200        {
201                if (name[i]=='.')
202                {
203                        if (wasDot)
204                                break;
205                        wasDot = true;
206                }
207                else
208                        wasDot = false;
209        }
210
211        if (i>0)
212                i+=3;
213        if (i<0)
214                i=0;
215
216        char *url = new char[c-i+1];
217
218        int j=0;
219
220        for (;i<c;i++,j++)
221                url[j] = name[i];
222
223        url[j]=0;
224
225        return url;
226}
227
228
229void indent(ostream &app, int ind)
230{
231        int i;
232       
233        for (i = 0; i < ind; i++)
234                app << ' ';
235}
236
237
238char *GetPath(const char *s)
239{
240        size_t i = strlen(s);
241        for (; i > 0; -- i)
242        {
243                if (s[i]=='/' || s[i]=='\\')
244                        break;
245        }
246
247        char *path = new char[i+1];
248        int j=0;
249        for (; j<i; j++)
250                path[j] = s[j];
251        path[j] = 0;
252        return path;
253}
254
255
256char *
257StripPath(const char *s)
258{
259        const int len = strlen(s);
260
261        int i = len;
262        for (; i>0; i--) {
263                if (s[i]=='/' || s[i]=='\\')
264                        break;
265        }
266
267        char *filename = new char[len - i];
268        int j = 0;
269
270        for (; j < len - i; ++ j)
271                filename[j] = s[i + j + 1];
272        //filename[j] = 0;
273
274        return filename;
275}
276
277char *
278strdup(char *a)
279{
280        if (a) {
281                char *s = new char[strlen(a)+1];
282                strcpy(s, a);
283                return s;
284        } else
285                return NULL;
286}
287
288bool
289FileExists(char *filename)
290{
291        FILE *f;
292
293        f = fopen(filename,"r");
294        if (f) {
295                fclose(f);
296                return true;
297        }
298        return false;
299}
300
301
302#if TOIMPLEMENT
303
304bool
305CreateDir(char *dir)
306{
307#ifdef _MSC_VER
308        HANDLE fFile; // File Handle
309        WIN32_FIND_DATA fileinfo; // File Information Structure
310        int n = strlen(dir) - 1;
311
312        if (dir[n] == '\\' || dir[n] == '/')
313                dir[n]=0;
314
315        fFile = FindFirstFile(dir, &fileinfo);
316
317        // if the file exists and it is a directory
318        if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
319                // Directory Exists close file and return
320                FindClose(fFile);
321                return true;
322        }
323
324        FindClose(fFile);
325
326        // Now lets cycle through the String Array and create each directory in turn
327        if (CreateDirectory(dir, 0)) {
328                SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
329                return true;
330        } else {
331                char *temp = GetPath(dir);
332
333                if (strcmp(temp, dir)!=0) {
334                        CreateDir(temp);
335                        if (CreateDirectory(dir, 0)) {
336                                SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
337                                return true;
338                        }
339                }
340                delete temp;
341        }
342        Debug << "cannot create directory " << dir << endl;
343#endif
344        return false;
345}
346
347
348#endif
349
350}
Note: See TracBrowser for help on using the repository browser.