KompareDiff2

cvsdiffparser.cpp
1/*
2 SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "cvsdiffparser.h"
8
9// lib
10#include "modellist.h"
11#include <komparediff2_logging.h>
12// Qt
13#include <QRegularExpression>
14
15using namespace KompareDiff2;
16
17CVSDiffParser::CVSDiffParser(const ModelList *list, const QStringList &diff)
18 : ParserBase(list, diff)
19{
20 // The regexps needed for context cvs diff parsing, the rest is the same as in parserbase.cpp
21 // third capture in header1 is non optional for cvs diff, it is the revision
22 m_contextDiffHeader1.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("\\*\\*\\* ([^\\t]+)\\t([^\\t]+)\\t(.*)\\n")));
23 m_contextDiffHeader2.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("--- ([^\\t]+)\\t([^\\t]+)(|\\t(.*))\\n")));
24
25 m_normalDiffHeader.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("Index: (.*)\\n")));
26}
27
28CVSDiffParser::~CVSDiffParser() = default;
29
30Format CVSDiffParser::determineFormat()
31{
32 // qCDebug(KOMPAREDIFF2_LOG) << "Determining the format of the CVSDiff";
33
34 QRegularExpression normalRE(QStringLiteral("[0-9]+[0-9,]*[acd][0-9]+[0-9,]*"));
35 QRegularExpression unifiedRE(QStringLiteral("^--- [^\\t]+\\t"));
36 QRegularExpression contextRE(QStringLiteral("^\\*\\*\\* [^\\t]+\\t"));
37 QRegularExpression rcsRE(QStringLiteral("^[acd][0-9]+ [0-9]+"));
38 QRegularExpression edRE(QStringLiteral("^[0-9]+[0-9,]*[acd]"));
39
40 for (const QString &diffLine : std::as_const(m_diffLines)) {
41 if (diffLine.indexOf(normalRE, 0) == 0) {
42// qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Normal diff...";
43 return Normal;
44 }
45 if (diffLine.indexOf(unifiedRE, 0) == 0) {
46// qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Unified diff...";
47 return Unified;
48 }
49 if (diffLine.indexOf(contextRE, 0) == 0) {
50// qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Context diff...";
51 return Context;
52 }
53 if (diffLine.indexOf(rcsRE, 0) == 0) {
54// qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a RCS diff...";
55 return RCS;
56 }
57 if (diffLine.indexOf(edRE, 0) == 0) {
58// qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from an ED diff...";
59 return Ed;
60 }
61 }
62 // qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from an unknown diff...";
63 return UnknownFormat;
64}
65
66bool CVSDiffParser::parseNormalDiffHeader()
67{
68 qCDebug(KOMPAREDIFF2_LOG) << "CVSDiffParser::parseNormalDiffHeader()";
69 bool result = false;
70
71 QStringList::ConstIterator diffEnd = m_diffLines.end();
72
73 while (m_diffIterator != diffEnd) {
74 const auto normalDiffHeaderMatch = m_normalDiffHeader.match(*m_diffIterator);
75 if (normalDiffHeaderMatch.hasMatch()) {
76 qCDebug(KOMPAREDIFF2_LOG) << "Matched length Header = " << normalDiffHeaderMatch.capturedLength();
77 qCDebug(KOMPAREDIFF2_LOG) << "Matched string Header = " << normalDiffHeaderMatch.captured(0);
78
79 m_currentModel = new DiffModel();
80 m_currentModel->setSourceFile(normalDiffHeaderMatch.captured(1));
81 m_currentModel->setDestinationFile(normalDiffHeaderMatch.captured(1));
82
83 result = true;
84
85 ++m_diffIterator;
86 break;
87 } else {
88 qCDebug(KOMPAREDIFF2_LOG) << "No match for: " << (*m_diffIterator);
89 }
90 ++m_diffIterator;
91 }
92
93 if (result == false) {
94 // Set this to the first line again and hope it is a single file diff
95 m_diffIterator = m_diffLines.begin();
96 m_currentModel = new DiffModel();
97 m_singleFileDiff = true;
98 }
99
100 return result;
101}
102
103bool CVSDiffParser::parseEdDiffHeader()
104{
105 return false;
106}
107
108bool CVSDiffParser::parseRCSDiffHeader()
109{
110 return false;
111}
112
113bool CVSDiffParser::parseEdHunkHeader()
114{
115 return false;
116}
117
118bool CVSDiffParser::parseRCSHunkHeader()
119{
120 return false;
121}
122
123bool CVSDiffParser::parseEdHunkBody()
124{
125 return false;
126}
127
128bool CVSDiffParser::parseRCSHunkBody()
129{
130 return false;
131}
A model describing the differences between two files.
Definition diffmodel.h:31
KIOCORE_EXPORT QStringList list(const QString &fileClass)
KompareDiff2 namespace.
Format
Patch format enum.
Definition global.h:16
iterator begin()
iterator end()
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
QString anchoredPattern(QStringView expression)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.