First step for using multiple recvs from mptp.
[swifty.git] / src / libswift / compat.h
1 /*
2  *  compat.h
3  *  compatibility wrappers
4  *
5  *  Created by Arno Bakker, Victor Grishchenko
6  *  Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
7  *
8  */
9 #ifndef SWIFT_COMPAT_H
10 #define SWIFT_COMPAT_H
11
12 #ifdef _MSC_VER
13 typedef unsigned char uint8_t;
14 typedef signed char int8_t;
15 typedef unsigned short uint16_t;
16 typedef short int16_t;
17 typedef unsigned int uint32_t;
18 typedef int int32_t;
19 typedef __int64 int64_t;
20 typedef unsigned __int64 uint64_t;
21 #else
22 #include <stdint.h>
23 #endif
24
25 #ifdef _WIN32
26 #include <winsock2.h>
27 #include <sys/stat.h>
28 #include <io.h>
29 #include <xutility> // for std::min/max
30 #else
31 #include <sys/mman.h>
32 #include <arpa/inet.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <unistd.h>
37 #include <dirent.h>
38 #include <sys/stat.h>
39 #endif
40
41 #include <fcntl.h>
42 #include <cstdio>
43 #include <cstdlib>
44 #include <string>
45 #include <errno.h>
46
47 #ifdef _MSC_VER
48 #include "getopt_win.h"
49 #else
50 #include <getopt.h>
51 #endif
52
53 #ifdef _WIN32
54 #define open(a,b,c)    _open(a,b,c)
55 #define strcasecmp         stricmp
56 #define strtok_r           strtok_s
57 #define stat(a,b)      _stat(a,b)
58 #endif
59 #ifndef S_IRUSR
60 #define S_IRUSR _S_IREAD
61 #endif
62 #ifndef S_IWUSR
63 #define S_IWUSR _S_IWRITE
64 #endif
65 #ifndef S_IRGRP
66 #define S_IRGRP _S_IREAD
67 #endif
68 #ifndef S_IROTH
69 #define S_IROTH _S_IREAD
70 #endif
71
72 #ifdef _WIN32
73 typedef char* setsockoptptr_t;
74 typedef int socklen_t;
75 #else
76 typedef void* setsockoptptr_t;
77 #endif
78
79 // libevent2 assumes WIN32 is defined
80 #ifdef _WIN32
81 #define WIN32   _WIN32
82 #endif
83 #include <event2/util.h>
84
85 #ifndef _WIN32
86 #define INVALID_SOCKET -1
87 #endif
88
89 #ifndef LONG_MAX
90 #include <limits>
91 #define LONG_MAX        numeric_limits<int>::max()
92 #endif
93
94 #ifdef _WIN32
95 // log2 is C99 which is not fully supported by MS VS
96 #define log2(x)         (log(x)/log(2.0))
97 #endif
98
99
100 // Arno, 2012-01-05: Handle 64-bit size_t & printf+scanf
101 #if SIZE_MAX > UINT_MAX
102 #define PRISIZET                "%llu"
103 #else
104 #define PRISIZET        "%lu"
105 #endif
106
107 namespace swift {
108
109 /** tint is the time integer type; microsecond-precise. */
110 typedef int64_t tint;
111 #define TINT_HOUR ((swift::tint)1000000*60*60)
112 #define TINT_MIN ((swift::tint)1000000*60)
113 #define TINT_SEC ((swift::tint)1000000)
114 #define TINT_MSEC ((swift::tint)1000)
115 #define TINT_uSEC ((swift::tint)1)
116 #define TINT_NEVER ((swift::tint)0x3fffffffffffffffLL)
117
118 #ifdef _WIN32
119 #define tintabs _abs64
120 #else
121 #define tintabs ::abs
122 #endif
123
124
125 #ifndef _WIN32
126 #define TCHAR   char
127 #endif
128
129
130 int64_t  file_size (int fd);
131
132 int     file_seek (int fd, int64_t offset);
133
134 int     file_resize (int fd, int64_t new_size);
135
136 void*   memory_map (int fd, size_t size=0);
137 void    memory_unmap (int fd, void*, size_t size);
138
139 void    print_error (const char* msg);
140
141 #ifdef _WIN32
142
143 /** UNIX pread approximation. Does change file pointer. Is not thread-safe */
144 size_t  pread(int fildes, void *buf, size_t nbyte, __int64 offset); // off_t not 64-bit dynamically on Win32
145
146 /** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
147 size_t  pwrite(int fildes, const void *buf, size_t nbyte, __int64 offset);
148
149 int     inet_aton(const char *cp, struct in_addr *inp);
150
151 #endif
152
153 std::string gettmpdir(void);
154
155 tint    usec_time ();
156
157 bool    make_socket_nonblocking(evutil_socket_t s);
158
159 bool    close_socket (evutil_socket_t sock);
160
161 struct timeval* tint2tv (tint t);
162
163
164 };
165
166 #endif
167