sd_xplatform.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* $Id$
  2. *
  3. * sd_xplatform.h
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #ifndef __sd_xplatform_h
  8. #define __sd_xplatform_h
  9. #ifndef _WIN32
  10. #include <unistd.h>
  11. #include <sys/time.h>
  12. #include <sys/stat.h>
  13. #else
  14. #include <time.h>
  15. #include <io.h> /* needed for _access */
  16. #define WIN32_LEAN_AND_MEAN
  17. #include <windows.h>
  18. #include <winsock2.h>
  19. #include <process.h>
  20. #endif
  21. #ifdef HAVE_STDINT_H
  22. # include <stdint.h>
  23. #define XP_UINT64 uint64_t
  24. #define XP_INT64 int64_t
  25. #else
  26. #ifndef _WIN32
  27. #define XP_UINT64 unsigned long long
  28. #define XP_INT64 long long
  29. #else
  30. #define XP_UINT64 DWORD64
  31. #define XP_INT64 __int64
  32. #endif
  33. #endif
  34. #include <log4c/defs.h>
  35. #include <sd/defs.h>
  36. /*extern int sd_optind; */
  37. LOG4C_DATA int sd_optind;
  38. extern void getopt_reset(void);
  39. extern int sd_getopt(int argc, char *const *argv, const char *opts);
  40. #ifdef _WIN32
  41. #define SD_GETOPT(a,b,c) sd_getopt(a,b,c)
  42. #define SD_OPTIND sd_optind
  43. #else
  44. #define SD_GETOPT(a,b,c) getopt(a,b,c)
  45. #define SD_OPTIND optind
  46. #endif
  47. #ifdef _WIN32
  48. #define SD_GETTIMEOFDAY(a,b) sd_gettimeofday(a,b)
  49. extern int sd_gettimeofday(LPFILETIME lpft, void* tzp);
  50. #else
  51. #define SD_GETTIMEOFDAY(a,b) gettimeofday(a,b)
  52. extern int sd_gettimeofday(struct timeval* tp, void* tzp);
  53. #endif
  54. #ifdef _WIN32
  55. #define FILE_SEP "\\"
  56. #else
  57. #define FILE_SEP "/"
  58. #endif
  59. #ifdef _WIN32
  60. #define SD_ACCESS_READ(a) _access(a,04)
  61. #else
  62. #define SD_ACCESS_READ(a) access(a,R_OK)
  63. #endif
  64. int sd_stat_ctime(const char* path, time_t* time);
  65. #define SD_STAT_CTIME(path, time) sd_stat_ctime(path, time)
  66. #ifndef _WIN32
  67. #define DIFF_CMD "/usr/bin/diff -q"
  68. #else
  69. #define DIFF_CMD "comp.exe"
  70. #endif
  71. #ifdef _WIN32
  72. #if defined(__MINGW32__) || defined(__MINGW64__)
  73. #define snprintf sd_snprintf
  74. #define vsnprintf sd_vsnprintf
  75. int sd_snprintf(char *str, size_t size, const char *format, ...) SD_ATTRIBUTE((format(printf, 3, 4)));
  76. int sd_vsnprintf(char *str, size_t size, const char *format, va_list ap);
  77. #else
  78. #define snprintf _snprintf
  79. #define vsnprintf _vsnprintf
  80. #endif
  81. #if !defined(HAVE_ALLOC_H) && defined(HAVE_MALLOC_H) && !defined(alloca)
  82. #define alloca _alloca
  83. #endif
  84. #define strncasecmp strnicmp
  85. #define strcasecmp stricmp
  86. #define YY_NO_UNISTD_H
  87. #if !defined(HAVE_SLEEP) || !HAVE_DECL_SLEEP
  88. #define sleep(x) Sleep(x*1000)
  89. #endif
  90. #endif
  91. /* Maybe should be using this for to mean
  92. * MS compiler #if defined(_MSC_VER)
  93. */
  94. #ifdef _WIN32
  95. #define pthread_t HANDLE
  96. #define pthread_mutex_t HANDLE
  97. #define pthread_attr_t DWORD
  98. #define THREAD_FUNCTION DWORD (WINAPI *)(void *)
  99. /*
  100. * This one not obvious: you would have naturally thought of mapping to
  101. * CreateThread()--turns out that to be safe using CRT functions
  102. * you need to use _begintheadex().
  103. * cf. http://msdn2.microsoft.com/en-us/library/7t9ha0zh.aspx
  104. * http://groups.google.com/group/comp.os.ms-windows.programmer.win32/browse_thread/thread/86d8624e7ee38c5d/f947ac76cd10f397?lnk=st&q=when+to+use+_beginthreadex&rnum=1#f947ac76cd10f397
  105. *
  106. */
  107. #define pthread_create(thhandle,attr,thfunc,tharg) \
  108. (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(THREAD_FUNCTION)thfunc,tharg,0,NULL))==NULL)
  109. #define pthread_join(thread, result) \
  110. ((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
  111. #define pthread_exit() _endthreadex(0)
  112. #define pthread_cancel(thread) TerminateThread(thread,0)
  113. #define pthread_mutex_init(pobject,pattr) (*pobject=CreateMutex(NULL,FALSE,NULL))
  114. #define pthread_mutex_lock(pobject) WaitForSingleObject(*pobject,INFINITE)
  115. #define pthread_mutex_unlock(pobject) ReleaseMutex(*pobject)
  116. #define pthread_mutex_destroy(pobject) CloseHandle(*pobject)
  117. #endif
  118. #ifdef __HP_cc
  119. #define inline __inline
  120. #endif
  121. #endif