MythTV  master
compat.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Simple header which encapsulates platform incompatibilities, so we
3 // do not need to litter the codebase with ifdefs.
4 
5 #ifndef COMPAT_H
6 #define COMPAT_H
7 
8 #ifdef __cplusplus
9 # include <QtGlobal> // for Q_OS_XXX
10 #endif
11 
12 #include <sys/param.h> // Defines BSD on FreeBSD, Mac OS X
13 
14 #include "mythconfig.h"
15 
16 // Libdvdnav now uses off64_t lseek64(), which BSD/Darwin doesn't have.
17 // Luckily, its lseek() is already 64bit compatible
18 #ifdef BSD
19  typedef off_t off64_t; //NOLINT(modernize-use-using) included from dvdnav C code
20 # define lseek64(f,o,w) lseek(f,o,w)
21 #endif
22 
23 #ifdef Q_OS_ANDROID
24 # ifndef S_IREAD
25 # define S_IREAD S_IRUSR
26 # endif
27 # ifndef S_IWRITE
28 # define S_IWRITE S_IRUSR
29 # endif
30 #endif
31 
32 #if defined(USING_MINGW)
33 #include <time.h>
34 #endif
35 
36 #ifndef _WIN32
37 # include <sys/time.h> // Mac OS X needs this before sys/resource
38 # include <sys/resource.h> // for setpriority
39 # include <sys/socket.h>
40 # include <sys/wait.h> // For WIFEXITED on Mac OS X
41 #else // _WIN32
42 # ifndef _MSC_VER
43 # define close wsock_close
44 # endif
45 
46 # ifndef NOMINMAX
47 # define NOMINMAX
48 # endif
49 
50 # include <windows.h>
51 
52 # undef DialogBox
53 # undef LoadImage
54 # undef LoadIcon
55 # undef GetObject
56 # undef DrawText
57 # undef CreateDialog
58 # undef CreateFont
59 # undef DeleteFile
60 # undef GetCurrentTime
61 # undef SetJob
62 # undef SendMessage
63 
64 # ifndef _MSC_VER
65 # include <winsock2.h>
66 # include <ws2tcpip.h>
67 # else
68 # include <io.h>
69 # endif
70 
71 # undef close
72 
73 # undef CopyFile
74 # undef MoveFile
75 
76 # define fsync(FD) 0
77 //used in videodevice only - that code is not windows-compatible anyway
78 # define minor(X) 0
79 
80  #if defined(__cplusplus)
81  using uint = unsigned int;
82  #else
83  typedef unsigned int uint;
84  #endif
85 
86 
87 # if defined(__cplusplus)
88 
89 # define setenv(x, y, z) ::SetEnvironmentVariableA(x, y)
90 # define unsetenv(x) 0
91 
92 
93  struct statfs {
94  // long f_type; /* type of filesystem */
95  long f_bsize; /* optimal transfer block size */
96  long f_blocks; /* total data blocks in file system */
97  // long f_bfree; /* free blocks in fs */
98  long f_bavail; /* free blocks avail to non-superuser */
99  // long f_files; /* total file nodes in file system */
100  // long f_ffree; /* free file nodes in fs */
101  // long f_fsid; /* file system id */
102  // long f_namelen; /* maximum length of filenames */
103  // long f_spare[6]; /* spare for later */
104  };
105  inline int statfs(const char* path, struct statfs* buffer)
106  {
107  DWORD spc = 0, bps = 0, fc = 0, c = 0;
108 
109  if (buffer && GetDiskFreeSpaceA(path, &spc, &bps, &fc, &c))
110  {
111  buffer->f_bsize = bps;
112  buffer->f_blocks = spc * c;
113  buffer->f_bavail = spc * fc;
114  return 0;
115  }
116 
117  return -1;
118  }
119 # endif
120 
121 #define lstat stat
122 #define nice(x) ((int)!::SetPriorityClass(\
123  ::GetCurrentProcess(), ((x) < -10) ? \
124  HIGH_PRIORITY_CLASS : (((x) < 0) ? \
125  ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
126  IDLE_PRIORITY_CLASS : (((x) > 0) ? \
127  BELOW_NORMAL_PRIORITY_CLASS : \
128  NORMAL_PRIORITY_CLASS)))))
129 #define PRIO_PROCESS 0
130 #define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
131 
132 
133  //signals: not tested
134 # define SIGHUP 1
135 # define SIGQUIT 3
136 # define SIGKILL 9
137 # define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
138 # define SIGUSR2 12 // used to restart LIRC as required
139 # define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
140 # define SIGALRM 14
141 # define SIGCONT 18
142 # define SIGSTOP 19
143 
144 # define O_SYNC 0
145 
146  // Success: mkfifo returns zero but CreateNamedPipeA returns a
147  // file handle. Failure: both return -1.
148  #define mkfifo(path, mode) \
149  (CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
150  PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
151  1024, 1024, 10000, nullptr) == INVALID_HANDLE_VALUE ? -1 : 0)
152 
153 # define RTLD_LAZY 0
154 # define dlopen(x, y) LoadLibraryA((x))
155 # define dlclose(x) !FreeLibrary((HMODULE)(x))
156 # define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
157 
158 # ifdef __cplusplus
159 # include <cstdio>
160  inline const char *dlerror(void)
161  {
162  #define DLERR_MAX 512
163  static char errStr[DLERR_MAX];
164  DWORD errCode = GetLastError();
165 
166  if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
167  FORMAT_MESSAGE_IGNORE_INSERTS |
168  FORMAT_MESSAGE_MAX_WIDTH_MASK,
169  nullptr, errCode,
170  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
171  errStr, DLERR_MAX - 1, nullptr))
172  snprintf(errStr, DLERR_MAX - 1,
173  "dlopen()/dlsym() caused error %d", (int)errCode);
174 
175  return errStr;
176  }
177 # else // __cplusplus
178 # include <stdio.h>
179 # define dlerror() "dlerror() is unimplemented."
180 # endif // __cplusplus
181 
182  // getuid/geteuid/setuid - not implemented
183 # define getuid() 0
184 # define geteuid() 0
185 # define setuid(x) 0
186 # define seteuid(x) 0
187 
188 
189 // TODO this stuff is not implemented yet
190 # define daemon(x, y) -1
191 # define getloadavg(x, y) -1
192 
193 // this stuff is untested
194 # define WIFEXITED(w) (((w) & 0xff) == 0)
195 # define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
196 # define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
197 # define WEXITSTATUS(w) (((w) >> 8) & 0xff)
198 # define WTERMSIG(w) ((w) & 0x7f)
199 
200 
201 # ifdef LZO_COMPILE_TIME_ASSERT_HEADER
202 # undef LZO_COMPILE_TIME_ASSERT_HEADER
203 # endif
204 
205 # define LZO_COMPILE_TIME_ASSERT_HEADER( a )
206 
207 # ifdef LZO_COMPILE_TIME_ASSERT
208 # undef LZO_COMPILE_TIME_ASSERT
209 # endif
210 
211 # define LZO_COMPILE_TIME_ASSERT( a )
212 
213 #endif // _WIN32
214 
215 
216 #ifdef _MSC_VER
217  #include <cstdlib> // for rand()
218  #include <ctime>
219  #include <sys/time.h>
220 
221  // Turn off the visual studio warnings (identifier was truncated)
222  #pragma warning(disable:4786)
223 
224  #ifdef restrict
225  #undef restrict
226  #endif
227 
228  #include <cinttypes>
229  #include <direct.h>
230  #include <process.h>
231 
232  #define strtoll _strtoi64
233  #define strncasecmp _strnicmp
234  #define snprintf _snprintf
235 
236  #ifdef _WIN64
237  using ssize_t = __int64;
238  #else
239  using ssize_t = int;
240  #endif
241 
242  // Check for execute, only checking existance in MSVC
243  #define X_OK 0
244 
245  #if (_MSC_VER < 1800)
246  #define rint( x ) floor(x + 0.5)
247  #define round( x ) floor(x + 0.5)
248 
249  #if ( _MSC_VER < 1700)
250  #define signbit( x ) ( x < 0 )
251  #endif
252 
253  #undef M_PI
254  #define M_PI 3.14159265358979323846
255  #endif
256 
257  #define getpid() _getpid()
258  #define ftruncate( fd, fsize ) _chsize( fd, fsize )
259 
260  #ifndef S_ISCHR
261  # ifdef S_IFCHR
262  # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
263  # else
264  # define S_ISCHR(m) 0
265  # endif
266  #endif /* !S_ISCHR */
267 
268  #ifndef S_ISBLK
269  # define S_ISBLK(m) 0
270  #endif
271 
272  #ifndef S_ISREG
273  # define S_ISREG(m) 1
274  #endif
275 
276  #ifndef S_ISDIR
277  # ifdef S_IFDIR
278  # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
279  # else
280  # define S_ISDIR(m) 0
281  # endif
282  #endif
283 
284  using mode_t = uint32_t;
285 
286  #if !defined(__cplusplus) && !defined( inline )
287  # define inline __inline
288  #endif
289 
290  #if !defined(__func__) // C99 & C++11
291  # define __func__ __FUNCTION__
292  #endif
293 
294 # define SIGTRAP SIGBREAK
295 # define STDERR_FILENO (int)GetStdHandle( STD_ERROR_HANDLE )
296 
297 
298 # if !defined(gmtime_r)
299 // FFmpeg libs already have a workaround, use it if the headers are included,
300 // use this otherwise.
301 static __inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
302 {
303  // this is safe on windows, where gmtime uses a thread local variable.
304  // using _gmtime_s() would be better, but needs to be tested on windows.
305  struct tm *tmp = gmtime(timep);
306  if (tmp)
307  {
308  *result = *tmp;
309  return result;
310  }
311  return nullptr;
312 }
313 # endif
314 
315 # if !defined(localtime_r)
316 // FFmpeg libs already have a workaround, use it if the headers are included,
317 // use this otherwise.
318 static __inline struct tm *localtime_r(const time_t *timep, struct tm *result)
319 {
320  // this is safe, windows uses a thread local variable for localtime().
321  if (timep && result)
322  {
323  struct tm *win_tmp = localtime(timep);
324  memcpy(result, win_tmp, sizeof(struct tm));
325  return result;
326  }
327  return nullptr;
328 }
329 # endif
330 
331 #include <sys/stat.h> // S_IREAD/WRITE on MinGW
332 # define S_IRUSR _S_IREAD
333 # ifndef lseek64
334 # define lseek64( f, o, w ) _lseeki64( f, o, w )
335 # endif
336 
337 #endif // _MSC_VER
338 
339 
340 #ifndef O_NONBLOCK
341 # define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
342 #endif
343 
344 #endif // COMPAT_H
tmp
static guint32 * tmp
Definition: goom_core.cpp:26
statfs
int statfs(const char *path, struct statfs *buffer)
Definition: compat.h:105
dlerror
const char * dlerror(void)
Definition: compat.h:160
uint
unsigned int uint
Definition: compat.h:81
off_t
#define off_t
Definition: mythiowrapper.cpp:241
statfs::f_blocks
long f_blocks
Definition: compat.h:96
statfs
Definition: compat.h:93
statfs::f_bsize
long f_bsize
Definition: compat.h:95
DLERR_MAX
#define DLERR_MAX
statfs::f_bavail
long f_bavail
Definition: compat.h:98