Exemplo n.º 1
0
void TXTSubtitleFormat::ReadFile(AssFile *target, wxString const& filename, wxString const& encoding) const {
	using namespace std;
	DialogTextImport dlg;
	if (dlg.ShowModal() == wxID_CANCEL) return;

	TextFileReader file(filename, encoding, false);

	target->LoadDefault(false);

	wxString actor;
	wxString separator = to_wx(OPT_GET("Tool/Import/Text/Actor Separator")->GetString());
	wxString comment = to_wx(OPT_GET("Tool/Import/Text/Comment Starter")->GetString());

	// Parse file
	while (file.HasMoreLines()) {
		wxString value = file.ReadLineFromFile();
		if(value.empty()) continue;

		// Check if this isn't a timecodes file
		if (value.StartsWith("# timecode"))
			throw SubtitleFormatParseError("File is a timecode file, cannot load as subtitles.", 0);

		// Read comment data
		bool isComment = false;
		if (!comment.empty() && value.StartsWith(comment)) {
			isComment = true;
			value = value.Mid(comment.size());
		}

		// Read actor data
		if (!isComment && !separator.empty()) {
			if (value[0] != ' ' && value[0] != '\t') {
				int pos = value.Find(separator);
				if (pos != wxNOT_FOUND) {
					actor = value.Left(pos);
					actor.Trim(false);
					actor.Trim(true);
					value = value.Mid(pos+1);
				}
			}
		}

		// Trim spaces at start
		value.Trim(false);

		if (value.empty())
			isComment = true;

		// Sets line up
		AssDialogue *line = new AssDialogue;
		line->Actor = isComment ? "" : actor;
		line->Comment = isComment;
		line->Text = value;
		line->End = 0;

		// Adds line
		target->Line.push_back(*line);
	}
}
Exemplo n.º 2
0
void TXTSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& encoding) const {
	DialogTextImport dlg;
	if (dlg.ShowModal() == wxID_CANCEL) return;

	TextFileReader file(filename, encoding, false);

	target->LoadDefault(false);

	std::string actor;
	std::string separator = OPT_GET("Tool/Import/Text/Actor Separator")->GetString();
	std::string comment = OPT_GET("Tool/Import/Text/Comment Starter")->GetString();

	// Parse file
	while (file.HasMoreLines()) {
		std::string value = file.ReadLineFromFile();
		if (value.empty() && !OPT_GET("Tool/Import/Text/Include Blank")->GetBool()) continue;

		// Check if this isn't a timecodes file
		if (boost::starts_with(value, "# timecode"))
			throw SubtitleFormatParseError("File is a timecode file, cannot load as subtitles.", 0);

		// Read comment data
		bool isComment = false;
		if (!comment.empty() && boost::starts_with(value, comment)) {
			isComment = true;
			value.erase(0, comment.size());
		}

		// Read actor data
		if (!isComment && !separator.empty() && !value.empty()) {
			if (value[0] != ' ' && value[0] != '\t') {
				size_t pos = value.find(separator);
				if (pos != std::string::npos) {
					actor = value.substr(0, pos);
					boost::trim(actor);
					value.erase(0, pos + 1);
				}
			}
		}

		// Trim spaces at start
		boost::trim_left(value);

		if (value.empty())
			isComment = true;

		// Sets line up
		AssDialogue *line = new AssDialogue;
		line->Actor = isComment ? std::string() : actor;
		line->Comment = isComment;
		line->Text = value;
		line->End = 0;

		target->Line.push_back(*line);
	}
}