diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-03-10 08:43:15 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-03-10 08:43:15 +0400 |
commit | 14bbde04412c94c71104659b22652a59c39cdacb (patch) | |
tree | 2ade4a52cf1cefd8b2a34a7949be990be2b38c7a /source/blender/blenlib/intern/path_util.c | |
parent | cb994563509400dfcafecf4d5e7fa2e066f9e55b (diff) |
patch [#34103] path_util_cleanup_path.patch
from Lawrence D'Oliveiro
Improve implementation of BLI_cleanup_path, including making it behave as documented.
"/../home/me" would become "home/me" rather then "/home/me"
Also remove redundant BLI_strncpy()
Diffstat (limited to 'source/blender/blenlib/intern/path_util.c')
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 913a664b97f..b7196fc6db5 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -362,7 +362,7 @@ void BLI_cleanup_path(const char *relabase, char *path) if (path[2] == '\0') { return; /* path is "//" - cant clean it */ } - path = path + 2; /* skip the first // */ + path = path + 2; /* leave the initial "//" untouched */ } } @@ -414,25 +414,27 @@ void BLI_cleanup_path(const char *relabase, char *path) return; } - /* support for odd paths: eg /../home/me --> /home/me - * this is a valid path in blender but we cant handle this the usual way below - * simply strip this prefix then evaluate the path as usual. pythons os.path.normpath() does this */ - while ((strncmp(path, "/../", 4) == 0)) { - memmove(path, path + 4, strlen(path + 4) + 1); - } - while ( (start = strstr(path, "/../")) ) { - eind = start + (4 - 1) /* strlen("/../") - 1 */; a = start - path - 1; - while (a > 0) { - if (path[a] == '/') break; - a--; - } - if (a < 0) { - break; + if (a > 0) { + /* <prefix>/<parent>/../<postfix> => <prefix>/<postfix> */ + eind = start + (4 - 1) /* strlen("/../") - 1 */; /* strip "/.." and keep last "/" */ + while (a > 0 && path[a] != '/') { /* find start of <parent> */ + a--; + } + memmove(path + a, eind, strlen(eind) + 1); } else { - memmove(path + a, eind, strlen(eind) + 1); + /* support for odd paths: eg /../home/me --> /home/me + * this is a valid path in blender but we cant handle this the usual way below + * simply strip this prefix then evaluate the path as usual. + * pythons os.path.normpath() does this */ + + /* Note: previous version of following call used an offset of 3 instead of 4, + * which meant that the "/../home/me" example actually became "home/me". + * Using offset of 3 gives behaviour consistent with the abovementioned + * Python routine. */ + memmove(path, path + 3, strlen(path + 3) + 1); } } @@ -1417,7 +1419,6 @@ void BLI_make_exist(char *dir) void BLI_make_existing_file(const char *name) { char di[FILE_MAX]; - BLI_strncpy(di, name, sizeof(di)); BLI_split_dir_part(name, di, sizeof(di)); /* make if if the dir doesn't exist */ |