diff options
author | Andrea Weikert <elubie@gmx.net> | 2005-10-25 00:52:51 +0400 |
---|---|---|
committer | Andrea Weikert <elubie@gmx.net> | 2005-10-25 00:52:51 +0400 |
commit | 76b7d3b402cdab298616a1b19ae23ca7cf6f6fdc (patch) | |
tree | 4a8de1c551cb0d23bf90ef338e53bb2f052f8c2d /source/blender/blenlib | |
parent | ce89799d3610329aed47dbde7ffb34d81c2c613b (diff) |
Fix for correct handling of relative filenames.
Now relative filenames that are not below the .blend file
in the directory hierarchy can be used.
CAUTION:
The relative filenames are not updated if the blend file
is moved to another dir or saved into another dir (save as)
We will rely on the smartness of the users for this.
my first official commit - thanks Ton!
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_blenlib.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/util.c | 79 |
2 files changed, 59 insertions, 22 deletions
diff --git a/source/blender/blenlib/BLI_blenlib.h b/source/blender/blenlib/BLI_blenlib.h index 0759a2f8865..a66285182ac 100644 --- a/source/blender/blenlib/BLI_blenlib.h +++ b/source/blender/blenlib/BLI_blenlib.h @@ -125,7 +125,7 @@ void BLI_splitdirstring(char *di,char *fi); */ int BLI_convertstringcode(char *path, char *basepath, int framenum); -void BLI_makestringcode(char *fromfile, char *str); +void BLI_makestringcode(const char *relfile, char *file); /** * Change every @a from in @a string into @a to. The diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c index 2095b537d79..e95428f5249 100644 --- a/source/blender/blenlib/intern/util.c +++ b/source/blender/blenlib/intern/util.c @@ -431,31 +431,68 @@ int BLI_strcaseeq(char *a, char *b) { return (BLI_strcasecmp(a, b)==0); } -void BLI_makestringcode(char *fromfile, char *str) +void BLI_makestringcode(const char *relfile, char *file) { - char *slash, len, temp[512]; + char * p; + char * q; + char * lslash; + int len=0; - strcpy(temp, fromfile); - - BLI_char_switch(temp, '\\', '/'); - BLI_char_switch(str, '\\', '/'); - - /* Find the last slash */ - slash = strrchr(temp, '/'); - if(slash) { - *(slash+1)= 0; - len= strlen(temp); - if(len) { - if(strncmp(str, temp, len)==0) { - temp[0]= '/'; - temp[1]= '/'; - strcpy(temp+2, str+len); -#ifdef WIN32 - BLI_char_switch(temp+2, '/', '\\'); + char temp[FILE_MAXDIR+FILE_MAXFILE]; + char res[FILE_MAXDIR+FILE_MAXFILE]; + strcpy(temp, relfile); + +#ifdef WIN32 + if (strlen(file) > 2) { + if ( temp[1] == ':' && file[1] == ':' && temp[0] != file[0] ) + return; + } #endif - strcpy(str, temp); - } + + BLI_char_switch(temp, '\\', '/'); + BLI_char_switch(file, '\\', '/'); + + /* the last slash in the file indicates where the path part ends */ + lslash = BLI_last_slash(temp); + + if (lslash) + { + /* find the prefix of the filename that is equal for both filenames. + This is replaced by the two slashes at the beginning */ + p = temp; + q = file; + while (*p == *q) { + ++p; ++q; + } + /* we might have passed the slash when the beginning of a dir matches + so we rewind. Only check on the actual filename + */ + if (*q != '/') { + while ( (q >= file) && (*q != '/') ) { --q; --p; } + } + else if (*p != '/') { + while ( (p >= temp) && (*p != '/') ) { --p; --q; } + } + + strcpy(res, "//"); + + /* p now points to the slash that is at the beginning of the part + where the path is different from the relative path. + We count the number of directories we need to go up in the + hierarchy to arrive at the common 'prefix' of the path + */ + while (p && p < lslash) { + if (*p == '/') + strcat(res, "../"); + ++p; } + + strcat(res, q+1); /* don't copy the slash at the beginning */ + +#ifdef WIN32 + BLI_char_switch(res+2, '/', '\\'); +#endif + strcpy(file, res); } } |