httpgw finally builds
[swift-upb.git] / compat.h
1 /*
2  *  compat.h
3  *  compatibility wrappers
4  *
5  *  Created by Arno Bakker, Victor Grishchenko
6  *  Copyright 2009 Delft University of Technology. 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 #else
30 #include <sys/mman.h>
31 #include <arpa/inet.h>
32 #include <sys/select.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #endif
36
37 #ifndef _WIN32
38 typedef int SOCKET;
39 #endif
40
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <cstdio>
44 #include <cstdlib>
45 #include <string>
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 #endif
56 #ifndef S_IRUSR
57 #define S_IRUSR _S_IREAD
58 #endif
59 #ifndef S_IWUSR
60 #define S_IWUSR _S_IWRITE
61 #endif
62 #ifndef S_IRGRP
63 #define S_IRGRP _S_IREAD
64 #endif
65 #ifndef S_IROTH
66 #define S_IROTH _S_IREAD
67 #endif
68
69 namespace swift {
70
71 /** tint is the time integer type; microsecond-precise. */
72 typedef int64_t tint;
73 #define TINT_HOUR ((tint)1000000*60*60)
74 #define TINT_MIN ((tint)1000000*60)
75 #define TINT_SEC ((tint)1000000)
76 #define TINT_MSEC ((tint)1000)
77 #define TINT_uSEC ((tint)1)
78 #define TINT_NEVER ((tint)0x3fffffffffffffffLL)
79
80
81 size_t  file_size (int fd);
82
83 int     file_seek (int fd, size_t offset);
84
85 int     file_resize (int fd, size_t new_size);
86
87 void*   memory_map (int fd, size_t size=0);
88 void    memory_unmap (int fd, void*, size_t size);
89
90 void    print_error (const char* msg);
91
92 #ifdef _WIN32
93
94 /** UNIX pread approximation. Does change file pointer. Is not thread-safe */
95 size_t  pread(int fildes, void *buf, size_t nbyte, long offset);
96
97 /** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
98 size_t  pwrite(int fildes, const void *buf, size_t nbyte, long offset);
99
100 int     inet_aton(const char *cp, struct in_addr *inp);
101
102 #endif
103
104 std::string gettmpdir(void);
105
106 tint    usec_time ();
107
108 bool    make_socket_nonblocking(SOCKET s);
109
110 bool    close_socket (SOCKET sock);
111
112
113 };
114
115 #endif
116