Fix crash pressing +/- in file-selector

Filenames over 128 chars would crash.
Move BLI_newname into file_ops,
this was only used in one place and isn't all that re-usable.
Also remove special behavior for 4 digits.
This commit is contained in:
Campbell Barton 2015-10-16 04:57:52 +11:00
parent cebaedd709
commit 2f0db80155
2 changed files with 32 additions and 26 deletions

View File

@ -190,31 +190,6 @@ int BLI_split_name_num(char *left, int *nr, const char *name, const char delim)
return name_len;
}
/**
* Looks for a string of digits within name (using BLI_stringdec) and adjusts it by add.
*/
void BLI_newname(char *name, int add)
{
char head[UNIQUE_NAME_MAX], tail[UNIQUE_NAME_MAX];
int pic;
unsigned short digits;
pic = BLI_stringdec(name, head, tail, &digits);
/* are we going from 100 -> 99 or from 10 -> 9 */
if (add < 0 && digits < 4 && digits > 0) {
int i, exp;
exp = 1;
for (i = digits; i > 1; i--) exp *= 10;
if (pic >= exp && (pic + add) < exp) digits--;
}
pic += add;
if (digits == 4 && pic < 0) pic = 0;
BLI_stringenc(name, head, tail, digits, pic);
}
/**
* Ensures name is unique (according to criteria specified by caller in unique_check callback),
* incrementing its numeric suffix as necessary. Returns true if name had to be adjusted.

View File

@ -2033,6 +2033,37 @@ void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
}
/**
* Looks for a string of digits within name (using BLI_stringdec) and adjusts it by add.
*/
static void filenum_newname(char *name, size_t name_size, int add)
{
char head[FILE_MAXFILE], tail[FILE_MAXFILE];
char name_temp[FILE_MAXFILE];
int pic;
unsigned short digits;
pic = BLI_stringdec(name, head, tail, &digits);
/* are we going from 100 -> 99 or from 10 -> 9 */
if (add < 0 && digits > 0) {
int i, exp;
exp = 1;
for (i = digits; i > 1; i--) {
exp *= 10;
}
if (pic >= exp && (pic + add) < exp) {
digits--;
}
}
pic += add;
if (pic < 0)
pic = 0;
BLI_stringenc(name_temp, head, tail, digits, pic);
BLI_strncpy(name, name_temp, name_size);
}
static int file_filenum_exec(bContext *C, wmOperator *op)
{
SpaceFile *sfile = CTX_wm_space_file(C);
@ -2040,7 +2071,7 @@ static int file_filenum_exec(bContext *C, wmOperator *op)
int inc = RNA_int_get(op->ptr, "increment");
if (sfile->params && (inc != 0)) {
BLI_newname(sfile->params->file, inc);
filenum_newname(sfile->params->file, sizeof(sfile->params->file), inc);
ED_area_tag_redraw(sa);
file_draw_check(C);
// WM_event_add_notifier(C, NC_WINDOW, NULL);