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 #endif // _WIN32
201 
202 
203 #ifdef _MSC_VER
204  #include <cstdlib> // for rand()
205  #include <ctime>
206  #include <sys/time.h>
207 
208  // Turn off the visual studio warnings (identifier was truncated)
209  #pragma warning(disable:4786)
210 
211  #ifdef restrict
212  #undef restrict
213  #endif
214 
215  #include <cinttypes>
216  #include <direct.h>
217  #include <process.h>
218 
219  #define strtoll _strtoi64
220  #define strncasecmp _strnicmp
221  #define snprintf _snprintf
222 
223  #ifdef _WIN64
224  using ssize_t = __int64;
225  #else
226  using ssize_t = int;
227  #endif
228 
229  // Check for execute, only checking existance in MSVC
230  #define X_OK 0
231 
232  #if (_MSC_VER < 1800)
233  #define rint( x ) floor(x + 0.5)
234  #define round( x ) floor(x + 0.5)
235 
236  #if ( _MSC_VER < 1700)
237  #define signbit( x ) ( x < 0 )
238  #endif
239 
240  #undef M_PI
241  #define M_PI 3.14159265358979323846
242  #endif
243 
244  #define getpid() _getpid()
245  #define ftruncate( fd, fsize ) _chsize( fd, fsize )
246 
247  #ifndef S_ISCHR
248  # ifdef S_IFCHR
249  # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
250  # else
251  # define S_ISCHR(m) 0
252  # endif
253  #endif /* !S_ISCHR */
254 
255  #ifndef S_ISBLK
256  # define S_ISBLK(m) 0
257  #endif
258 
259  #ifndef S_ISREG
260  # define S_ISREG(m) 1
261  #endif
262 
263  #ifndef S_ISDIR
264  # ifdef S_IFDIR
265  # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
266  # else
267  # define S_ISDIR(m) 0
268  # endif
269  #endif
270 
271  using mode_t = uint32_t;
272 
273  #if !defined(__cplusplus) && !defined( inline )
274  # define inline __inline
275  #endif
276 
277  #if !defined(__func__) // C99 & C++11
278  # define __func__ __FUNCTION__
279  #endif
280 
281 # define SIGTRAP SIGBREAK
282 # define STDERR_FILENO (int)GetStdHandle( STD_ERROR_HANDLE )
283 
284 
285 # if !defined(gmtime_r)
286 // FFmpeg libs already have a workaround, use it if the headers are included,
287 // use this otherwise.
288 static __inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
289 {
290  // this is safe on windows, where gmtime uses a thread local variable.
291  // using _gmtime_s() would be better, but needs to be tested on windows.
292  struct tm *tmp = gmtime(timep);
293  if (tmp)
294  {
295  *result = *tmp;
296  return result;
297  }
298  return nullptr;
299 }
300 # endif
301 
302 # if !defined(localtime_r)
303 // FFmpeg libs already have a workaround, use it if the headers are included,
304 // use this otherwise.
305 static __inline struct tm *localtime_r(const time_t *timep, struct tm *result)
306 {
307  // this is safe, windows uses a thread local variable for localtime().
308  if (timep && result)
309  {
310  struct tm *win_tmp = localtime(timep);
311  memcpy(result, win_tmp, sizeof(struct tm));
312  return result;
313  }
314  return nullptr;
315 }
316 # endif
317 
318 #include <sys/stat.h> // S_IREAD/WRITE on MinGW
319 # define S_IRUSR _S_IREAD
320 # ifndef lseek64
321 # define lseek64( f, o, w ) _lseeki64( f, o, w )
322 # endif
323 
324 #endif // _MSC_VER
325 
326 
327 #ifndef O_NONBLOCK
328 # define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
329 #endif
330 
331 #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
uint
unsigned int uint
Definition: freesurround.h:24