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

Revision 2751, 5.5 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 *
180TimeString()
181{
182        time_t t;
183        time(&t);
184        return ctime(&t);
185}
186
187char *
188GetAbsPath(char *name)
189{
190        //  char *url = new char[strlen(name)+1];
191        //  strcpy(url,name);
192
193        // get the absolute path
194        int c = strlen(name);
195        int i = c-1;
196        bool wasDot = false;
197        // search for double dot
198        for (;i>=0;i--)
199                if (name[i]=='.') {
200                        if (wasDot)
201                                break;
202                        wasDot = true;
203                } else
204                        wasDot = false;
205
206                if (i>0)
207                        i+=3;
208                if (i<0)
209                        i=0;
210
211                char *url = new char[c-i+1];
212                int j=0;
213                for (;i<c;i++,j++)
214                        url[j] = name[i];
215                url[j]=0;
216
217                return url;
218}
219
220
221void indent(ostream &app, int ind)
222{
223        int i;
224       
225        for (i = 0; i < ind; i++)
226                app << ' ';
227}
228
229
230char *GetPath(const char *s)
231{
232        int i=strlen(s);
233        for (; i>0; i--) {
234                if (s[i]=='/' || s[i]=='\\')
235                        break;
236        }
237
238        char *path = new char[i+1];
239        int j=0;
240        for (; j<i; j++)
241                path[j] = s[j];
242        path[j] = 0;
243        return path;
244}
245
246
247char *
248StripPath(const char *s)
249{
250        const int len = strlen(s);
251
252        int i = len;
253        for (; i>0; i--) {
254                if (s[i]=='/' || s[i]=='\\')
255                        break;
256        }
257
258        char *filename = new char[len - i];
259        int j = 0;
260
261        for (; j < len - i; ++ j)
262                filename[j] = s[i + j + 1];
263        //filename[j] = 0;
264
265        return filename;
266}
267
268char *
269strdup(char *a)
270{
271        if (a) {
272                char *s = new char[strlen(a)+1];
273                strcpy(s, a);
274                return s;
275        } else
276                return NULL;
277}
278
279bool
280FileExists(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
293#if TOIMPLEMENT
294
295bool
296CreateDir(char *dir)
297{
298#ifdef _MSC_VER
299        HANDLE fFile; // File Handle
300        WIN32_FIND_DATA fileinfo; // File Information Structure
301        int n = strlen(dir) - 1;
302
303        if (dir[n] == '\\' || dir[n] == '/')
304                dir[n]=0;
305
306        fFile = FindFirstFile(dir, &fileinfo);
307
308        // if the file exists and it is a directory
309        if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
310                // Directory Exists close file and return
311                FindClose(fFile);
312                return true;
313        }
314
315        FindClose(fFile);
316
317        // Now lets cycle through the String Array and create each directory in turn
318        if (CreateDirectory(dir, 0)) {
319                SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
320                return true;
321        } else {
322                char *temp = GetPath(dir);
323
324                if (strcmp(temp, dir)!=0) {
325                        CreateDir(temp);
326                        if (CreateDirectory(dir, 0)) {
327                                SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
328                                return true;
329                        }
330                }
331                delete temp;
332        }
333        Debug << "cannot create directory " << dir << endl;
334#endif
335        return false;
336}
337
338
339#endif
340
341}
Note: See TracBrowser for help on using the repository browser.