Skip to content

Commit

Permalink
Fix createSaveFilename encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
panpaul committed Apr 26, 2024
1 parent 992a301 commit 4cb25ce
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/model/Document.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Document.h"

#include <array>
#include <codecvt> // for codecvt_utf8_utf16
#include <ctime> // for size_t, localtime, strf...
#include <iomanip>
#include <sstream>
Expand Down Expand Up @@ -133,8 +134,9 @@ auto Document::createSaveFolder(fs::path lastSavePath) -> fs::path {
return lastSavePath;
}

auto Document::createSaveFilename(DocumentType type, const std::string& defaultSaveName, const std::string& defaultPdfName) -> fs::path {
constexpr static std::string_view forbiddenChars = {"\\/:*?\"<>|"};
auto Document::createSaveFilename(DocumentType type, const std::string& defaultSaveName,
const std::string& defaultPdfName) -> fs::path {
constexpr static std::wstring_view forbiddenChars = {L"\\/:*?\"<>|"};
std::string wildcardString;
if (type != Document::PDF) {
if (!filepath.empty()) {
Expand All @@ -154,13 +156,16 @@ auto Document::createSaveFilename(DocumentType type, const std::string& defaultS
wildcardString = SaveNameUtils::parseFilenameFromWildcardString(defaultPdfName, this->filepath.filename());
}

const char* format = wildcardString.empty() ? defaultSaveName.c_str() : wildcardString.c_str();
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

auto format_str = wildcardString.empty() ? defaultSaveName : wildcardString;
auto format = converter.from_bytes(format_str);

// Todo (cpp20): use <format>
std::ostringstream ss;
std::wostringstream ss;
ss.imbue(std::locale());
time_t curtime = time(nullptr);
ss << std::put_time(localtime(&curtime), format);
ss << std::put_time(localtime(&curtime), format.c_str());
auto filename = ss.str();
// Todo (cpp20): use <ranges>
for (auto& c: filename) {
Expand Down

0 comments on commit 4cb25ce

Please sign in to comment.