简化路径的代码(留着看看)

直接放代码~

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
int buffer_path_simplify(char *dest, char *src){
int toklen; char c, pre1;
char *start, *slash, *walk, *out;
unsigned short pre;
if (src == NULL || dest == NULL) return -1;
walk = src; start = dest; out = dest; slash = dest;
while (*walk == ' ') {
walk++;
}
pre1 = *(walk++);
c = *(walk++);
pre = pre1;
if (pre1 != '/') {
pre = ('/' << 8) | pre1; *(out++) = '/';
}
*(out++) = pre1;
if (pre1 == '\0') {
return 0;
}
while (1) {
if (c == '/' || c == '\0') {
toklen = out - slash;
if (toklen == 3 && pre == (('.' << 8) | '.')) {
out = slash;
if (out > start) {
out--;
while (out > start && *out != '/') {
out--;
}
}
if (c == '\0'){
out++;
} else if (toklen == 1 || pre == (('/' << 8) | '.'))
{
out = slash;
if (c == '\0') out++;
}
slash = out;
}
if (c == '\0') break;
pre1 = c;
pre = (pre << 8) | pre1; c = *walk; *out = pre1; out++; walk++;
}
*out = '\0';
return 0;
}
int main (int argc, char *argv[]){
char buf[1024] = {0};
assert (argc == 2);
buffer_path_simplify (buf, argv[1]);
printf ("%s\n", buf);
return 0;
}