File indexing completed on 2024-04-28 17:01:40

0001 /*
0002 SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
0003 
0004 SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "diffparser.h"
0008 
0009 #include <QRegularExpression>
0010 
0011 #include <komparediffdebug.h>
0012 
0013 using namespace Diff2;
0014 
0015 DiffParser::DiffParser(const KompareModelList* list, const QStringList& diff) : ParserBase(list, diff)
0016 {
0017     // The regexps needed for context diff parsing, the rest is the same as in parserbase.cpp
0018     m_contextDiffHeader1.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("\\*\\*\\* ([^\\t]+)(\\t([^\\t]+))?\\n")));
0019     m_contextDiffHeader2.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("--- ([^\\t]+)(\\t([^\\t]+))?\\n")));
0020 }
0021 
0022 DiffParser::~DiffParser()
0023 {
0024 }
0025 
0026 enum Kompare::Format DiffParser::determineFormat()
0027 {
0028     qCDebug(LIBKOMPAREDIFF2) << "Determining the format of the diff Diff" << m_diffLines;
0029 
0030     QRegularExpression normalRE(QStringLiteral("[0-9]+[0-9,]*[acd][0-9]+[0-9,]*"));
0031     QRegularExpression unifiedRE(QStringLiteral("^--- "));
0032     QRegularExpression contextRE(QStringLiteral("^\\*\\*\\* "));
0033     QRegularExpression rcsRE(QStringLiteral("^[acd][0-9]+ [0-9]+"));
0034     QRegularExpression edRE(QStringLiteral("^[0-9]+[0-9,]*[acd]"));
0035 
0036     QStringList::ConstIterator it = m_diffLines.begin();
0037 
0038     while (it != m_diffLines.end())
0039     {
0040         qCDebug(LIBKOMPAREDIFF2) << (*it);
0041         if (it->indexOf(normalRE, 0) == 0)
0042         {
0043             qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Normal diff...";
0044             return Kompare::Normal;
0045         }
0046         else if (it->indexOf(unifiedRE, 0) == 0)
0047         {
0048             qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Unified diff...";
0049             return Kompare::Unified;
0050         }
0051         else if (it->indexOf(contextRE, 0) == 0)
0052         {
0053             qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Context diff...";
0054             return Kompare::Context;
0055         }
0056         else if (it->indexOf(rcsRE, 0) == 0)
0057         {
0058             qCDebug(LIBKOMPAREDIFF2) << "Difflines are from an RCS diff...";
0059             return Kompare::RCS;
0060         }
0061         else if (it->indexOf(edRE, 0) == 0)
0062         {
0063             qCDebug(LIBKOMPAREDIFF2) << "Difflines are from an ED diff...";
0064             return Kompare::Ed;
0065         }
0066         ++it;
0067     }
0068     qCDebug(LIBKOMPAREDIFF2) << "Difflines are from an unknown diff...";
0069     return Kompare::UnknownFormat;
0070 }