source: trunk/VUT/GtpVisibilityPreprocessor/src/common.cpp @ 542

Revision 542, 4.7 KB checked in by mattausch, 18 years ago (diff)
Line 
1
2#include <math.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <time.h>
6
7#include "common.h"
8
9#ifdef _X_WINDOW_
10#include "xwindow.h"
11#endif
12
13#ifndef _MSC_VER
14#include <unistd.h>
15#include <sys/types.h>
16#include <sys/time.h>
17#include <sys/times.h>
18#include <sys/resource.h>
19#else
20#include <windows.h>
21#include <sys/types.h>
22#include <sys/timeb.h>
23#endif
24
25
26
27// global file streams
28ofstream Debug;
29
30int CGlobals::Special=0;
31
32
33// Puts the specified number of spaces out to cout.  Used for
34// indentation during CObject3D::Describe().
35void
36indent(ostream &app, int ind)
37{
38  int i;
39
40  for (i = 0; i < ind; i++)
41    app << ' ';
42}
43
44
45
46Real Limits::Threshold = 1e-6f;
47Real Limits::Small = 0.01f;
48Real Limits::Infinity = 1e20;
49
50
51
52
53Real
54Random(Real max)
55{
56#ifdef __UNIX__
57  return (random()*max/0x7FFFFFFF);
58#else
59  return (((Real)rand())/RAND_MAX)*max;
60#endif
61 
62}
63
64int Random(int max)
65{
66  return rand()%max;
67}
68
69void Randomize()
70{
71  time_t t;
72  srand((unsigned) time(&t));
73}
74
75void
76Randomize(const unsigned int seed)
77{
78  srand(seed);
79}
80
81void GetKey(char *s)
82{
83#ifdef _X_WINDOW_
84XWaitForButton(s);
85#else
86getch();
87#endif
88}
89
90#ifdef  _MSC_VER
91static bool hasHRTimer = false;
92static LARGE_INTEGER hrFreq;
93#endif
94
95
96void
97InitTiming()
98{
99#ifdef  _MSC_VER
100  hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq);
101#endif
102}
103
104long
105GetTime()
106{
107#ifndef  _MSC_VER
108  static struct rusage r;
109  getrusage(RUSAGE_SELF,&r);
110  return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec;
111#else
112  if (hasHRTimer) {
113    LARGE_INTEGER counter;
114    QueryPerformanceCounter(&counter);
115    // return in usec
116    return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
117  } else {
118    static struct _timeb mtime;
119    _ftime(&mtime);
120   
121    return 1000*(1000*mtime.time + mtime.millitm);
122  }
123#endif
124}
125
126
127long
128GetRealTime()
129{
130#ifndef  _MSC_VER
131  static struct timeval _tstart;
132  static struct timezone tz;
133  gettimeofday(&_tstart,&tz);
134  return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
135#else
136  if (hasHRTimer) {
137    LARGE_INTEGER counter;
138    QueryPerformanceCounter(&counter);
139    // return in usec
140    return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
141  } else {
142    static struct _timeb mtime;
143    _ftime(&mtime);
144    return 1000*(1000*mtime.time + mtime.millitm);
145  }
146#endif
147}
148
149// return time diff. in ms
150Real
151TimeDiff(long time1,long time2) // in ms
152{
153  const Real clk=1.0e-3f; // ticks per second
154  long t=time2-time1;
155 
156  return ((t<0)?-t:t)*clk;
157}
158
159char *
160TimeString()
161{
162  time_t t;
163  time(&t);
164  return ctime(&t);
165}
166
167char *
168GetAbsPath(char *name)
169{
170  //  char *url = new char[strlen(name)+1];
171  //  strcpy(url,name);
172
173  // get the absolute path
174  int c = strlen(name);
175  int i = c-1;
176  bool wasDot = false;
177  // search for double dot
178  for (;i>=0;i--)
179    if (name[i]=='.') {
180      if (wasDot)
181   break;
182      wasDot = true;
183    } else
184      wasDot = false;
185
186  if (i>0)
187    i+=3;
188  if (i<0)
189    i=0;
190 
191  char *url = new char[c-i+1];
192  int j=0;
193  for (;i<c;i++,j++)
194    url[j] = name[i];
195  url[j]=0;
196 
197  return url;
198}
199
200char *
201GetPath(const char *s)
202{
203  int i=strlen(s);
204  for (; i>0; i--) {
205    if (s[i]=='/' || s[i]=='\\')
206      break;
207  }
208 
209  char *path = new char[i+1];
210  int j=0;
211  for (; j<i; j++)
212    path[j] = s[j];
213  path[j] = 0;
214  return path;
215}
216
217
218char *
219strdup(char *a)
220{
221  if (a) {
222    char *s = new char[strlen(a)+1];
223    strcpy(s,a);
224    return s;
225  } else
226    return NULL;
227}
228
229bool
230FileExists(char *filename)
231{
232  FILE *f;
233
234  f = fopen(filename,"r");
235  if (f) {
236    fclose(f);
237    return true;
238  }
239  return false;
240}
241
242
243#if 0
244bool
245CreateDir(char *dir)
246{
247#ifdef _MSC_VER
248  HANDLE fFile; // File Handle
249  WIN32_FIND_DATA fileinfo; // File Information Structure
250  int n = strlen(dir) - 1;
251 
252  if (dir[n] == '\\' || dir[n] == '/')
253    dir[n]=0;
254 
255  fFile = FindFirstFile(dir, &fileinfo);
256 
257  // if the file exists and it is a directory
258  if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
259    // Directory Exists close file and return
260    FindClose(fFile);
261    return true;
262  }
263 
264  FindClose(fFile);
265
266  // Now lets cycle through the String Array and create each directory in turn
267  if (CreateDirectory(dir, 0)) {
268    SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
269    return true;
270  } else {
271    char *temp = GetPath(dir);
272
273    if (strcmp(temp, dir)!=0) {
274      CreateDir(temp);
275      if (CreateDirectory(dir, 0)) {
276   SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
277   return true;
278      }
279    }
280    delete temp;
281  }
282  Debug << "cannot create directory " << dir << endl;
283#endif
284  return false;
285}
286#endif
Note: See TracBrowser for help on using the repository browser.