Skip to content

Commit

Permalink
Fix reading and writing srt files with unicode in the path.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatherly committed Oct 19, 2024
1 parent b7d94b7 commit 48bd013
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/modules/plus/subtitles/subtitles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@
#include <sstream>
#include <string>

#ifdef _WIN32
#include <windows.h>

static wchar_t *utf8ToWide(const char *strUtf8)
{
wchar_t *strWide = nullptr;
int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, strUtf8, -1, NULL, 0);
if (n > 0) {
strWide = (wchar_t *) calloc(n, sizeof(wchar_t));
if (strWide) {
MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, strWide, n);
}
}
return strWide;
}
#endif /* ifdef _WIN32 */

static Subtitles::SubtitleVector readFromSrtStream(std::istream &stream)
{
enum {
Expand Down Expand Up @@ -128,13 +145,25 @@ static bool writeToSrtStream(std::ostream &stream, const Subtitles::SubtitleVect

Subtitles::SubtitleVector Subtitles::readFromSrtFile(const std::string &path)
{
#ifdef _WIN32
wchar_t *wpath = utf8ToWide(path.c_str());
std::ifstream fileStream(wpath);
free(wpath);
#else
std::ifstream fileStream(path);
#endif
return readFromSrtStream(fileStream);
}

bool Subtitles::writeToSrtFile(const std::string &path, const SubtitleVector &items)
{
#ifdef _WIN32
wchar_t *wpath = utf8ToWide(path.c_str());
std::ofstream fileStream(wpath, std::ios::out | std::ios::trunc);
free(wpath);
#else
std::ofstream fileStream(path.c_str(), std::ios::out | std::ios::trunc);
#endif
if (!fileStream.is_open()) {
return false;
}
Expand Down

0 comments on commit 48bd013

Please sign in to comment.