COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
unixcompat.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 /* these routines are header-only */
9 
10 
11 #ifndef COV_WINCOMPAT_H
12 #define COV_WINCOMPAT_H
13 
14 #include <stdarg.h>
15 #include <string.h>
16 #include <stdio.h>
17 
18 #ifndef WINCOMPATINLINE
19 #ifdef __cplusplus
20 #define WINCOMPATINLINE inline
21 #else
22 #define WINCOMPATINLINE static
23 #endif
24 #endif
25 
26 #if defined(_WIN32) || defined(_WIN64)
27 #include <float.h>
28 #if !defined(__MINGW32__)
29 // don't do this, otherwise std::isnan does not work#define isnan _isnan
30 namespace std
31 {
32  inline int isnan(double X){return _isnan(X);};
33  inline int isinf(double X){return !_finite(X);};
34  inline int finite(double X){return _finite(X);};
35 }
36 #endif
37 #include <winsock2.h>
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AMD_MEAN
40 #include <windows.h>
41 #undef WIN32_LEAN_AMD_MEAN
42 #else
43 #include <windows.h>
44 #endif
45 #ifndef _WIN32_WCE
46 #include <sys/types.h>
47 #include <sys/timeb.h>
48 #include <direct.h>
49 #endif
50 #ifndef SURFACE
51 
52 WINCOMPATINLINE char *basename(char *pathptr)
53 {
54  static char fullPath[MAX_PATH];
55  static char dummystr[] = ".";
56  char *filebn = 0;
57  char *retval = dummystr;
58  if (GetFullPathName(pathptr, MAX_PATH, fullPath, &filebn) != 0)
59  {
60  if (*filebn != 0)
61  retval = filebn;
62  }
63  return retval;
64 }
65 #endif
66 
67 #if !defined(__MINGW32__)
68 WINCOMPATINLINE int strcasecmp(const char *s1, const char *s2)
69 {
70  return stricmp(s1, s2);
71 }
72 #endif
73 
74 WINCOMPATINLINE void sleep(int time)
75 {
76  Sleep(time * 1000);
77 }
78 
79 WINCOMPATINLINE void usleep(int time)
80 {
81  Sleep(time / 1000);
82 }
83 
84 #if !defined(__MINGW32__)
85 WINCOMPATINLINE int strncasecmp(const char *s1, const char *s2, size_t n)
86 {
87  return strnicmp(s1, s2, n);
88 }
89 
90 #if defined(_MSC_VER) && (_MSC_VER < 1900)
91 WINCOMPATINLINE int snprintf(char *str, size_t size, const char *format, ...)
92 {
93  int result;
94  va_list ap;
95  va_start(ap, format);
96  result = _vsnprintf(str, size, format, ap);
97  va_end(ap);
98  str[size - 1] = '\0';
99  return result;
100 }
101 #endif
102 #endif
103 
104 #if defined(_MSC_VER) && (_MSC_VER < 1020)
105 WINCOMPATINLINE int vsnprintf(char *str, size_t size, const char *format, va_list ap)
106 {
107  int result = _vsnprintf(str, size, format, ap);
108  str[size - 1] = '\0';
109  return result;
110 }
111 #endif
112 
113 #if defined(__MINGW32__)
114 #include <sys/time.h>
115 #else
116 #ifdef timezone
117 #undef timezone
118 #endif
119 struct timezone
120 {
121  int tz_minuteswest;
122  int tz_dsttime;
123 };
124 
125 WINCOMPATINLINE int gettimeofday(struct timeval *tv, struct timezone *tz)
126 {
127  struct __timeb64 currentTime;
128  (void)tz;
129 #if _MSC_VER < 1400
130  _ftime64(&currentTime);
131 #else
132  _ftime64_s(&currentTime);
133 #endif
134  tv->tv_sec = (long)currentTime.time;
135  tv->tv_usec = (long)currentTime.millitm * 1000;
136 
137  return 0;
138 }
139 #endif
140 
141 #else /* unix */
142 #include <unistd.h>
143 #include <sys/time.h>
144 #endif /* _WIN32 or _WIN64 */
145 
146 #if defined(_WIN32) || defined(__hpux)
147 WINCOMPATINLINE char *strtok_r(char *s1, const char *s2, char **lasts)
148 {
149  char *ret;
150 
151  if (s1 == NULL)
152  s1 = *lasts;
153  while (*s1 && strchr(s2, *s1))
154  ++s1;
155  if (*s1 == '\0')
156  return NULL;
157  ret = s1;
158  while (*s1 && !strchr(s2, *s1))
159  ++s1;
160  if (*s1)
161  *s1++ = '\0';
162  *lasts = s1;
163  return ret;
164 }
165 #endif
166 
167 #if !defined(__linux__) && !defined(__APPLE__)
168 WINCOMPATINLINE char *strsep(char **s, const char *delim)
169 {
170  if (!s)
171  return NULL;
172 
173  char *start = *s;
174  char *p = *s;
175 
176  if (p == NULL)
177  return NULL;
178 
179  while (*p)
180  {
181  const char *d = delim;
182  while (*d)
183  {
184  if (*p == *d)
185  {
186  *p = '\0';
187  *s = p + 1;
188  return start;
189  }
190  d++;
191  }
192  p++;
193  }
194 
195  *s = NULL;
196  return start;
197 }
198 #endif
199 
200 #if defined(__APPLE__)
201 WINCOMPATINLINE off_t lseek64(int fildes, off_t offset, int whence)
202 {
203  return lseek(fildes, offset, whence);
204 }
205 #endif
206 
207 #endif /* COV_WINCOMPAT_H */
GLsizeiptr size
Definition: khronos-glext.h:6610
GLintptr offset
Definition: khronos-glext.h:6611
#define WINCOMPATINLINE
Definition: unixcompat.h:22
static const int s2
Definition: SammConv.cpp:38
GLfloat GLfloat p
Definition: khronos-glext.h:9861
GLbyte GLbyte tz
Definition: khronos-glext.h:9715
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: khronos-glext.h:6344
GLuint64EXT * result
Definition: khronos-glext.h:12573
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
Definition: khronos-glext.h:13144
GLdouble s
Definition: khronos-glext.h:6441
WINCOMPATINLINE char * strsep(char **s, const char *delim)
Definition: unixcompat.h:168
GLdouble n
Definition: khronos-glext.h:8447
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
GLuint start
Definition: khronos-glext.h:6343
#define NULL
Definition: covise_list.h:22