hplatform.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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: 4819) // Unicode
  48. #pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
  49. #endif
  50. // __MINGW32__
  51. // __GNUC__
  52. #ifdef __GNUC__
  53. #ifndef _GNU_SOURCE
  54. #define _GNU_SOURCE 1
  55. #endif
  56. #endif
  57. // __clang__
  58. // HV_EXPORT
  59. #ifdef HV_STATICLIB
  60. #define HV_EXPORT
  61. #elif defined(OS_WIN)
  62. #if defined(HV_EXPORTS) || defined(hv_EXPORTS)
  63. #define HV_EXPORT __declspec(dllexport)
  64. #else
  65. #define HV_EXPORT __declspec(dllimport)
  66. #endif
  67. #elif defined(__GNUC__)
  68. #define HV_EXPORT __attribute__((visibility("default")))
  69. #else
  70. #define HV_EXPORT
  71. #endif
  72. // ARCH
  73. #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  74. #define ARCH_X86
  75. #define ARCH_X86_32
  76. #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
  77. #define ARCH_X64
  78. #define ARCH_X86_64
  79. #elif defined(__arm__)
  80. #define ARCH_ARM
  81. #elif defined(__aarch64__) || defined(__ARM64__)
  82. #define ARCH_ARM64
  83. #endif
  84. // ENDIAN
  85. #ifndef BIG_ENDIAN
  86. #define BIG_ENDIAN 4321
  87. #endif
  88. #ifndef LITTLE_ENDIAN
  89. #define LITTLE_ENDIAN 1234
  90. #endif
  91. #define NET_ENDIAN BIG_ENDIAN
  92. #ifndef BYTE_ORDER
  93. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
  94. #define BYTE_ORDER LITTLE_ENDIAN
  95. #elif defined(__ARMEB__)
  96. #define BYTE_ORDER BIG_ENDIAN
  97. #endif
  98. #endif
  99. // headers
  100. #ifdef OS_WIN
  101. #ifndef WIN32_LEAN_AND_MEAN
  102. #define WIN32_LEAN_AND_MEAN
  103. #endif
  104. #define _CRT_NONSTDC_NO_DEPRECATE
  105. #define _CRT_SECURE_NO_WARNINGS
  106. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  107. #include <winsock2.h>
  108. #include <ws2tcpip.h> // for inet_pton,inet_ntop
  109. #include <windows.h>
  110. #include <process.h> // for getpid,exec
  111. #include <direct.h> // for mkdir,rmdir,chdir,getcwd
  112. #include <io.h> // for open,close,read,write,lseek,tell
  113. #define hv_delay(ms) Sleep(ms)
  114. #define hv_mkdir(dir) mkdir(dir)
  115. #ifndef S_ISREG
  116. #define S_ISREG(st_mode) (((st_mode) & S_IFMT) == S_IFREG)
  117. #endif
  118. #ifndef S_ISDIR
  119. #define S_ISDIR(st_mode) (((st_mode) & S_IFMT) == S_IFDIR)
  120. #endif
  121. #else
  122. #include <unistd.h>
  123. #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
  124. // socket
  125. #include <sys/socket.h>
  126. #include <sys/select.h>
  127. #include <arpa/inet.h>
  128. #include <netinet/in.h>
  129. #include <netinet/tcp.h>
  130. #include <netinet/udp.h>
  131. #include <netdb.h> // for gethostbyname
  132. #define hv_delay(ms) usleep((ms)*1000)
  133. #define hv_mkdir(dir) mkdir(dir, 0777)
  134. #endif
  135. #ifdef _MSC_VER
  136. typedef int pid_t;
  137. typedef int gid_t;
  138. typedef int uid_t;
  139. #define strcasecmp stricmp
  140. #define strncasecmp strnicmp
  141. #else
  142. typedef int BOOL;
  143. typedef unsigned char BYTE;
  144. typedef unsigned short WORD;
  145. typedef void* HANDLE;
  146. #include <strings.h>
  147. #define stricmp strcasecmp
  148. #define strnicmp strncasecmp
  149. #endif
  150. // ANSI C
  151. #include <assert.h>
  152. #include <stddef.h>
  153. #include <stdarg.h>
  154. #include <stdio.h>
  155. #include <stdlib.h>
  156. #include <string.h>
  157. #include <ctype.h>
  158. #include <float.h>
  159. #include <limits.h>
  160. #include <errno.h>
  161. #include <time.h>
  162. #include <math.h>
  163. #include <signal.h>
  164. #ifndef __cplusplus
  165. #if HAVE_STDBOOL_H
  166. #include <stdbool.h>
  167. #else
  168. #ifndef bool
  169. #define bool char
  170. #endif
  171. #ifndef true
  172. #define ture 1
  173. #endif
  174. #ifndef false
  175. #define false 0
  176. #endif
  177. #endif
  178. #endif
  179. #if HAVE_STDINT_H
  180. #include <stdint.h>
  181. #elif defined(_MSC_VER) && _MSC_VER < 1700
  182. typedef __int8 int8_t;
  183. typedef __int16 int16_t;
  184. typedef __int32 int32_t;
  185. typedef __int64 int64_t;
  186. typedef unsigned __int8 uint8_t;
  187. typedef unsigned __int16 uint16_t;
  188. typedef unsigned __int32 uint32_t;
  189. typedef unsigned __int64 uint64_t;
  190. #endif
  191. typedef float float32_t;
  192. typedef double float64_t;
  193. // sizeof(var) = 8
  194. typedef union {
  195. bool b;
  196. char ch;
  197. char* str;
  198. long long num;
  199. float f;
  200. double lf;
  201. void* ptr;
  202. } var;
  203. typedef int (*method_t)(void* userdata);
  204. typedef void (*procedure_t)(void* userdata);
  205. #if HAVE_SYS_TYPES_H
  206. #include <sys/types.h>
  207. #endif
  208. #if HAVE_SYS_STAT_H
  209. #include <sys/stat.h>
  210. #endif
  211. #if HAVE_FCNTL_H
  212. #include <fcntl.h>
  213. #endif
  214. #ifndef _MSC_VER
  215. #if HAVE_SYS_TIME_H
  216. #include <sys/time.h> // for gettimeofday
  217. #endif
  218. #if HAVE_PTHREAD_H
  219. #include <pthread.h>
  220. #endif
  221. #endif
  222. #endif // HV_PLATFORM_H_