KompareDiff2

parser.cpp
1/*
2SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
3SPDX-FileCopyrightText: 2010 Kevin Kofler <kevin.kofler@chello.at>
4
5SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "parser.h"
9
10#include <komparediffdebug.h>
11#include "cvsdiffparser.h"
12#include "diffparser.h"
13#include "perforceparser.h"
14#include "diffmodel.h"
15#include "diffmodellist.h"
16
17using namespace Diff2;
18
19Parser::Parser(const KompareModelList* list) :
20 m_list(list)
21{
22}
23
24Parser::~Parser()
25{
26}
27
28int Parser::cleanUpCrap(QStringList& diffLines)
29{
30 QStringList::Iterator it = diffLines.begin();
31
32 int nol = 0;
33
34 QLatin1String noNewLine("\\ No newline");
35
36 for (; it != diffLines.end(); ++it)
37 {
38 if ((*it).startsWith(noNewLine))
39 {
40 it = diffLines.erase(it);
41 // correcting the advance of the iterator because of the remove
42 --it;
43 QString temp(*it);
44 temp.truncate(temp.indexOf(QLatin1Char('\n')));
45 *it = temp;
46 ++nol;
47 }
48 }
49
50 return nol;
51}
52
53DiffModelList* Parser::parse(QStringList& diffLines, bool* malformed)
54{
55 /* Basically determine the generator then call the parse method */
56 ParserBase* parser;
57
58 m_generator = determineGenerator(diffLines);
59
60 int nol = cleanUpCrap(diffLines);
61 qCDebug(LIBKOMPAREDIFF2) << "Cleaned up " << nol << " line(s) of crap from the diff...";
62
63 switch (m_generator)
64 {
65 case Kompare::CVSDiff :
66 qCDebug(LIBKOMPAREDIFF2) << "It is a CVS generated diff...";
67 parser = new CVSDiffParser(m_list, diffLines);
68 break;
69 case Kompare::Diff :
70 qCDebug(LIBKOMPAREDIFF2) << "It is a diff generated diff...";
71 parser = new DiffParser(m_list, diffLines);
72 break;
73 case Kompare::Perforce :
74 qCDebug(LIBKOMPAREDIFF2) << "It is a Perforce generated diff...";
75 parser = new PerforceParser(m_list, diffLines);
76 break;
77 default:
78 // Nothing to delete, just leave...
79 return nullptr;
80 }
81
82 m_format = parser->format();
83 DiffModelList* modelList = parser->parse(malformed);
84 if (modelList)
85 {
86 qCDebug(LIBKOMPAREDIFF2) << "Modelcount: " << modelList->count();
87 DiffModelListIterator modelIt = modelList->begin();
88 DiffModelListIterator mEnd = modelList->end();
89 for (; modelIt != mEnd; ++modelIt)
90 {
91 qCDebug(LIBKOMPAREDIFF2) << "Hunkcount: " << (*modelIt)->hunkCount();
92 qCDebug(LIBKOMPAREDIFF2) << "Diffcount: " << (*modelIt)->differenceCount();
93 }
94 }
95
96 delete parser;
97
98 return modelList;
99}
100
101enum Kompare::Generator Parser::determineGenerator(const QStringList& diffLines)
102{
103 // Shit have to duplicate some code with this method and the ParserBase derived classes
104 QLatin1String cvsDiff("Index: ");
105 QLatin1String perforceDiff("==== ");
106
107 QStringList::ConstIterator it = diffLines.begin();
108 QStringList::ConstIterator linesEnd = diffLines.end();
109
110 while (it != linesEnd)
111 {
112 if ((*it).startsWith(cvsDiff))
113 {
114 qCDebug(LIBKOMPAREDIFF2) << "Diff is a CVSDiff";
115 return Kompare::CVSDiff;
116 }
117 else if ((*it).startsWith(perforceDiff))
118 {
119 qCDebug(LIBKOMPAREDIFF2) << "Diff is a Perforce Diff";
120 return Kompare::Perforce;
121 }
122 ++it;
123 }
124
125 qCDebug(LIBKOMPAREDIFF2) << "We'll assume it is a diff Diff";
126
127 // For now we'll assume it is a diff file diff, later we might
128 // try to really determine if it is a diff file diff.
129 return Kompare::Diff;
130}
A list of DiffModel.
Diff2 namespace.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
Generator
Patch generator enum.
Definition kompare.h:39
typedef ConstIterator
typedef Iterator
iterator begin()
qsizetype count() const const
iterator end()
iterator erase(const_iterator begin, const_iterator end)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:10:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.