From 8fdd5d987e20c6c3e6ecc277212f4bdacda3bb7d Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sun, 8 Jan 2012 01:33:19 +0000 Subject: [PATCH] Remove the encoding parameter from AssFile::SaveMemory, as it only supports utf-8, and simplify the implementation Originally committed to SVN as r6220. --- aegisub/src/ass_file.cpp | 35 ++++------------------- aegisub/src/ass_file.h | 2 +- aegisub/src/subtitles_provider_csri.cpp | 2 +- aegisub/src/subtitles_provider_libass.cpp | 2 +- 4 files changed, 8 insertions(+), 33 deletions(-) diff --git a/aegisub/src/ass_file.cpp b/aegisub/src/ass_file.cpp index 684bc6679..3ba79f696 100644 --- a/aegisub/src/ass_file.cpp +++ b/aegisub/src/ass_file.cpp @@ -211,45 +211,20 @@ wxString AssFile::AutoSave() { return dstpath.GetFullPath(); } -void AssFile::SaveMemory(std::vector &dst,const wxString encoding) { - // Set encoding - wxString enc = encoding; - if (enc.IsEmpty()) enc = "UTF-8"; - if (enc != "UTF-8") throw "Memory writer only supports UTF-8 for now."; - +void AssFile::SaveMemory(std::vector &dst) { // Check if subs contain at least one style // Add a default style if they don't for compatibility with libass/asa if (GetStyles().Count() == 0) - InsertStyle(new AssStyle()); + InsertStyle(new AssStyle); // Prepare vector dst.clear(); dst.reserve(0x4000); // Write file - entryIter cur; - unsigned int lineSize = 0; - unsigned int targetSize = 0; - unsigned int pos = 0; - wxCharBuffer buffer; - for (cur=Line.begin();cur!=Line.end();cur++) { - // Convert - wxString temp = (*cur)->GetEntryData() + "\r\n"; - buffer = temp.mb_str(wxConvUTF8); - lineSize = strlen(buffer); - - // Raise capacity if needed - targetSize = dst.size() + lineSize; - if (dst.capacity() < targetSize) { - unsigned int newSize = dst.capacity(); - while (newSize < targetSize) newSize *= 2; - dst.reserve(newSize); - } - - // Append line - pos = dst.size(); - dst.resize(targetSize); - memcpy(&dst[pos],buffer,lineSize); + for (entryIter cur = Line.begin(); cur != Line.end(); ++cur) { + wxCharBuffer buffer = ((*cur)->GetEntryData() + "\r\n").utf8_str(); + copy(buffer.data(), buffer.data() + buffer.length(), back_inserter(dst)); } } diff --git a/aegisub/src/ass_file.h b/aegisub/src/ass_file.h index 8de28c43c..dac9c7cbf 100644 --- a/aegisub/src/ass_file.h +++ b/aegisub/src/ass_file.h @@ -139,7 +139,7 @@ public: /// @brief Save to a memory buffer. Used for subtitle providers which support it /// @param[out] dst Destination vector - void SaveMemory(std::vector &dst,const wxString encoding=""); + void SaveMemory(std::vector &dst); /// Add file name to the MRU list void AddToRecent(wxString file); /// Can the file be saved in its current format? diff --git a/aegisub/src/subtitles_provider_csri.cpp b/aegisub/src/subtitles_provider_csri.cpp index 9210dfb57..0a221f0f6 100644 --- a/aegisub/src/subtitles_provider_csri.cpp +++ b/aegisub/src/subtitles_provider_csri.cpp @@ -86,7 +86,7 @@ void CSRISubtitlesProvider::LoadSubtitles(AssFile *subs) { // Open from memory if (canOpenMem) { std::vector data; - subs->SaveMemory(data,wxSTRING_ENCODING); + subs->SaveMemory(data); instance.reset(csri_open_mem(renderer,&data[0],data.size(),NULL), &csri_close); } diff --git a/aegisub/src/subtitles_provider_libass.cpp b/aegisub/src/subtitles_provider_libass.cpp index 442724548..37711ff4b 100644 --- a/aegisub/src/subtitles_provider_libass.cpp +++ b/aegisub/src/subtitles_provider_libass.cpp @@ -161,7 +161,7 @@ LibassSubtitlesProvider::~LibassSubtitlesProvider() { void LibassSubtitlesProvider::LoadSubtitles(AssFile *subs) { // Prepare subtitles std::vector data; - subs->SaveMemory(data,"UTF-8"); + subs->SaveMemory(data); // Load file if (ass_track) ass_free_track(ass_track);