add .gitignore
[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 #ifdef _WIN32
70 #define setsockoptptr_t (char*)
71 #else
72 #define setsockoptptr_t void*
73 #endif
74
75
76 namespace swift {
77
78 /** tint is the time integer type; microsecond-precise. */
79 typedef int64_t tint;
80 #define TINT_HOUR ((tint)1000000*60*60)
81 #define TINT_MIN ((tint)1000000*60)
82 #define TINT_SEC ((tint)1000000)
83 #define TINT_MSEC ((tint)1000)
84 #define TINT_uSEC ((tint)1)
85 #define TINT_NEVER ((tint)0x3fffffffffffffffLL)
86
87
88 size_t  file_size (int fd);
89
90 int     file_seek (int fd, size_t offset);
91
92 int     file_resize (int fd, size_t new_size);
93
94 void*   memory_map (int fd, size_t size=0);
95 void    memory_unmap (int fd, void*, size_t size);
96
97 void    print_error (const char* msg);
98
99 #ifdef _WIN32
100
101 /** UNIX pread approximation. Does change file pointer. Is not thread-safe */
102 size_t  pread(int fildes, void *buf, size_t nbyte, long offset);
103
104 /** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
105 size_t  pwrite(int fildes, const void *buf, size_t nbyte, long offset);
106
107 int     inet_aton(const char *cp, struct in_addr *inp);
108
109 #endif
110
111 std::string gettmpdir(void);
112
113 tint    usec_time ();
114
115 bool    make_socket_nonblocking(SOCKET s);
116
117 bool    close_socket (SOCKET sock);
118
119
120 };
121
122 #endif
123