Early file attachment code (note that they are NOT saved back to the file, so they are essentially lost)

Originally committed to SVN as r440.
This commit is contained in:
Rodrigo Braz Monteiro 2006-06-30 22:44:42 +00:00
parent 60b87000e5
commit f2645de0c7
4 changed files with 64 additions and 12 deletions

View file

@ -41,7 +41,8 @@
/////////////// ///////////////
// Constructor // Constructor
AssAttachment::AssAttachment() { AssAttachment::AssAttachment(wxString name) {
filename = name;
data = boost::shared_ptr<AttachData> (new AttachData); data = boost::shared_ptr<AttachData> (new AttachData);
} }
@ -56,10 +57,9 @@ AssAttachment::~AssAttachment() {
// Clone // Clone
AssEntry *AssAttachment::Clone() { AssEntry *AssAttachment::Clone() {
// New object // New object
AssAttachment *clone = new AssAttachment; AssAttachment *clone = new AssAttachment(filename);
// Copy fields // Copy fields
clone->filename = filename;
clone->data = data; clone->data = data;
// Return // Return
@ -93,31 +93,73 @@ void AssAttachment::Finish() {
/////////////// ///////////////
// Constructor // Constructor
AttachData::AttachData() { AttachData::AttachData() {
data = NULL;
} }
////////////// //////////////
// Destructor // Destructor
AttachData::~AttachData() { AttachData::~AttachData() {
delete data;
} }
//////////// ////////////
// Get data // Get data
const void *AttachData::GetData() { const void *AttachData::GetData() {
return (void*) data; return (void*) &data[0];
} }
//////////// ////////////
// Add data // Add data
void AttachData::AddData(wxString data) { void AttachData::AddData(wxString data) {
buffer += data;
} }
////////// //////////
// Finish // Finish
void AttachData::Finish() { void AttachData::Finish() {
// Source and dest buffers
unsigned char src[4];
unsigned char dst[3];
int bufPos = 0;
bool ok = true;
// Read buffer
while (ok) {
// Find characters left
int left = buffer.Length() - bufPos;
int nbytes;
// At least four, proceed normally
if (left >= 4) {
// Move 4 bytes from buffer to src
for (int i=0;i<4;i++) {
src[i] = (unsigned char) buffer[bufPos] - 33;
bufPos++;
}
ok = true;
nbytes = 3;
}
// Zero, end
else if (left == 0) {
ok = false;
break;
}
// Convert the 4 bytes from source to 3 in dst
dst[0] = (src[0] << 2) | (src[1] >> 4);
dst[1] = ((src[1] & 0xF) << 4) | (src[2] >> 2);
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<nbytes;i++) data[size+i] = dst[i];
}
// Clear buffer
buffer.Clear();
buffer.Shrink();
} }

View file

@ -41,13 +41,15 @@
// Headers // Headers
#include "ass_entry.h" #include "ass_entry.h"
#include "boost/shared_ptr.hpp" #include "boost/shared_ptr.hpp"
#include <vector>
/////////////////// ///////////////////
// Attachment data // Attachment data
class AttachData { class AttachData {
private: private:
char *data; std::vector<unsigned char> data;
wxString buffer;
public: public:
AttachData(); AttachData();
@ -75,6 +77,6 @@ public:
ASS_EntryType GetType() { return ENTRY_ATTACHMENT; } ASS_EntryType GetType() { return ENTRY_ATTACHMENT; }
AssEntry *Clone(); AssEntry *Clone();
AssAttachment(); AssAttachment(wxString name);
~AssAttachment(); ~AssAttachment();
}; };

View file

@ -271,9 +271,9 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA) {
} }
// Attachment // Attachment
else if (group == _T("[Fonts]")) { else if (group == _T("[Fonts]") || group == _T("[Graphics]")) {
// Check if it's valid data // Check if it's valid data
bool validData = true; bool validData = data.Length() > 0;
for (size_t i=0;i<data.Length();i++) { for (size_t i=0;i<data.Length();i++) {
if (data[i] < 33 || data[i] >= 97) validData = false; if (data[i] < 33 || data[i] >= 97) validData = false;
} }
@ -295,7 +295,11 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA) {
// Valid data // Valid data
if (validData) { if (validData) {
// Create attachment if needed // Create attachment if needed
if (!attach) attach = new AssAttachment; if (!attach) {
attach = new AssAttachment(data.Mid(10));
attach->StartMS = lasttime;
attach->group = group;
}
// Insert data // Insert data
attach->AddData(data); attach->AddData(data);

View file

@ -61,9 +61,10 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
// Parse file // Parse file
wxString curgroup; wxString curgroup;
int lasttime = -1; int lasttime = -1;
wxString wxbuffer;
while (file.HasMoreLines()) { while (file.HasMoreLines()) {
// Reads line // Reads line
wxString wxbuffer = file.ReadLineFromFile(); wxbuffer = file.ReadLineFromFile();
// Convert v4 styles to v4+ styles // Convert v4 styles to v4+ styles
if (wxbuffer.Lower() == _T("[v4 styles]")) { if (wxbuffer.Lower() == _T("[v4 styles]")) {
@ -88,6 +89,9 @@ void ASSSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
throw wxString(_T("Error processing line: ")) + wxbuffer; throw wxString(_T("Error processing line: ")) + wxbuffer;
} }
} }
// Add one last empty line in case it didn't end with one
if (!wxbuffer.IsEmpty()) AddLine(_T(""),curgroup,lasttime,IsSSA);
} }