Remove the encoding parameter from AssFile::SaveMemory, as it only supports utf-8, and simplify the implementation

Originally committed to SVN as r6220.
This commit is contained in:
Thomas Goyne 2012-01-08 01:33:19 +00:00
parent c379e1e8e0
commit 8fdd5d987e
4 changed files with 8 additions and 33 deletions

View file

@ -211,45 +211,20 @@ wxString AssFile::AutoSave() {
return dstpath.GetFullPath();
}
void AssFile::SaveMemory(std::vector<char> &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<char> &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));
}
}

View file

@ -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<char> &dst,const wxString encoding="");
void SaveMemory(std::vector<char> &dst);
/// Add file name to the MRU list
void AddToRecent(wxString file);
/// Can the file be saved in its current format?

View file

@ -86,7 +86,7 @@ void CSRISubtitlesProvider::LoadSubtitles(AssFile *subs) {
// Open from memory
if (canOpenMem) {
std::vector<char> data;
subs->SaveMemory(data,wxSTRING_ENCODING);
subs->SaveMemory(data);
instance.reset(csri_open_mem(renderer,&data[0],data.size(),NULL), &csri_close);
}

View file

@ -161,7 +161,7 @@ LibassSubtitlesProvider::~LibassSubtitlesProvider() {
void LibassSubtitlesProvider::LoadSubtitles(AssFile *subs) {
// Prepare subtitles
std::vector<char> data;
subs->SaveMemory(data,"UTF-8");
subs->SaveMemory(data);
// Load file
if (ass_track) ass_free_track(ass_track);