Structural deformable models
utils.h
Go to the documentation of this file.
1 #ifndef _UTILS_H_
2 #define _UTILS_H_
3 
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 #ifdef WIN32
8 #include <io.h>
9 #endif
10 #ifdef LINUX
11 #include <dirent.h>
12 #endif
13 #include <ctype.h>
14 #include <assert.h>
15 #include <sys/time.h>
16 #include "common.h"
17 
18 #define MAKE_STRING( msg ) \
19  ( ((std::ostringstream&)(std::ostringstream() << msg)).str() )
20 #define MAKE_CSTRING( msg ) \
21  ( ((std::ostringstream&)(std::ostringstream() << msg)).str().c_str() )
22 
23 #define COMMENT_CHAR '#'
24 
25 inline std::istream& comment(std::istream &s)
26 {
27  char c;
28  while (s.get(c)) {
29  if (!isspace(c)) {
30  if (c == COMMENT_CHAR) {
31  while (s.get(c) && c != '\n');
32  } else {
33  s.putback(c);
34  break;
35  }
36  }
37  }
38  return s;
39 }
40 
41 inline unsigned long getMilliSeconds() {
42 #ifdef WIN32
43 // typedef unsigned long LONGLONG;
44 // typedef long LARGE_INTEGER;
45 
46  static LONGLONG frequency=0;
47  if(!frequency)
48  QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
49  LONGLONG counter;
50  QueryPerformanceCounter((LARGE_INTEGER*)&counter);
51  return (unsigned long) (counter*1000 / frequency);
52 #else
53  static struct timeval time;
54  gettimeofday(&time, 0);
55  return (unsigned long)time.tv_sec*1000 + (unsigned long)time.tv_usec/1000;
56 #endif
57 }
58 
59 template<class T>
60 std::string toString(T v) {
61  std::stringstream ss;
62  ss << v;
63  return ss.str();
64 }
65 
66 template<class T>
67 T& fromString(const std::string& str, T& v) {
68  std::istringstream ss(str);
69  ss >> v;
70  return v;
71 }
72 
73 std::string& replaceAll(std::string& txt, char src, char tar);
74 
75 int matlabCall(const std::string& cmd);
76 
77 const char* getTemp();
78 
79 // only usable if LINUX defined
80 const std::string& __set_fnmatchpattern(const std::string& pattern);
81 int __fixed_fnmatch(const struct dirent* dent);
82 
83 #ifdef LINUX
84 template<class T>
85 int findFiles(const std::string& fmask, T iter) { return 0; }
86 #else
87 template<class T>
88 int findFiles(const std::string& fmask, T iter) {
89  struct _finddata_t ff;
90  int ffhandle;
91  int ffcount=0;
92  if((ffhandle = _findfirst(fmask.c_str(), &ff)) != -1) {
93  do {
94  *iter++ = ff.name;
95  ffcount++;
96  } while(_findnext(ffhandle, &ff) == 0);
97  }
98  return ffcount;
99 }
100 #endif
101 
102 #endif
T & fromString(const std::string &str, T &v)
Definition: utils.h:67
int findFiles(const std::string &fmask, T iter)
Definition: utils.h:88
std::string toString(T v)
Definition: utils.h:60
unsigned long getMilliSeconds()
Definition: utils.h:41
std::string & replaceAll(std::string &txt, char src, char tar)
Definition: utils.cpp:32
std::istream & comment(std::istream &s)
Definition: utils.h:25
int __fixed_fnmatch(const struct dirent *dent)
Definition: ffind.cpp:12
int matlabCall(const std::string &cmd)
Definition: matlab.cpp:7
const std::string & __set_fnmatchpattern(const std::string &pattern)
Definition: utils.cpp:15
#define COMMENT_CHAR
Definition: utils.h:23
const char * getTemp()
Definition: utils.cpp:25