-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathplatform.h
177 lines (160 loc) · 6.76 KB
/
platform.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (C) 2013- [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, visit the http://fsf.org website.
*/
#include <limits.h>
#ifndef __PLATFORM_H__
#define __PLATFORM_H__
/// @file
/// 类型及平台控制文件
namespace xdelta {
#ifdef _WIN32
#define NAMESPACE_STD_BEGIN _STD_BEGIN
#define NAMESPACE_STD_END _STD_END
#define hash_map _STDEXT hash_map
#define hash_set _STDEXT hash_set
#ifdef XDELTALIB_EXPORTS
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#else
#define NAMESPACE_STD_BEGIN namespace std {
#define NAMESPACE_STD_END }
#ifndef O_BINARY
#define O_BINARY 0
#endif
#define DLL_EXPORT
#define FILE_BEGIN SEEK_SET
#define FILE_CURRENT SEEK_CUR
#define FILE_END SEEK_END
#endif
/// \fn std::string DLL_EXPORT fmt_string (const char * fmt, ...);
/// \brief 格式化错误信息。
/// \param[in] fmt 格式字串
/// \return 返回 string 表示的错误信息。
std::string DLL_EXPORT fmt_string (const char * fmt, ...);
/// \fn std::string DLL_EXPORT error_msg ();
/// \brief 返回当前错误的字串表示。
/// \return 返回当前错误的字串表示。
std::string DLL_EXPORT error_msg ();
/// \fn std::string get_tmp_fname (const std::string & fname);
/// \brief 从输入的文件名取得临时文件名。
/// \param[in] fname 输入的文件名。
/// \return 临时文件名。
std::string DLL_EXPORT get_tmp_fname (const std::string & fname);
#ifdef _WIN32
#define SEPERATOR "\\"
#else
#define SEPERATOR "/"
#endif
/*---------------------------------------------------------------------------*/
/* */
/* Socket Macros */
/* */
/*---------------------------------------------------------------------------*/
#ifdef _WIN32
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
#define ACCEPT(a,b,c) accept(a,b,c)
#define CONNECT(a,b,c) connect(a,b,c)
#define CLOSE(a) closesocket(a)
#define READ(a,b,c) read(a,b,c)
#define RECV(a,b,c,d) recv(a, (char *)b, c, d)
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
#define RECV_FLAGS MSG_WAITALL
#define SELECT(a,b,c,d,e) select((int32_t)a,b,c,d,e)
#define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d)
#define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f)
#define SEND_FLAGS 0
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
#define SET_SOCKET_ERROR(x,y) errno=y
#define SOCKET_ERROR_INTERUPT EINTR
#define SOCKET_ERROR_TIMEDOUT EAGAIN
#define WRITE(a,b,c) write(a,b,c)
#define WRITEV(a,b,c) Writev(b, c)
#define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e)
#define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e)
#define GETHOSTBYNAME(a) gethostbyname((const char *)a)
#elif defined (_LINUX) || defined (_UNIX)
#define ACCEPT(a,b,c) accept(a,b,c)
#define CONNECT(a,b,c) connect(a,b,c)
#define CLOSE(a) close(a)
#define READ(a,b,c) read(a,b,c)
#define RECV(a,b,c,d) recv(a, (void *)b, c, d)
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
#define RECV_FLAGS MSG_WAITALL
#define SELECT(a,b,c,d,e) select(a,b,c,d,e)
#define SEND(a,b,c,d) send(a, (const char_t *)b, c, d)
#define SENDTO(a,b,c,d,e,f) sendto(a, (const char_t *)b, c, d, e, f)
#define SEND_FLAGS 0
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
#define SET_SOCKET_ERROR(x,y) errno=y
#define SOCKET_ERROR_INTERUPT EINTR
#define SOCKET_ERROR_TIMEDOUT EAGAIN
#define WRITE(a,b,c) write(a,b,c)
#define WRITEV(a,b,c) writev(a, b, c)
#define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
#define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
#define GETHOSTBYNAME(a) gethostbyname((const char *)a)
#endif
/*---------------------------------------------------------------------------*/
/* */
/* File Macros */
/* */
/*---------------------------------------------------------------------------*/
//#define STRUCT_STAT struct stat
//#define LSTAT(x,y) lstat(x,y)
//#define FILE_HANDLE FILE *
//#define CLEARERR(x) clearerr(x)
//#define FCLOSE(x) fclose(x)
//#define FEOF(x) feof(x)
//#define FERROR(x) ferror(x)
//#define FFLUSH(x) fflush(x)
//#define FILENO(s) fileno(s)
//#define FOPEN(x,y) fopen(x, y)
//#define FREAD(a,b,c,d) fread(a, b, c, d)
//#define FSTAT(s, st) fstat(FILENO(s), st)
//#define FWRITE(a,b,c,d) fwrite(a, b, c, d)
//#define STAT_BLK_SIZE(x) ((x).st_blksize)
/*---------------------------------------------------------------------------*/
/* */
/* Misc Macros */
/* */
/*---------------------------------------------------------------------------*/
#if defined(_WIN32)
#define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
#else
#define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
#endif
#if defined(_WIN32)
#define STRTOULL(x) _atoi64(x)
#else
#define STRTOULL(x) strtoull(x, NULL, 10)
#endif
#if defined(_WIN32)
#define SNPRINTF _snprintf
#define PRINTF printf
#define VPRINTF vprintf
#define FPRINTF fprintf
#else
#define SNPRINTF snprintf
#define PRINTF printf
#define VPRINTF vprintf
#define FPRINTF fprintf
#endif
} // namespace xdelta
#endif //__PLATFORM_H__