-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomn.h
201 lines (182 loc) · 5.72 KB
/
comn.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
+----------------------------------------------------------------------+
| Author: Xingzhi Liu <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef COMN_H
#define COMN_H
// types
#ifndef _STDINT_H
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
#endif
typedef int bool_t;
typedef int sock_t;
// constants
enum {
GK_FALSE = 0,
GK_TRUE = 1,
GK_FAILURE = -1,
GK_SUCCESS = 0,
GK_ERROR = -1,
GK_OK = 0,
GK_INEXIST = 0,
GK_EXIST = 1
};
// macros
#define COUNTOF(array) (sizeof (array) / sizeof ((array)[0]))
#define countof(array) (sizeof (array) / sizeof ((array)[0]))
#define TOLOWER(x) ('A' <= (x) && (x) <= 'Z' ? (x) + 32 : (x))
#define XZERO(x) memset (&(x), '\0', sizeof (x))
#define ISSPACE(x) isspace(x)
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
#define ISGBK(ch) ((unsigned char)ch & 0x80)
#define DIRISEXIST(dir) (access(dir,F_OK) == 0)
#define FILEISEXIST(file) (access(file,F_OK) == 0)
#define strcasebeginwith(a,b) (strncasecmp(b,a,strlen(a)) == 0)
#define strcaseequal(s1,s2) (strcasecmp(s1,s2) == 0)
// ports
#ifdef WIN32
#define PATH_SEPARATOR '\\'
#define PATH_SEPARATOR_STR "\\"
#define strcasecmp stricmp
#define strncasecmp strnicmp
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define bzero(x, y) memset((x), 0, (y))
#define index strchr
#define rindex strrchr
#else
#define PATH_SEPARATOR '/'
#define PATH_SEPARATOR_STR "/"
#endif
// limits
#define MAX_URL_LEN 1024
#define MAX_LINE_LEN 1024
#define MAX_PATH_LEN 255
#define MAX_NAME_LEN 255
#define BUFSIZE_1M (1*1024*1024)
#define BUFSIZE_4M (4*1024*1024)
// buffer
typedef struct {
char *p;
int size;
int free;
int pos;
int reserve1;
int reserve2;
int reserve3;
}buffer_t;
#define INIT_BUFFER(BUFFER,SIZE) {\
BUFFER.p = (char *)xcalloc(1,(SIZE)+1);\
BUFFER.size = (SIZE);\
BUFFER.free = (SIZE);\
BUFFER.pos = 0;\
BUFFER.reserve1 = 0;\
BUFFER.reserve2 = 0;\
BUFFER.reserve3 = 0;\
}
#define CLEAR_BUFFER(BUFFER,SIZE) {\
BUFFER.free = (SIZE);\
BUFFER.pos = 0;\
BUFFER.reserve1 = 0;\
BUFFER.reserve2 = 0;\
BUFFER.reserve3 = 0;\
}
#define RESIZE_BUFFER(BUFFER,OLD_SIZE,RE_SIZE) {\
BUFFER.p = (char *)xrealloc(BUFFER.p,(OLD_SIZE) + (RE_SIZE) +1);\
BUFFER.size = (OLD_SIZE) + (RE_SIZE);\
BUFFER.free += (RE_SIZE);\
}
#define RESUME_BUFFER(BUFFER,SIZE) {\
if(BUFFER.size != (SIZE)) {\
BUFFER.p = (char *)xrealloc(BUFFER.p,(SIZE)+1);\
BUFFER.size = (SIZE);\
}\
BUFFER.free = (SIZE);\
BUFFER.pos = 0;\
BUFFER.reserve1 = 0;\
BUFFER.reserve2 = 0;\
BUFFER.reserve3 = 0;\
}
//注意: 这是一个复合语句,注意在 if while for 等语句中用{}包含起来
//增加appstrlen变量的目的是防止STR_LEN=strlen(),因为这种情况下要调用9次。
#define APPEND_BUFFER(BUFFER,STR,STR_LEN) {\
int appstrlen = (int)(STR_LEN); \
if(BUFFER.free <= appstrlen) {\
BUFFER.p = (char *)xrealloc(BUFFER.p,BUFFER.size + appstrlen +1);\
BUFFER.size = BUFFER.size + appstrlen;\
BUFFER.free += appstrlen;\
}\
memcpy(BUFFER.p + BUFFER.pos,STR,appstrlen);\
*(BUFFER.p+BUFFER.pos+appstrlen) = 0;\
BUFFER.pos += appstrlen;\
BUFFER.free -= appstrlen;\
}
// 拷贝字符串包括\0到buffer,如果空间不足则设置RETURN=NULL,否则RETURN=字符串指针
#define STRCPY_BUFFER(BUFFER,STR,RETURN) do {\
int cpystrlen = strlen(STR); \
if(BUFFER.free <= cpystrlen) {\
RETURN = NULL;\
} else { \
RETURN = BUFFER.p + BUFFER.pos;\
memcpy(BUFFER.p + BUFFER.pos,STR,cpystrlen);\
*(BUFFER.p+BUFFER.pos+cpystrlen) = 0;\
BUFFER.pos += (cpystrlen+1);\
BUFFER.free -= (cpystrlen+1);\
} \
} while(0)
#define STRCAT_BUFFER(BUFFER,STR,RETURN) do {\
int catstrlen = strlen(STR); \
if(BUFFER.pos && *(BUFFER.p+BUFFER.pos-1) == '\0') { \
BUFFER.pos--; \
BUFFER.free++; \
} \
if(BUFFER.free <= catstrlen) {\
RETURN = NULL;\
} else { \
RETURN = BUFFER.p + BUFFER.pos;\
memcpy(BUFFER.p + BUFFER.pos,STR,catstrlen);\
*(BUFFER.p+BUFFER.pos+catstrlen) = 0;\
BUFFER.pos += (catstrlen+1);\
BUFFER.free -= (catstrlen+1);\
} \
} while(0)
// 拷贝字符串包括\0到buffer,如果空间不足则设置RETURN=NULL,否则RETURN=字符串指针
#define STRCPY2_BUFFER(BUFFER,STR,LEN,RETURN) do {\
int cpystrlen = (LEN); \
if(BUFFER.free <= cpystrlen) {\
RETURN = NULL;\
} else { \
RETURN = BUFFER.p + BUFFER.pos;\
memcpy(BUFFER.p + BUFFER.pos,STR,cpystrlen);\
*(BUFFER.p+BUFFER.pos+cpystrlen) = 0;\
BUFFER.pos += (cpystrlen+1);\
BUFFER.free -= (cpystrlen+1);\
} \
} while(0)
#define STRCAT2_BUFFER(BUFFER,STR,LEN,RETURN) do {\
int catstrlen = (LEN); \
if(BUFFER.pos && *(BUFFER.p+BUFFER.pos-1) == '\0') { \
BUFFER.pos--; \
BUFFER.free++; \
} \
if(BUFFER.free <= catstrlen) {\
RETURN = NULL;\
} else { \
RETURN = BUFFER.p + BUFFER.pos;\
memcpy(BUFFER.p + BUFFER.pos,STR,catstrlen);\
*(BUFFER.p+BUFFER.pos+catstrlen) = 0;\
BUFFER.pos += (catstrlen+1);\
BUFFER.free -= (catstrlen+1);\
} \
} while(0)
#define FREE_BUFFER(BUFFER) xfree(BUFFER.p);
#endif