Created
December 2, 2010 14:33
-
-
Save neesenk/725410 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "darray.h" | |
int change_path(D_ARRAY *path, const char *change, size_t ch_len) | |
{ | |
#define IS_DIR_END(path) ((path)->array[(path)->len - 1] == '/') | |
#define PUT_DRI(path, dir, dir_len) do { \ | |
if ((path)->len == 0 || !IS_DIR_END(path)) \ | |
dynamic_array_put(path, "/", 1); \ | |
dynamic_array_put(path, dir, dir_len); \ | |
} while (0); | |
/* 最多可能需要添加额外两个'/' */ | |
if (dynamic_array_check_size(path, ch_len + 3) < 0) | |
return -1; | |
if (ch_len > 0 && *change == '/') | |
D_ARRAY_RESET(path); | |
while (ch_len > 0) { | |
const char *ptr = memchr(change, '/', ch_len); | |
size_t dir_len = ptr ? ptr - change : ch_len; | |
if (dir_len == 0 || (dir_len == 1 && change[0] == '.')) | |
goto LOOP_NEXT; | |
if (dir_len == 2 && change[0] == '.' && change[1] == '.') { | |
if (path->len > 0 && IS_DIR_END(path)) | |
path->len--; | |
while (path->len > 0 && !IS_DIR_END(path)) | |
path->len--; | |
goto LOOP_NEXT; | |
} | |
PUT_DRI(path, change, dir_len); | |
LOOP_NEXT: | |
if (ptr == NULL) | |
break; | |
ch_len -= dir_len + 1, change += dir_len + 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment