YDLIDAR SDK  V1.3.6
Host.h
1 /*---------------------------------------------------------------------------*/
2 /* */
3 /* Host.h - Basic header file to provide cross-platform solutions via */
4 /* macros, conditional compilation, etc. */
5 /* */
6 /* Author : Mark Carrier (mark@carrierlabs.com) */
7 /* */
8 /*---------------------------------------------------------------------------*/
9 /* Copyright (c) 2007 CarrierLabs, LLC. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The name of the author may not be used to endorse or promote products
24  * derived from this software without specific prior written permission.
25  *
26  * 4. The name "CarrierLabs" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please contact
29  * mark@carrierlabs.com.
30  *
31  * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
32  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
35  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *----------------------------------------------------------------------------*/
44 #ifndef __HOST_H__
45 #define __HOST_H__
46 
47 #include <limits.h>
48 
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif
53 
54 /*---------------------------------------------------------------------------*/
55 /* */
56 /* Type Definition Macros */
57 /* */
58 /*---------------------------------------------------------------------------*/
59 #ifndef __WORDSIZE
60  /* Assume 32 */
61  #define __WORDSIZE 32
62 #endif
63 
64 #if defined(__linux__) || defined(_DARWIN)
65 #include <stdint.h>
66  typedef int SOCKET;
67 #endif
68 
69 
70 #if defined(_WIN32)
71  struct iovec {
72  void *iov_base;
73  size_t iov_len;
74  };
75 
76  typedef unsigned char uint8_t;
77  // typedef char int8_t;
78  typedef unsigned short uint16_t;
79  typedef short int16_t;
80  typedef unsigned int uint32_t;
81  typedef int int32_t;
82 #endif
83 
84 #if defined(_WIN32)
85  typedef int socklen_t;
86 #endif
87 
88 #if defined(_WIN32)
89  typedef unsigned long long int uint64_t;
90  typedef long long int int64_t;
91 #elif (__WORDSIZE == 32)
92  __extension__
93  typedef long long int int64_t;
94  __extension__
95  typedef unsigned long long int uint64_t;
96 #elif (__WORDSIZE == 64)
97 // typedef unsigned long int uint64_t;
98 // typedef long int int64_t;
99 #endif
100 
101 #if defined(_WIN32)
102 
103  #ifndef UINT8_MAX
104  #define UINT8_MAX (UCHAR_MAX)
105  #endif
106  #ifndef UINT16_MAX
107  #define UINT16_MAX (USHRT_MAX)
108  #endif
109  #ifndef UINT32_MAX
110  #define UINT32_MAX (ULONG_MAX)
111  #endif
112 
113  #if __WORDSIZE == 64
114  #define SIZE_MAX (18446744073709551615UL)
115  #else
116  #ifndef SIZE_MAX
117  #define SIZE_MAX (4294967295U)
118  #endif
119  #endif
120 #endif
121 
122 #if defined(_WIN32)
123  #define ssize_t size_t
124 #endif
125 
126 #define __small_endian
127 
128 #ifndef __GNUC__
129 #define __attribute__(x)
130 #endif
131 
132 #ifndef TRUE
133  #define TRUE 1
134 #endif
135 
136 #ifndef FALSE
137  #define FALSE 0
138 #endif
139 
140 #ifndef htonll
141 #ifdef _BIG_ENDIAN
142 #define htonll(x) (x)
143 #define ntohll(x) (x)
144 #else
145 #define htonll(x) ((((uint64)htonl(x)) << 32) + htonl(x >> 32))
146 #define ntohll(x) ((((uint64)ntohl(x)) << 32) + ntohl(x >> 32))
147 #endif
148 #endif
149 
150 /*---------------------------------------------------------------------------*/
151 /* */
152 /* Socket Macros */
153 /* */
154 /*---------------------------------------------------------------------------*/
155 #if defined(_WIN32)
156 #define SHUT_RD 0
157 #define SHUT_WR 1
158 #define SHUT_RDWR 2
159 #define ACCEPT(a,b,c) accept(a,b,c)
160 #define CONNECT(a,b,c) connect(a,b,c)
161 #define CLOSE(a) closesocket(a)
162 #define READ(a,b,c) read(a,b,c)
163 #define RECV(a,b,c,d) recv(a, (char *)b, c, d)
164 #define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
165 #define RECV_FLAGS MSG_WAITALL
166 #define SELECT(a,b,c,d,e) select((int32_t)a,b,c,d,e)
167 #define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d)
168 #define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f)
169 #define SEND_FLAGS 0
170 #define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
171 #define SET_SOCKET_ERROR(x,y) errno=y
172 #define SOCKET_ERROR_INTERUPT EINTR
173 #define SOCKET_ERROR_TIMEDOUT EAGAIN
174 #define WRITE(a,b,c) write(a,b,c)
175 #define WRITEV(a,b,c) Writev(b, c)
176 #define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e)
177 #define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e)
178 #define GETHOSTBYNAME(a) gethostbyname(a)
179 #define IOCTLSOCKET(a, b, c) ioctlsocket(a,b,(u_long*)c)
180 #endif
181 
182 #if defined(__linux__) || defined(_DARWIN)
183 #define ACCEPT(a,b,c) accept(a,b,c)
184 #define CONNECT(a,b,c) connect(a,b,c)
185 #define CLOSE(a) close(a)
186 #define READ(a,b,c) read(a,b,c)
187 #define RECV(a,b,c,d) recv(a, (void *)b, c, d)
188 #define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
189 #define RECV_FLAGS MSG_WAITALL
190 #define SELECT(a,b,c,d,e) select(a,b,c,d,e)
191 #define SEND(a,b,c,d) send(a, (const int8_t *)b, c, d)
192 #define SENDTO(a,b,c,d,e,f) sendto(a, (const int8_t *)b, c, d, e, f)
193 #define SEND_FLAGS 0
194 #define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
195 #define SET_SOCKET_ERROR(x,y) errno=y
196 #define SOCKET_ERROR_INTERUPT EINTR
197 #define SOCKET_ERROR_TIMEDOUT EAGAIN
198 #define WRITE(a,b,c) write(a,b,c)
199 #define WRITEV(a,b,c) writev(a, b, c)
200 #define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
201 #define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
202 #define GETHOSTBYNAME(a) gethostbyname(a)
203 #define IOCTLSOCKET(a, b, c) ioctl(a,b,c)
204 #endif
205 
206 
207 /*---------------------------------------------------------------------------*/
208 /* */
209 /* File Macros */
210 /* */
211 /*---------------------------------------------------------------------------*/
212 #define STRUCT_STAT struct stat
213 #define LSTAT(x,y) lstat(x,y)
214 #define FILE_HANDLE FILE *
215 #define CLEARERR(x) clearerr(x)
216 #define FCLOSE(x) fclose(x)
217 #define FEOF(x) feof(x)
218 #define FERROR(x) ferror(x)
219 #define FFLUSH(x) fflush(x)
220 #define FILENO(s) fileno(s)
221 #define FOPEN(x,y) fopen(x, y)
222  //#define FREAD(a,b,c,d) fread(a, b, c, d)
223 #define FSTAT(s, st) fstat(FILENO(s), st)
224  //#define FWRITE(a,b,c,d) fwrite(a, b, c, d)
225 #define STAT_BLK_SIZE(x) ((x).st_blksize)
226 
227 
228 /*---------------------------------------------------------------------------*/
229 /* */
230 /* Misc Macros */
231 /* */
232 /*---------------------------------------------------------------------------*/
233 #if defined(_WIN32)
234  #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
235 #else
236  #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
237 #endif
238 
239 #if defined(_WIN32)
240  #define STRTOULL(x) _atoi64(x)
241 #else
242  #define STRTOULL(x) strtoull(x, NULL, 10)
243 #endif
244 
245 #if defined(_WIN32)
246  #define SNPRINTF _snprintf
247  #define PRINTF printf
248  #define VPRINTF vprintf
249  #define FPRINTF fprintf
250 #else
251  #define SNPRINTF snprintf
252  #define PRINTF printf
253  #define VPRINTF vprintf
254  #define FPRINTF fprintf
255 #endif
256 
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif /* __HOST_H__ */