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

Revision 2603, 5.9 KB checked in by bittner, 17 years ago (diff)

sse disabled for hr

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