bool XMLReader::ParseEndElement()
	{
		// ETag ::= '</' Name S? '>'
		if (ParseString("</") && ParseName(mCurrentName) && ParseOptionalWhitespace() && ParseString(">")) {
			if (mOpenTags.empty()) return false;
			Tag & currentTag = mOpenTags.back();
			if (currentTag.mName != mCurrentName.mName) return false;
			mNodeType = kEndElement;
			PopTag();
			return true;
		}
		return false;
	}
Esempio n. 2
0
bool CRealTextParser::ParseRealText(wstring p_szFile)
{
    vector<int> vStartTimecodes;
    vector<int> vEndTimecodes;
    bool bPrevEndTimeMissing = false;
    list<Tag> listTags;
    list<Tag> listPreviousOpenTags;

    while (p_szFile.length() > 0) {
        if (p_szFile.at(0) == '<') {
            Tag oTag;
            if (!ExtractTag(p_szFile, oTag)) {
                return false;
            }

            if (oTag.m_bComment) {
                continue;
            }

            if (oTag.m_szName == L"time") {
                int iStartTimecode = GetTimecode(oTag.m_mapAttributes[L"begin"]);
                int iEndTimecode = GetTimecode(oTag.m_mapAttributes[L"end"]);

                //FilterReduntantTags(listTags);
                wstring szLine = RenderTags(listTags);

                if (bPrevEndTimeMissing) {
                    pair<int, int> pairTimecodes(vStartTimecodes.back(), iStartTimecode);

                    // Fix issues where the next time code isn't valid end time code for the previous subtitle
                    if (pairTimecodes.first >= pairTimecodes.second) {
                        pairTimecodes.second = pairTimecodes.first + m_iDefaultSubtitleDurationInMillisecs;
                    }

                    if (szLine.length() > 0) {
                        m_RealText.m_mapLines[pairTimecodes] = szLine;
                    }

                    bPrevEndTimeMissing = false;
                } else if (!vStartTimecodes.empty() && !vEndTimecodes.empty()) {
                    pair<int, int> pairTimecodes(vStartTimecodes.back(), vEndTimecodes.back());

                    if (szLine.length() > 0) {
                        m_RealText.m_mapLines[pairTimecodes] = szLine;
                    }

                }

                vStartTimecodes.push_back(iStartTimecode);
                if (iEndTimecode <= 0) {
                    bPrevEndTimeMissing = true;
                } else {
                    vEndTimecodes.push_back(iEndTimecode);
                }
            } else if (oTag.m_szName == L"b" || oTag.m_szName == L"i" || oTag.m_szName == L"font") {
                if (oTag.m_bOpen) {
                    listPreviousOpenTags.push_back(oTag);
                }

                if (oTag.m_bClose) {
                    PopTag(listPreviousOpenTags, oTag.m_szName);
                }

                listTags.push_back(oTag);
            } else if (oTag.m_szName == L"clear") {
                listTags.clear();

                // set existing tags
                listTags.insert(listTags.end(), listPreviousOpenTags.begin(), listPreviousOpenTags.end());
            } else if (oTag.m_szName == L"window") {
                if (oTag.m_bOpen) {
                    m_RealText.m_WindowTag = oTag;
                }

                // Ignore close
            } else if (oTag.m_szName == L"center") {
                m_RealText.m_bCenter = true;
            } else if (oTag.m_szName == L"required") {
                // Ignore
            } else if (oTag.m_szName == L"") {
                // Ignore
            } else {
                // assume formating tag (handled later)
                listTags.push_back(oTag);
            }
        } else {
            Tag oTextTag;
            if (!ExtractTextTag(p_szFile, oTextTag)) {
                return false;
            }

            listTags.push_back(oTextTag);
        }
    }

    // Handle final line
    //FilterReduntantTags(listTags);
    wstring szLine = RenderTags(listTags);

    if (bPrevEndTimeMissing) {
        pair<int, int> pairTimecodes(vStartTimecodes.back(), vStartTimecodes.back() + m_iDefaultSubtitleDurationInMillisecs);

        if (szLine.length() > 0) {
            m_RealText.m_mapLines[pairTimecodes] = szLine;
        }

        bPrevEndTimeMissing = false;
    } else if (!vStartTimecodes.empty() && !vEndTimecodes.empty()) {
        pair<int, int> pairTimecodes(vStartTimecodes.back(), vEndTimecodes.back());

        if (szLine.length() > 0) {
            m_RealText.m_mapLines[pairTimecodes] = szLine;
        }

    }

    return true;
}