From 00bc0c7ef8f09031f6fa8107bfe6a01ebe1213aa Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 22 Dec 2011 21:15:28 +0000 Subject: [PATCH] Significantly clean up and simplify AssAttachment Originally committed to SVN as r6038. --- aegisub/src/ass_attachment.cpp | 208 +++++------------------------ aegisub/src/ass_attachment.h | 58 +++----- aegisub/src/dialog_attachments.cpp | 2 +- 3 files changed, 50 insertions(+), 218 deletions(-) diff --git a/aegisub/src/ass_attachment.cpp b/aegisub/src/ass_attachment.cpp index 36d6d6d68..6bba6dfec 100644 --- a/aegisub/src/ass_attachment.cpp +++ b/aegisub/src/ass_attachment.cpp @@ -34,9 +34,6 @@ /// @ingroup subs_storage /// - -//////////// -// Includes #include "config.h" #ifndef AGI_PRE @@ -46,87 +43,26 @@ #include "ass_attachment.h" - - -/// @brief Constructor -/// @param _name -/// -AssAttachment::AssAttachment(wxString _name) { - // Parse name - filename = _name; - wxFileName fname(GetFileName()); +AssAttachment::AssAttachment(wxString name) +: data(new std::vector) +, filename(name) +{ + wxFileName fname(filename); wxString ext = fname.GetExt().Lower(); - wxString name; - if (ext == "ttf") { - name = fname.GetName() + "_0." + ext; - } - else name = _name; - - // Set data - filename = name; - data = std::tr1::shared_ptr (new AttachData); + if (ext == "ttf") + filename = fname.GetName() + "_0." + ext; } - - -/// @brief Destructor -/// -AssAttachment::~AssAttachment() { -} - - - -/// @brief Clone -/// @return -/// AssEntry *AssAttachment::Clone() const { - // New object AssAttachment *clone = new AssAttachment(filename); - - // Copy fields clone->data = data; clone->group = group; - - // Return return clone; } - - -/// @brief Get data -/// @return -/// -const DataVec &AssAttachment::GetData() { - return data->GetData(); -} - - - -/// @brief Add more data -/// @param _data -/// -void AssAttachment::AddData(wxString _data) { - data->AddData(_data); -} - - - -/// @brief Finish adding data -/// -void AssAttachment::Finish() { - data->Finish(); -} - - - -/// @brief Get encoded data to write on file -/// @return -/// const wxString AssAttachment::GetEntryData() const { - // Get data - const DataVec &dat = data->GetData(); int pos = 0; - int size = dat.size(); + int size = data->size(); int written = 0; unsigned char src[3]; unsigned char dst[4]; @@ -144,10 +80,10 @@ const wxString AssAttachment::GetEntryData() const { if (read > 3) read = 3; // Read source - src[0] = dat[pos]; - if (read >= 2) src[1] = dat[pos+1]; + src[0] = (*data)[pos]; + if (read >= 2) src[1] = (*data)[pos+1]; else src[1] = 0; - if (read == 3) src[2] = dat[pos+2]; + if (read == 3) src[2] = (*data)[pos+2]; else src[2] = 0; pos += read; @@ -173,32 +109,16 @@ const wxString AssAttachment::GetEntryData() const { } } - // Return return entryData; } - - -/// @brief Extract as a file -/// @param filename -/// @return -/// void AssAttachment::Extract(wxString filename) { - // Open file wxFileOutputStream fp(filename); if (!fp.Ok()) return; - fp.Write(&data->GetData()[0],data->GetData().size()); + fp.Write(&(*data)[0], data->size()); } - - -/// @brief Read a file as attachment -/// @param filename -/// void AssAttachment::Import(wxString filename) { - // Data - DataVec &datavec = data->GetData(); - // Open file and get size wxFileInputStream fp(filename); if (!fp.Ok()) throw "Failed opening file"; @@ -206,102 +126,38 @@ void AssAttachment::Import(wxString filename) { fp.SeekI(0,wxFromStart); // Set size and read - datavec.resize(size); - fp.Read(&datavec[0],size); + data->resize(size); + fp.Read(&(*data)[0],size); } - - -/// @brief Get filename -/// @param raw -/// @return -/// wxString AssAttachment::GetFileName(bool raw) { - // Raw if (raw || filename.Right(4).Lower() != ".ttf") return filename; // Remove stuff after last underscore if it's a font - int lastUnder = -1; - for (size_t i=0;ireserve(buffer.size() * 3 / 4); // Read buffer - while (ok) { + for(size_t pos = 0; pos + 1 < buffer.size(); ) { // Find characters left - int read = buffer.Length() - bufPos; - if (read > 4) read = 4; - int nbytes; + size_t read = std::min(buffer.size() - pos, 4); - // At least four, proceed normally - if (read >= 2) { - // Move 4 bytes from buffer to src - for (int i=0;i> 4); @@ -309,14 +165,10 @@ void AttachData::Finish() { dst[2] = ((src[2] & 0x3) << 6) | (src[3]); // Push into vector - size_t size = data.size(); - data.resize(size+nbytes); - for (int i=0;i #include @@ -44,60 +41,43 @@ #include "ass_entry.h" -/// DOCME -typedef std::vector DataVec; - - -/// @class AttachData -/// @brief DOCME -class AttachData { -private: - - /// DOCME - DataVec data; - - /// DOCME - wxString buffer; - -public: - AttachData(); - ~AttachData(); - - DataVec &GetData(); - void AddData(wxString data); - void Finish(); -}; - - - /// @class AssAttachment /// @brief DOCME class AssAttachment : public AssEntry { -private: + /// Decoded file data + std::tr1::shared_ptr > data; - /// DOCME - std::tr1::shared_ptr data; + /// Encoded data which has been read from the script but not yet decoded + wxString buffer; - /// DOCME + /// Name of the attached file, with SSA font mangling if it is a ttf wxString filename; public: - const DataVec &GetData(); + /// Get the size of the attached file in bytes + size_t GetSize() const { return data->size(); } - void AddData(wxString data); + /// Add a line of data (without newline) read from a subtitle file to the + /// buffer waiting to be decoded + void AddData(wxString data) { buffer += data; } + /// Decode all data passed with AddData void Finish(); + /// Extract the contents of this attachment to a file + /// @param filename Path to save the attachment to void Extract(wxString filename); + + /// Import the contents of a file as an attachment + /// @param filename Path to import void Import(wxString filename); + + /// Get the name of the attached file + /// @param raw If false, remove the SSA filename mangling wxString GetFileName(bool raw=false); const wxString GetEntryData() const; - - /// @brief DOCME - /// ASS_EntryType GetType() const { return ENTRY_ATTACHMENT; } AssEntry *Clone() const; AssAttachment(wxString name); - ~AssAttachment(); }; diff --git a/aegisub/src/dialog_attachments.cpp b/aegisub/src/dialog_attachments.cpp index 6986d3823..77fef05f2 100644 --- a/aegisub/src/dialog_attachments.cpp +++ b/aegisub/src/dialog_attachments.cpp @@ -117,7 +117,7 @@ void DialogAttachments::UpdateList() { // Add item int row = listView->GetItemCount(); listView->InsertItem(row,attach->GetFileName(true)); - listView->SetItem(row,1,PrettySize(attach->GetData().size())); + listView->SetItem(row,1,PrettySize(attach->GetSize())); listView->SetItem(row,2,attach->group); listView->SetItemPtrData(row,wxPtrToUInt(attach)); }