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

0001 /*
0002 SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
0003 SPDX-FileCopyrightText: 2010 Kevin Kofler <kevin.kofler@chello.at>
0004 
0005 SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "parser.h"
0009 
0010 #include <komparediffdebug.h>
0011 #include "cvsdiffparser.h"
0012 #include "diffparser.h"
0013 #include "perforceparser.h"
0014 #include "diffmodel.h"
0015 #include "diffmodellist.h"
0016 
0017 using namespace Diff2;
0018 
0019 Parser::Parser(const KompareModelList* list) :
0020     m_list(list)
0021 {
0022 }
0023 
0024 Parser::~Parser()
0025 {
0026 }
0027 
0028 int Parser::cleanUpCrap(QStringList& diffLines)
0029 {
0030     QStringList::Iterator it = diffLines.begin();
0031 
0032     int nol = 0;
0033 
0034     QLatin1String noNewLine("\\ No newline");
0035 
0036     for (; it != diffLines.end(); ++it)
0037     {
0038         if ((*it).startsWith(noNewLine))
0039         {
0040             it = diffLines.erase(it);
0041             // correcting the advance of the iterator because of the remove
0042             --it;
0043             QString temp(*it);
0044             temp.truncate(temp.indexOf(QLatin1Char('\n')));
0045             *it = temp;
0046             ++nol;
0047         }
0048     }
0049 
0050     return nol;
0051 }
0052 
0053 DiffModelList* Parser::parse(QStringList& diffLines, bool* malformed)
0054 {
0055     /* Basically determine the generator then call the parse method */
0056     ParserBase* parser;
0057 
0058     m_generator = determineGenerator(diffLines);
0059 
0060     int nol = cleanUpCrap(diffLines);
0061     qCDebug(LIBKOMPAREDIFF2) << "Cleaned up " << nol << " line(s) of crap from the diff...";
0062 
0063     switch (m_generator)
0064     {
0065     case Kompare::CVSDiff :
0066         qCDebug(LIBKOMPAREDIFF2) << "It is a CVS generated diff...";
0067         parser = new CVSDiffParser(m_list, diffLines);
0068         break;
0069     case Kompare::Diff :
0070         qCDebug(LIBKOMPAREDIFF2) << "It is a diff generated diff...";
0071         parser = new DiffParser(m_list, diffLines);
0072         break;
0073     case Kompare::Perforce :
0074         qCDebug(LIBKOMPAREDIFF2) << "It is a Perforce generated diff...";
0075         parser = new PerforceParser(m_list, diffLines);
0076         break;
0077     default:
0078         // Nothing to delete, just leave...
0079         return nullptr;
0080     }
0081 
0082     m_format = parser->format();
0083     DiffModelList* modelList = parser->parse(malformed);
0084     if (modelList)
0085     {
0086         qCDebug(LIBKOMPAREDIFF2) << "Modelcount: " << modelList->count();
0087         DiffModelListIterator modelIt = modelList->begin();
0088         DiffModelListIterator mEnd    = modelList->end();
0089         for (; modelIt != mEnd; ++modelIt)
0090         {
0091             qCDebug(LIBKOMPAREDIFF2) << "Hunkcount:  " << (*modelIt)->hunkCount();
0092             qCDebug(LIBKOMPAREDIFF2) << "Diffcount:  " << (*modelIt)->differenceCount();
0093         }
0094     }
0095 
0096     delete parser;
0097 
0098     return modelList;
0099 }
0100 
0101 enum Kompare::Generator Parser::determineGenerator(const QStringList& diffLines)
0102 {
0103     // Shit have to duplicate some code with this method and the ParserBase derived classes
0104     QLatin1String cvsDiff("Index: ");
0105     QLatin1String perforceDiff("==== ");
0106 
0107     QStringList::ConstIterator it = diffLines.begin();
0108     QStringList::ConstIterator linesEnd = diffLines.end();
0109 
0110     while (it != linesEnd)
0111     {
0112         if ((*it).startsWith(cvsDiff))
0113         {
0114             qCDebug(LIBKOMPAREDIFF2) << "Diff is a CVSDiff";
0115             return Kompare::CVSDiff;
0116         }
0117         else if ((*it).startsWith(perforceDiff))
0118         {
0119             qCDebug(LIBKOMPAREDIFF2) << "Diff is a Perforce Diff";
0120             return Kompare::Perforce;
0121         }
0122         ++it;
0123     }
0124 
0125     qCDebug(LIBKOMPAREDIFF2) << "We'll assume it is a diff Diff";
0126 
0127     // For now we'll assume it is a diff file diff, later we might
0128     // try to really determine if it is a diff file diff.
0129     return Kompare::Diff;
0130 }