示例#1
0
void AssParser::ParseScriptInfoLine(std::string const& data) {
	if (boost::starts_with(data, ";")) {
		// Skip stupid comments added by other programs
		// Of course, we'll add our own in place later... ;)
		return;
	}

	if (boost::starts_with(data, "ScriptType:")) {
		std::string version_str = data.substr(11);
		boost::trim(version_str);
		boost::to_lower(version_str);
		if (version_str == "v4.00")
			version = 0;
		else if (version_str == "v4.00+")
			version = 1;
		else
			throw SubtitleFormatParseError("Unknown SSA file format version", nullptr);
	}

	// Nothing actually supports the Collisions property and malformed values
	// crash VSFilter, so just remove it entirely
	if (boost::starts_with(data, "Collisions:"))
		return;

	size_t pos = data.find(':');
	if (pos == data.npos) return;

	target->Info.push_back(*new AssInfo(data.substr(0, pos), boost::trim_left_copy(data.substr(pos + 1))));
}
示例#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);
	}
}