hplatform.h 5.2 KB

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