hplatform.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef HV_PLATFORM_H_
  2. #define HV_PLATFORM_H_
  3. #include "hconfig.h"
  4. // OS
  5. #if defined(WIN64) || defined(_WIN64)
  6. #define OS_WIN64
  7. #define OS_WIN32
  8. #elif defined(WIN32)|| defined(_WIN32)
  9. #define OS_WIN32
  10. #elif defined(ANDROID) || defined(__ANDROID__)
  11. #define OS_ANDROID
  12. #define OS_LINUX
  13. #elif defined(linux) || defined(__linux) || defined(__linux__)
  14. #define OS_LINUX
  15. #elif defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
  16. #include <TargetConditionals.h>
  17. #if defined(TARGET_OS_MAC) && TARGET_OS_MAC
  18. #define OS_MAC
  19. #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
  20. #define OS_IOS
  21. #endif
  22. #define OS_DARWIN
  23. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  24. #define OS_FREEBSD
  25. #define OS_BSD
  26. #elif defined(__NetBSD__)
  27. #define OS_NETBSD
  28. #define OS_BSD
  29. #elif defined(__OpenBSD__)
  30. #define OS_OPENBSD
  31. #define OS_BSD
  32. #elif defined(sun) || defined(__sun) || defined(__sun__)
  33. #define OS_SOLARIS
  34. #else
  35. #error "Unsupported operating system platform!"
  36. #endif
  37. #if defined(OS_WIN32) || defined(OS_WIN64)
  38. #undef OS_UNIX
  39. #define OS_WIN
  40. #else
  41. #define OS_UNIX
  42. #endif
  43. // CC
  44. // _MSC_VER
  45. #ifdef _MSC_VER
  46. #pragma warning (disable: 4100) // unused param
  47. #pragma warning (disable: 4251) // STL dll
  48. #pragma warning (disable: 4819) // Unicode
  49. #pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
  50. #endif
  51. // __MINGW32__
  52. // __GNUC__
  53. #ifdef __GNUC__
  54. #ifndef _GNU_SOURCE
  55. #define _GNU_SOURCE 1
  56. #endif
  57. #endif
  58. // __clang__
  59. // ARCH
  60. #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  61. #define ARCH_X86
  62. #define ARCH_X86_32
  63. #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
  64. #define ARCH_X64
  65. #define ARCH_X86_64
  66. #elif defined(__arm__)
  67. #define ARCH_ARM
  68. #elif defined(__aarch64__) || defined(__ARM64__)
  69. #define ARCH_ARM64
  70. #endif
  71. // ENDIAN
  72. #ifndef BIG_ENDIAN
  73. #define BIG_ENDIAN 4321
  74. #endif
  75. #ifndef LITTLE_ENDIAN
  76. #define LITTLE_ENDIAN 1234
  77. #endif
  78. #define NET_ENDIAN BIG_ENDIAN
  79. #ifndef BYTE_ORDER
  80. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
  81. #define BYTE_ORDER LITTLE_ENDIAN
  82. #elif defined(__ARMEB__)
  83. #define BYTE_ORDER BIG_ENDIAN
  84. #endif
  85. #endif
  86. // headers
  87. #ifdef OS_WIN
  88. #ifndef WIN32_LEAN_AND_MEAN
  89. #define WIN32_LEAN_AND_MEAN
  90. #endif
  91. #define _CRT_NONSTDC_NO_DEPRECATE
  92. #define _CRT_SECURE_NO_WARNINGS
  93. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  94. #include <winsock2.h>
  95. #include <ws2tcpip.h> // for inet_pton,inet_ntop
  96. #include <windows.h>
  97. #include <process.h> // for getpid,exec
  98. #include <direct.h> // for mkdir,rmdir,chdir,getcwd
  99. #include <io.h> // for open,close,read,write,lseek,tell
  100. #define hv_delay(ms) Sleep(ms)
  101. #define hv_mkdir(dir) mkdir(dir)
  102. #ifndef S_ISREG
  103. #define S_ISREG(st_mode) (((st_mode) & S_IFMT) == S_IFREG)
  104. #endif
  105. #ifndef S_ISDIR
  106. #define S_ISDIR(st_mode) (((st_mode) & S_IFMT) == S_IFDIR)
  107. #endif
  108. #else
  109. #include <unistd.h>
  110. #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
  111. // socket
  112. #include <sys/socket.h>
  113. #include <sys/select.h>
  114. #include <arpa/inet.h>
  115. #include <netinet/in.h>
  116. #include <netinet/tcp.h>
  117. #include <netinet/udp.h>
  118. #include <netdb.h> // for gethostbyname
  119. #define hv_delay(ms) usleep((ms)*1000)
  120. #define hv_mkdir(dir) mkdir(dir, 0777)
  121. #endif
  122. #ifdef _MSC_VER
  123. typedef int pid_t;
  124. typedef int gid_t;
  125. typedef int uid_t;
  126. #define strcasecmp stricmp
  127. #define strncasecmp strnicmp
  128. #else
  129. typedef int BOOL;
  130. typedef unsigned char BYTE;
  131. typedef unsigned short WORD;
  132. typedef void* HANDLE;
  133. #include <strings.h>
  134. #define stricmp strcasecmp
  135. #define strnicmp strncasecmp
  136. #endif
  137. // ANSI C
  138. #include <assert.h>
  139. #include <stddef.h>
  140. #include <stdarg.h>
  141. #include <stdio.h>
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include <ctype.h>
  145. #include <float.h>
  146. #include <limits.h>
  147. #include <errno.h>
  148. #include <time.h>
  149. #include <math.h>
  150. #include <signal.h>
  151. #ifndef __cplusplus
  152. #if HAVE_STDBOOL_H
  153. #include <stdbool.h>
  154. #else
  155. #ifndef bool
  156. #define bool char
  157. #endif
  158. #ifndef true
  159. #define ture 1
  160. #endif
  161. #ifndef false
  162. #define false 0
  163. #endif
  164. #endif
  165. #endif
  166. #if HAVE_STDINT_H
  167. #include <stdint.h>
  168. #elif defined(_MSC_VER) && _MSC_VER < 1700
  169. typedef __int8 int8_t;
  170. typedef __int16 int16_t;
  171. typedef __int32 int32_t;
  172. typedef __int64 int64_t;
  173. typedef unsigned __int8 uint8_t;
  174. typedef unsigned __int16 uint16_t;
  175. typedef unsigned __int32 uint32_t;
  176. typedef unsigned __int64 uint64_t;
  177. #endif
  178. typedef float float32_t;
  179. typedef double float64_t;
  180. // sizeof(var) = 8
  181. typedef union {
  182. bool b;
  183. char ch;
  184. char* str;
  185. long long num;
  186. float f;
  187. double lf;
  188. void* ptr;
  189. } var;
  190. typedef int (*method_t)(void* userdata);
  191. typedef void (*procedure_t)(void* userdata);
  192. #if HAVE_SYS_TYPES_H
  193. #include <sys/types.h>
  194. #endif
  195. #if HAVE_SYS_STAT_H
  196. #include <sys/stat.h>
  197. #endif
  198. #if HAVE_FCNTL_H
  199. #include <fcntl.h>
  200. #endif
  201. #ifndef _MSC_VER
  202. #if HAVE_SYS_TIME_H
  203. #include <sys/time.h> // for gettimeofday
  204. #endif
  205. #if HAVE_PTHREAD_H
  206. #include <pthread.h>
  207. #endif
  208. #endif
  209. #endif // HV_PLATFORM_H_