diff options
Diffstat (limited to 'source/blender/blenlib/intern/util.c')
-rw-r--r-- | source/blender/blenlib/intern/util.c | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c index 6d8dd3d9b50..46bb1eb0104 100644 --- a/source/blender/blenlib/intern/util.c +++ b/source/blender/blenlib/intern/util.c @@ -41,6 +41,7 @@ #include <string.h> #include <stdlib.h> #include <stdarg.h> +#include <math.h> /* for log10 */ #include "MEM_guardedalloc.h" @@ -1027,7 +1028,8 @@ void BLI_makestringcode(const char *relfile, char *file) int BLI_convertstringcode(char *path, const char *basepath, int framenum) { - int len, wasrelative; + int wasrelative; + int ch_sta, ch_end; char tmp[FILE_MAX]; char base[FILE_MAX]; char vol[3] = {'\0', '\0', '\0'}; @@ -1082,12 +1084,54 @@ int BLI_convertstringcode(char *path, const char *basepath, int framenum) MEM_freeN(filepart); } - - len= strlen(tmp); - if(len && tmp[len-1]=='#') { - sprintf(tmp+len-1, "%04d", framenum); + + + /* Insert current frame: file### -> file001 */ + ch_end = 0; + for (ch_sta = 0; tmp[ch_sta] != '\0'; ch_sta++) { + if (tmp[ch_sta] == '#') { + ch_end = ch_sta+1; + while (tmp[ch_end] == '#') { + ch_end++; + } + break; + } } - + if (ch_end) { /* warning, ch_end is the last # +1 */ + /* Add the frame number? */ + short numlen, hashlen; + char format[16]; /* 6 is realistically the maxframe (300000), so 8 should be enough, but 16 to be safe. */ + + numlen = 1 + (int)log10((double)framenum); /* this is the number of chars in the number */ + hashlen = ch_end - ch_sta; + + sprintf(format, "%d", framenum); + + if (numlen==hashlen) { /* simple case */ + memcpy(tmp+ch_sta, format, numlen); + } else if (numlen < hashlen) { + memcpy(tmp+ch_sta + (hashlen-numlen), format, numlen); /*dont copy the string terminator */ + memset(tmp+ch_sta, '0', hashlen-numlen); + } else { + /* number is longer then number of #'s */ + if (tmp[ch_end] == '\0') { /* hashes are last, no need to move any string*/ + /* bad juju - not testing string length here :/ */ + memcpy(tmp+ch_sta, format, numlen+1); /* add 1 to get the string terminator \0 */ + } else { + /* we need to move the end characters */ + int i = strlen(tmp); /* +1 to copy the string terminator */ + int j = i + (numlen-hashlen); /* from/to */ + while (i >= ch_end) { + tmp[j] = tmp[i]; + i--; + j--; + } + memcpy(tmp + ch_sta, format, numlen); + } + } + } + /* done with file### stuff */ + strcpy(path, tmp); #ifdef WIN32 /* skip first two chars, which in case of |