source: GTP/trunk/Lib/Vis/Preprocessing/src/common.cpp @ 1634

Revision 1634, 5.0 KB checked in by mattausch, 18 years ago (diff)

multiple path support for kd

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