KompareDiff2

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

KDE's Doxygen guidelines are available online.