From 2727dd96bdbc83cf11e620381ddc09bc59a04f02 Mon Sep 17 00:00:00 2001 From: Richard Stanway Date: Fri, 28 May 2021 22:32:46 +0200 Subject: [PATCH] UI: Handle prefixes when using paths in recording format The replay buffer and screenshot functions add a prefix to the output filename. The code in GetFormatString assumed the format string was the entire filename, and inserted the prefix at the beginning. This caused illegal paths such as "Screenshot /2021/05/22-35.mkv" to be created if the user had specified a path in the format string, resulting in lost files. This commit inserts the prefix before the last / character to ensure it only affects the filename portion of the format string. Fixes #4707 --- UI/obs-app.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 0c4d597a7..a7070f2ba 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -1774,13 +1774,27 @@ string GetFormatString(const char *format, const char *prefix, { string f; - if (prefix && *prefix) { - f += prefix; - if (f.back() != ' ') - f += " "; - } + f = format; - f += format; + if (prefix && *prefix) { + string str_prefix = prefix; + + if (str_prefix.back() != ' ') + str_prefix += " "; + + size_t insert_pos = 0; + size_t tmp; + + tmp = f.find_last_of('/'); + if (tmp != string::npos && tmp > insert_pos) + insert_pos = tmp + 1; + + tmp = f.find_last_of('\\'); + if (tmp != string::npos && tmp > insert_pos) + insert_pos = tmp + 1; + + f.insert(insert_pos, str_prefix); + } if (suffix && *suffix) { if (*suffix != ' ')