void TTXTSubtitleFormat::WriteFile(const AssFile *src, wxString const& filename, wxString const& encoding) const { // Convert to TTXT AssFile copy(*src); ConvertToTTXT(copy); // Create XML structure wxXmlDocument doc; wxXmlNode *root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "TextStream"); root->AddAttribute("version", "1.1"); doc.SetRoot(root); // Create header WriteHeader(root); // Create lines AssDialogue *prev = 0; for (LineList::iterator cur = copy.Line.begin(); cur != copy.Line.end(); ++cur) { AssDialogue *current = dynamic_cast<AssDialogue*>(*cur); if (current && !current->Comment) { WriteLine(root, prev, current); prev = current; } else throw TTXTParseError("Unexpected line type in TTXT file", 0); } // Save XML doc.Save(filename); }
void TTXTSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& encoding) const { target->LoadDefault(false); // Load XML document wxXmlDocument doc; if (!doc.Load(filename.wstring())) throw TTXTParseError("Failed loading TTXT XML file.", nullptr); // Check root node name if (doc.GetRoot()->GetName() != "TextStream") throw TTXTParseError("Invalid TTXT file.", nullptr); // Check version wxString verStr = doc.GetRoot()->GetAttribute("version", ""); int version = -1; if (verStr == "1.0") version = 0; else if (verStr == "1.1") version = 1; else throw TTXTParseError("Unknown TTXT version: " + from_wx(verStr), nullptr); // Get children AssDialogue *diag = nullptr; int lines = 0; for (wxXmlNode *child = doc.GetRoot()->GetChildren(); child; child = child->GetNext()) { // Line if (child->GetName() == "TextSample") { if ((diag = ProcessLine(child, diag, version))) { lines++; target->Line.push_back(*diag); } } // Header else if (child->GetName() == "TextStreamHeader") { ProcessHeader(child); } } // No lines? if (lines == 0) target->Line.push_back(*new AssDialogue); }