File attachment loading/saving supposedly all done

Originally committed to SVN as r442.
This commit is contained in:
Rodrigo Braz Monteiro 2006-07-01 00:54:33 +00:00
parent fe43cb641d
commit 59364bb272
2 changed files with 86 additions and 59 deletions

View File

@ -106,12 +106,18 @@ const wxString AssAttachment::GetEntryData() {
entryData += filename + _T("\r\n"); entryData += filename + _T("\r\n");
// Read three bytes // Read three bytes
while (pos+3 <= size) { while (pos < size) {
// Number to read
int read = size - pos;
if (read > 3) read = 3;
// Read source // Read source
src[0] = dat[pos]; src[0] = dat[pos];
src[1] = dat[pos+1]; if (read >= 2) src[1] = dat[pos+1];
src[2] = dat[pos+2]; else src[1] = 0;
pos += 3; if (read == 3) src[2] = dat[pos+2];
else src[2] = 0;
pos += read;
// Codify // Codify
dst[0] = src[0] >> 2; dst[0] = src[0] >> 2;
@ -119,13 +125,16 @@ const wxString AssAttachment::GetEntryData() {
dst[2] = ((src[1] & 0xF) << 2) | ((src[2] & 0xC0) >> 6); dst[2] = ((src[1] & 0xF) << 2) | ((src[2] & 0xC0) >> 6);
dst[3] = src[2] & 0x3F; dst[3] = src[2] & 0x3F;
// Number to write
int toWrite = read+1;
// Convert to text // Convert to text
for (int i=0;i<4;i++) { for (int i=0;i<toWrite;i++) {
entryData += wxChar(dst[i]+33); entryData += wxChar(dst[i]+33);
written++; written++;
// Line break // Line break
if (written == 80) { if (written == 80 && pos < size) {
written = 0; written = 0;
entryData += _T("\r\n"); entryData += _T("\r\n");
} }
@ -177,22 +186,24 @@ void AttachData::Finish() {
// Read buffer // Read buffer
while (ok) { while (ok) {
// Find characters left // Find characters left
int left = buffer.Length() - bufPos; int read = buffer.Length() - bufPos;
if (read > 4) read = 4;
int nbytes; int nbytes;
// At least four, proceed normally // At least four, proceed normally
if (left >= 4) { if (read >= 2) {
// Move 4 bytes from buffer to src // Move 4 bytes from buffer to src
for (int i=0;i<4;i++) { for (int i=0;i<read;i++) {
src[i] = (unsigned char) buffer[bufPos] - 33; src[i] = (unsigned char) buffer[bufPos] - 33;
bufPos++; bufPos++;
} }
for (int i=read;i<4;i++) src[i] = 0;
ok = true; ok = true;
nbytes = 3; nbytes = read-1;
} }
// Zero, end // Zero, end
else if (left == 0) { else {
ok = false; ok = false;
break; break;
} }

View File

@ -233,10 +233,74 @@ wxString AssFile::GetString() {
return ret; return ret;
} }
/////////////////////// ///////////////////////
// Appends line to Ass // Appends line to Ass
// -------------------
// I strongly advice you against touching this function unless you know what you're doing;
// even moving things out of order might break ASS parsing - AMZ.
//
int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA) { int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA) {
// Group
AssEntry *entry = NULL; AssEntry *entry = NULL;
wxString origGroup = group;
static wxString keepGroup;
if (!keepGroup.IsEmpty()) group = keepGroup;
// Attachment
if (group == _T("[Fonts]") || group == _T("[Graphics]")) {
// Check if it's valid data
size_t dataLen = data.Length();
bool validData = (dataLen > 0) && (dataLen <= 80);
for (size_t i=0;i<dataLen;i++) {
if (data[i] < 33 || data[i] >= 97) validData = false;
}
// Is the filename line?
bool isFilename = (data.Left(10) == _T("fontname: ") && group == _T("[Fonts]")) || (data.Left(10) == _T("filename: ") && group == _T("[Graphics]"));
// The attachment file is static, since it is built through several calls to this
// After it's done building, it's reset to NULL
static AssAttachment *attach = NULL;
// Attachment exists, and data is over
if (attach && (!validData || isFilename)) {
attach->Finish();
keepGroup.Clear();
group = origGroup;
Line.push_back(attach);
attach = NULL;
}
// Create attachment if needed
if (isFilename) {
attach = new AssAttachment(data.Mid(10));
attach->StartMS = lasttime;
attach->group = group;
keepGroup = group;
return lasttime;
}
// Valid data?
if (validData) {
// Insert data
attach->AddData(data);
// Done building
if (data.Length() < 80) {
attach->Finish();
keepGroup.Clear();
group = origGroup;
entry = attach;
attach = NULL;
}
// Not done
else {
return lasttime;
}
}
}
// Dialogue // Dialogue
if (group == _T("[Events]")) { if (group == _T("[Events]")) {
@ -270,54 +334,6 @@ int AssFile::AddLine (wxString data,wxString group,int lasttime,bool &IsSSA) {
} }
} }
// Attachment
else if (group == _T("[Fonts]") || group == _T("[Graphics]")) {
// Check if it's valid data
size_t dataLen = data.Length();
bool validData = (dataLen > 0) && (dataLen <= 80);
for (size_t i=0;i<dataLen;i++) {
if (data[i] < 33 || data[i] >= 97) validData = false;
}
// Is the filename line?
bool isFilename = (data.Left(10) == _T("fontname: ") && group == _T("[Fonts]")) || (data.Left(10) == _T("filename: ") && group == _T("[Graphics]"));
// The attachment file is static, since it is built through several calls to this
// After it's done building, it's reset to NULL
static AssAttachment *attach = NULL;
// Attachment exists, and data is over
if (attach && (!validData || isFilename)) {
attach->Finish();
Line.push_back(attach);
attach = NULL;
}
// Create attachment if needed
if (isFilename) {
attach = new AssAttachment(data.Mid(10));
attach->StartMS = lasttime;
attach->group = group;
return lasttime;
}
// Valid data?
if (validData) {
// Insert data
attach->AddData(data);
// Done building
if (data.Length() < 80) {
attach->Finish();
entry = attach;
attach = NULL;
}
// Not done
else return lasttime;
}
}
// Script info // Script info
else if (group == _T("[Script Info]")) { else if (group == _T("[Script Info]")) {
// Comment // Comment