From 086e3f4a09f25ebc2c3326d654526f0b96bf3acb Mon Sep 17 00:00:00 2001 From: bl Date: Wed, 17 Feb 2016 20:06:11 -0800 Subject: [PATCH] UI: Add GenerateSpecifiedFilename function Maps specifiers and accepts ones that work across multiple OSes. On some systems, depending on locale, the specifier may resolve to an empty string or nothing. GenerateSpecifiedFilename will avoid conversion of the specifier if this happens, to help guard against this. --- obs/obs-app.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ obs/obs-app.hpp | 2 ++ 2 files changed, 80 insertions(+) diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index 663c62a3f..83ce4cf6e 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -935,6 +935,84 @@ string GenerateTimeDateFilename(const char *extension, bool noSpace) return string(file); } +string GenerateSpecifiedFilename(const char *extension, bool noSpace, + const char *format) +{ + time_t now = time(0); + struct tm *cur_time; + cur_time = localtime(&now); + + const size_t spec_count = 23; + const char *spec[][2] = { + {"%yyyy", "%Y"}, + {"%yy", "%y"}, + {"%mm", "%m"}, + {"%dd", "%d"}, + {"%hh", "%H"}, + {"%mm", "%M"}, + {"%ss", "%S"}, + {"%%", "%%"}, + + {"%a", ""}, + {"%A", ""}, + {"%b", ""}, + {"%B", ""}, + {"%d", ""}, + {"%H", ""}, + {"%I", ""}, + {"%m", ""}, + {"%M", ""}, + {"%p", ""}, + {"%S", ""}, + {"%y", ""}, + {"%Y", ""}, + {"%z", ""}, + {"%Z", ""}, + }; + + char convert[128] = {}; + string sf = format; + string c; + size_t pos = 0, len; + + while (pos < sf.length()) { + len = 0; + for (size_t i = 0; i < spec_count && len == 0; i++) { + + if (sf.find(spec[i][0], pos) == pos) { + if (strlen(spec[i][1])) + strftime(convert, sizeof(convert), + spec[i][1], cur_time); + else + strftime(convert, sizeof(convert), + spec[i][0], cur_time); + + len = strlen(spec[i][0]); + + c = convert; + if (c.length() && c.find_first_not_of(' ') != + std::string::npos) + sf.replace(pos, len, convert); + } + } + + if (len) + pos += strlen(convert); + else if (!len && sf.at(pos) == '%') + sf.erase(pos,1); + else + pos++; + } + + if (noSpace) + replace(sf.begin(), sf.end(), ' ', '_'); + + sf += '.'; + sf += extension; + + return (sf.length() < 256) ? sf : sf.substr(0, 255); +} + vector> GetLocaleNames() { string path; diff --git a/obs/obs-app.hpp b/obs/obs-app.hpp index 5fa2f7c73..e68e572fd 100644 --- a/obs/obs-app.hpp +++ b/obs/obs-app.hpp @@ -34,6 +34,8 @@ std::string CurrentTimeString(); std::string CurrentDateTimeString(); std::string GenerateTimeDateFilename(const char *extension, bool noSpace=false); +std::string GenerateSpecifiedFilename(const char *extension, bool noSpace, + const char *format); QObject *CreateShortcutFilter(); struct BaseLexer {