-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.c
304 lines (251 loc) · 5 KB
/
utils.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**********************************************************
*Filename: utils.c
*Author: 星云鹏
*Date: 2008-05-15
*
*与协议无关的若干函数
*********************************************************/
#include "coms.h"
#include "utils.h"
#include <errno.h>
//////////////////////////////////////////////
//读写
ssize_t readn(int fd, void *buf, size_t count)
{
char *strtmp;
ssize_t reval, realcount=0;
strtmp = (char *)buf;
while (count>0)
{
reval = read(fd, strtmp, count);
if (reval<0)
if (errno == EINTR)
continue;
else return -1;
else if (reval>0)
{
count -= reval;
strtmp += reval;
realcount += reval;
continue;
}
else break;
}
return realcount;
}
ssize_t readDelimiter(int fd, void *buf, ssize_t count, char ch)
{
char *strtmp, *buftmp=buf;
ssize_t reval, realcount=0;
strtmp = (char *)buf;
while (realcount<count)
{
reval = read(fd, strtmp, 1);
if (reval<0)
if (errno == EINTR)
continue;
else return -1;
else if (reval>0)
{
realcount++;
if (*strtmp++ == ch)
break;
continue;
}
else break;
}
if (strtmp>buftmp)
*(strtmp--) = '\0';
else *buftmp = '\0';
return realcount;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
char *strtmp;
ssize_t reval, realcount=count;
strtmp = (char *)buf;
while(count>0)
{
reval = write(fd, strtmp, count);
if (reval < 0)
if (errno == EINTR)
continue;
else return -1;
count -= reval;
strtmp += reval;
}
return realcount;
}
ssize_t readline(int fd, void *buf, int size)
{
char *strtmp;
ssize_t reval, realcount=0;
strtmp = (char *)buf;
while(size>1)
{
reval = read(fd, strtmp, 1);
if (reval<0)
if (errno == EINTR)
continue;
else return -1;
else if (reval == 0)
break;
else
{
realcount++;
size--;
if (*strtmp++ =='\n')
break;
}
}
*strtmp='\0';
return realcount;
}
//////////////////////////////////////////////
//处理:
void delColon(char* dest, int size) //:: ---> :
{
char buf[FILENAME];
char *pd=dest, *pb=buf;
int flag=0;
while (*pb++ = *pd++) // = not ==
if ((*pd == ':') && (*(pd-1) == ':'))
{
pd++;
flag = 1;
}
if (flag)
strncpy(dest, buf, size);
}
void addColon(char* dest, int size) //: ---> ::
{
char buf[FILENAME], *pb, *pd;
int flag=0;
unsigned int tmp=0;
pb = buf;
pd = dest;
while ((*pd != '\0')&&(tmp<sizeof(buf)))
{
if ((*pb++ = *pd) == ':') //=是故意的
{
*pb++ = ':';
tmp++;
flag = 1;
}
pd++;
tmp++;
}
buf[tmp]='\0';
if (flag)
strncpy(dest, buf, size);
}
///////////////////////////////////////////////
//用户选择
int inputNo(int min, int max, int def, char *prom)
{
char which[30];
int tmp, index;
if (min>max)
return -1;
if ((def<min)||(def>max))
def = min;
while (1)
{
printf(prom);
if (fgets(which, sizeof(which), stdin)==NULL)
return -1;
transfStr(which, 0);
if (which[0]=='\0')
{
index = def;
break;
}
tmp = strlen(which);
if (which[tmp-1]=='\n')
which[--tmp]='\0';
index = atoi(which);
if (index<min || index>max)
printf("Invalid input. Please input again.\n");
else break;
}
return index;
}
/////////////////////////////////////////////
//
int getFileName(char* dest, const char* fullpath, int size)//get filename from fullpath
{
char strtmp[128], *head;
const char *tail;
int tmp;
tmp = strlen(fullpath);
tail = fullpath + tmp - 1;
while (*tail == '/') //去掉结尾所有的/
tail--;
head = strtmp;
while (tail>=fullpath && *tail!='/') //得到filename的倒序
*head++ = *tail--;
head--;
while (size>0 && head>=strtmp)
{
*dest++ = *head--;
size--;
}
if (size>0)
{
*dest = '\0';
return 0; //
}
else return -1; //文件名太长
}
int getParentPath(char *dest, int size)
{
char *last, *ptr;
if (size<2)
return -1;
if ((last = strrchr(dest, '/'))==NULL)
strncpy(dest, "./", size);
else if (*(last+1)!='\0')
*(last+1) = '\0';
else
{
ptr = last - 1;
while (ptr>=dest)
{
if (*ptr=='/' && *(ptr+1)!='/')
{
*(ptr+1) = '\0';
break;
}
ptr--;
}
if (ptr<dest)
if (*dest!='/')
strncpy(dest, "./", size);
else dest[1] = '\0';
}
return 0;
}
//////////////////////////////////////////////////
//字符串处理,去掉开头结尾空白字符,变成全小写(flag==1)
void transfStr(char *dest, int flag)
{
char *ptr;
int len;
ptr = dest;
while (isspace(*ptr))
ptr++;
len = strlen(ptr);
if (ptr > dest)
memmove(dest, ptr, len+1);
ptr = dest+len-1;
while (isspace(*ptr))
ptr--;
*(ptr+1) = '\0';
ptr = dest;
if (flag == 1)
while (*ptr!='\0')
{
*ptr = tolower(*ptr);
ptr++;
}
}