From 7a209feb22f57ca4a9462d37e2a4d3d759f18076 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Fri, 26 Jan 2007 22:55:42 +0000 Subject: [PATCH] Implemented direct saving to memory for asa Originally committed to SVN as r897. --- aegisub/ass_file.cpp | 44 +++++++++++++++++++++++++++-- aegisub/ass_file.h | 1 + aegisub/subtitles_provider_csri.cpp | 11 ++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/aegisub/ass_file.cpp b/aegisub/ass_file.cpp index bee7f41f4..59d80629f 100644 --- a/aegisub/ass_file.cpp +++ b/aegisub/ass_file.cpp @@ -143,8 +143,8 @@ void AssFile::Load (const wxString _filename,const wxString charset,bool addToRe } -///////////////////////////// -// Chooses format to save in +//////////////////////////// +// Save a file to Hard Disk void AssFile::Save(wxString _filename,bool setfilename,bool addToRecent,const wxString encoding) { // Finds last dot int i = 0; @@ -177,6 +177,46 @@ void AssFile::Save(wxString _filename,bool setfilename,bool addToRecent,const wx } +//////////////////////////////// +// Saves a file to a ram vector +void AssFile::SaveMemory(std::vector &dst,const wxString encoding) { + // Set encoding + wxString enc = encoding; + if (enc.IsEmpty()) enc = _T("UTF-8"); + if (enc != _T("UTF-8")) throw _T("Memory writer only supports UTF-8 for now."); + + // 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() + _T("\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); + } +} + + //////////////////////////////////////////// // Exports file with proper transformations void AssFile::Export(wxString _filename) { diff --git a/aegisub/ass_file.h b/aegisub/ass_file.h index 8f6874cd1..d39b61f85 100644 --- a/aegisub/ass_file.h +++ b/aegisub/ass_file.h @@ -93,6 +93,7 @@ public: wxString GetString(); // Returns the whole file as a single string void Load(wxString file,wxString charset=_T(""),bool addToRecent=true); // Load from a file void Save(wxString file,bool setfilename=false,bool addToRecent=true,const wxString encoding=_T("")); // Save to a file. Pass true to second argument if this isn't a copy + void SaveMemory(std::vector &dst,const wxString encoding=_T("")); // Save to a memory string void Export(wxString file); // Saves exported copy, with effects applied void AddToRecent(wxString file); // Adds file name to list of recently opened files bool CanSave(); // Returns true if the file can be saved in its current format diff --git a/aegisub/subtitles_provider_csri.cpp b/aegisub/subtitles_provider_csri.cpp index 2733ced32..f5d25c6c7 100644 --- a/aegisub/subtitles_provider_csri.cpp +++ b/aegisub/subtitles_provider_csri.cpp @@ -97,18 +97,19 @@ CSRISubtitlesProvider::~CSRISubtitlesProvider() { // Load subtitles void CSRISubtitlesProvider::LoadSubtitles(AssFile *subs) { // Close - // HACK: REMOVE THE FOLLOWING LINE - if (instance) return; if (instance) csri_close(instance); instance = NULL; // Prepare subtitles - wxString subsfilename = VideoContext::Get()->GetTempWorkFile(); - subs->Save(subsfilename,false,false,_T("UTF-8")); + //wxString subsfilename = VideoContext::Get()->GetTempWorkFile(); + //subs->Save(subsfilename,false,false,_T("UTF-8")); + std::vector data; + subs->SaveMemory(data,_T("UTF-8")); delete subs; // Open - instance = csri_open_file(csri_renderer_default(),subsfilename.mb_str(wxConvUTF8),NULL); + //instance = csri_open_file(csri_renderer_default(),subsfilename.mb_str(wxConvUTF8),NULL); + instance = csri_open_mem(csri_renderer_default(),&data[0],data.size(),NULL); }