00001
00002
00003
00004
00005 #ifndef __COMPAT_H__
00006 #define __COMPAT_H__
00007
00008 #ifdef _WIN32
00009 # ifndef _MSC_VER
00010 # define close wsock_close
00011 # endif
00012
00013 #ifndef NOMINMAX
00014 #define NOMINMAX
00015 #endif
00016
00017 #include <windows.h>
00018
00019 # undef DialogBox
00020 # undef LoadImage
00021 # undef LoadIcon
00022 # undef GetObject
00023 # undef DrawText
00024 # undef CreateDialog
00025 # undef CreateFont
00026 # undef DeleteFile
00027 # undef GetCurrentTime
00028 # undef SetJob
00029 # undef SendMessage
00030
00031 #ifndef _MSC_VER
00032 # include <winsock2.h>
00033 # include <ws2tcpip.h>
00034 #else
00035 # include <io.h>
00036 # endif
00037
00038 # define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char*)(d), e)
00039 # undef close
00040 # include <stdio.h>
00041 # include <unistd.h>
00042 #else
00043 # include <sys/time.h>
00044 # include <sys/resource.h>
00045 # include <sys/socket.h>
00046 # include <sys/wait.h>
00047 # include <stdio.h>
00048 # include <unistd.h>
00049 #endif
00050
00051 #ifdef USING_MINGW
00052 #include <unistd.h>
00053 #include <stdlib.h>
00054 #include <time.h>
00055 #include <sys/time.h>
00056 #endif
00057
00058 #ifdef _MSC_VER
00059
00060 #pragma warning(disable:4786)
00061
00062 #ifdef restrict
00063 #undef restrict
00064 #endif
00065
00066 #include <inttypes.h>
00067 #include <direct.h>
00068 #include <process.h>
00069
00070 #define strtoll _strtoi64
00071 #define strncasecmp _strnicmp
00072 #define snprintf _snprintf
00073
00074 #ifdef _WIN64
00075 typedef __int64 ssize_t;
00076 #else
00077 typedef int ssize_t;
00078 #endif
00079
00080
00081 #define X_OK 0
00082
00083 #define rint( x ) floor(x + 0.5)
00084 #define round( x ) floor(x + 0.5)
00085 #define getpid() _getpid()
00086 #define ftruncate( fd, fsize ) _chsize( fd, fsize )
00087
00088 #ifndef S_ISCHR
00089 # ifdef S_IFCHR
00090 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
00091 # else
00092 # define S_ISCHR(m) 0
00093 # endif
00094 #endif
00095
00096 #ifndef S_ISBLK
00097 # define S_ISBLK(m) 0
00098 #endif
00099
00100 #ifndef S_ISREG
00101 # define S_ISREG(m) 1
00102 #endif
00103
00104 #ifndef S_ISDIR
00105 # ifdef S_IFDIR
00106 # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
00107 # else
00108 # define S_ISDIR(m) 0
00109 # endif
00110 #endif
00111 #endif
00112
00113 #ifdef _WIN32
00114 typedef unsigned int uint;
00115 # undef M_PI
00116 # define M_PI 3.14159265358979323846
00117 #endif
00118
00119 #ifdef USING_MINGW
00120 #define fsync(FD) 0
00121
00122 #define minor(X) 0
00123 #endif
00124
00125 #if defined(__cplusplus) && defined(USING_MINGW)
00126 #include <QtGlobal>
00127 static inline void srandom(unsigned int seed) { qsrand(seed); }
00128 static inline long int random(void) { return qrand(); }
00129 #endif // USING_MINGW
00130
00131 #if defined(__cplusplus) && defined(USING_MINGW)
00132 #define setenv(x, y, z) ::SetEnvironmentVariableA(x, y)
00133 #define unsetenv(x) 0
00134 #endif
00135
00136 #if defined(__cplusplus) && defined(USING_MINGW)
00137 inline unsigned sleep(unsigned int x)
00138 {
00139 Sleep(x * 1000);
00140 return 0;
00141 }
00142 #endif // defined(__cplusplus) && defined(USING_MINGW)
00143
00144 #if defined(__cplusplus) && defined(USING_MINGW)
00145 struct statfs {
00146
00147 long f_bsize;
00148 long f_blocks;
00149
00150 long f_bavail;
00151
00152
00153
00154
00155
00156 };
00157 inline int statfs(const char* path, struct statfs* buffer)
00158 {
00159 DWORD spc = 0, bps = 0, fc = 0, c = 0;
00160
00161 if (buffer && GetDiskFreeSpaceA(path, &spc, &bps, &fc, &c))
00162 {
00163 buffer->f_bsize = bps;
00164 buffer->f_blocks = spc * c;
00165 buffer->f_bavail = spc * fc;
00166 return 0;
00167 }
00168
00169 return -1;
00170 }
00171 #endif // USING_MINGW
00172
00173 #ifdef USING_MINGW
00174 #define lstat stat
00175 #define nice(x) ((int)!::SetPriorityClass(\
00176 ::GetCurrentProcess(), ((x) < -10) ? \
00177 HIGH_PRIORITY_CLASS : (((x) < 0) ? \
00178 ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
00179 IDLE_PRIORITY_CLASS : (((x) > 0) ? \
00180 BELOW_NORMAL_PRIORITY_CLASS : \
00181 NORMAL_PRIORITY_CLASS)))))
00182 #define PRIO_PROCESS 0
00183 #define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
00184 #endif // USING_MINGW
00185
00186 #ifdef USING_MINGW
00187
00188 #define SIGHUP 1
00189 #define SIGQUIT 3
00190 #define SIGKILL 9
00191 #define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
00192 #define SIGUSR2 12 // used to restart LIRC as required
00193 #define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
00194 #define SIGALRM 14
00195 #define SIGCONT 18
00196 #define SIGSTOP 19
00197
00198 #define S_IRGRP 0
00199 #define S_IROTH 0
00200 #define O_SYNC 0
00201 #endif // USING_MINGW
00202
00203 #ifdef USING_MINGW
00204 #define mkfifo(path, mode) \
00205 (int)CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
00206 PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
00207 1024, 1024, 10000, NULL)
00208 #endif // USING_MINGW
00209
00210 #ifdef USING_MINGW
00211 #define RTLD_LAZY 0
00212 #define dlopen(x, y) LoadLibraryA((x))
00213 #define dlclose(x) !FreeLibrary((HMODULE)(x))
00214 #define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
00215 #ifdef __cplusplus
00216 inline const char *dlerror(void)
00217 {
00218 #define DLERR_MAX 512
00219 static char errStr[DLERR_MAX];
00220 DWORD errCode = GetLastError();
00221
00222 if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
00223 FORMAT_MESSAGE_IGNORE_INSERTS |
00224 FORMAT_MESSAGE_MAX_WIDTH_MASK,
00225 NULL, errCode,
00226 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00227 errStr, DLERR_MAX - 1, NULL))
00228 snprintf(errStr, DLERR_MAX - 1,
00229 "dlopen()/dlsym() caused error %d", (int)errCode);
00230
00231 return errStr;
00232 }
00233 #else // __cplusplus
00234 #define dlerror() "dlerror() is unimplemented."
00235 #endif // __cplusplus
00236 #endif // USING_MINGW
00237
00238 #ifdef USING_MINGW
00239
00240 #define getuid() 0
00241 #define geteuid() 0
00242 #define setuid(x)
00243 #endif // USING_MINGW
00244
00245 #if defined(USING_MINGW) && !defined(gmtime_r)
00246
00247
00248 static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
00249 {
00250
00251
00252 struct tm *tmp = gmtime(timep);
00253 if (tmp)
00254 {
00255 *result = *tmp;
00256 return result;
00257 }
00258 return NULL;
00259 }
00260 #endif
00261
00262 #if defined(USING_MINGW) && !defined(localtime_r)
00263
00264
00265 static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
00266 {
00267
00268 if (timep && result)
00269 {
00270 struct tm *win_tmp = localtime(timep);
00271 memcpy(result, win_tmp, sizeof(struct tm));
00272 return result;
00273 }
00274 return NULL;
00275 }
00276 #endif
00277
00278 #ifdef USING_MINGW
00279 #define timeradd(a, b, result) \
00280 do { \
00281 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
00282 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
00283 if ((result)->tv_usec >= 1000000) \
00284 { \
00285 ++(result)->tv_sec; \
00286 (result)->tv_usec -= 1000000; \
00287 } \
00288 } while (0)
00289 #define timersub(a, b, result) \
00290 do { \
00291 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
00292 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
00293 if ((result)->tv_usec < 0) { \
00294 --(result)->tv_sec; \
00295 (result)->tv_usec += 1000000; \
00296 } \
00297 } while (0)
00298 #endif // USING_MINGW
00299
00300 #ifdef USING_MINGW
00301
00302 #define daemon(x, y) -1
00303 #define getloadavg(x, y) -1
00304 #endif // USING_MINGW
00305
00306 #ifdef USING_MINGW
00307
00308 #define WIFEXITED(w) (((w) & 0xff) == 0)
00309 #define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
00310 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
00311 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
00312 #define WTERMSIG(w) ((w) & 0x7f)
00313 #endif // USING_MINGW
00314
00315
00316 #ifndef _MSC_VER
00317 #include <sys/param.h>
00318 #endif
00319 #include <sys/stat.h>
00320
00321
00322
00323 #include <sys/types.h>
00324
00325 #ifdef USING_MINGW
00326 typedef long suseconds_t;
00327 #endif
00328
00329 #include "mythconfig.h"
00330
00331 #if CONFIG_DARWIN && ! defined (_SUSECONDS_T)
00332 typedef int32_t suseconds_t;
00333 #endif
00334
00335
00336
00337 #ifdef BSD
00338 typedef off_t off64_t;
00339 #define lseek64(f,o,w) lseek(f,o,w)
00340 #endif
00341
00342 #if defined(_MSC_VER)
00343 # define S_IRUSR _S_IREAD
00344 # ifndef lseek64
00345 # define lseek64( f, o, w ) _lseeki64( f, o, w )
00346 # endif
00347 #endif
00348
00349 #ifdef USING_MINGW
00350 #define fseeko(stream, offset, whence) fseeko64(stream, offset, whence)
00351 #define ftello(stream) ftello64(stream)
00352 #endif
00353
00354 #include <stdio.h>
00355 #if defined(USING_MINGW) && defined(FILENAME_MAX) && !defined( _MSC_VER )
00356 #include <errno.h>
00357 #include <dirent.h>
00358 #include <string.h>
00359 #include <stddef.h>
00360 static inline int readdir_r(
00361 DIR *dirp, struct dirent *entry, struct dirent **result)
00362 {
00363 errno = 0;
00364 struct dirent *tmp = readdir(dirp);
00365 if (tmp && entry)
00366 {
00367 int offset = offsetof(struct dirent, d_name);
00368 memcpy(entry, tmp, offset);
00369 strncpy(entry->d_name, tmp->d_name, FILENAME_MAX);
00370 tmp->d_name[strlen(entry->d_name)] = '\0';
00371 if (result)
00372 *result = entry;
00373 return 0;
00374 }
00375 else
00376 {
00377 if (result)
00378 *result = NULL;
00379 return errno;
00380 }
00381 }
00382 #endif
00383
00384 #ifdef _WIN32
00385 #define PREFIX64 "I64"
00386 #else
00387 #define PREFIX64 "ll"
00388 #endif
00389
00390 #endif // __COMPAT_H__