summaryrefslogtreecommitdiffstats
path: root/kompare/libdiff2/parser.cpp
blob: 67386508610e886687f456e2e9a6dae0d862afcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**************************************************************************
**                             parser.cpp
**                              -------------------
**      begin                   : Sun Aug  4 15:05:35 2002
**      copyright               : (C) 2002-2004 Otto Bruggeman
**      email                   : otto.bruggeman@home.nl
**
***************************************************************************/
/***************************************************************************
**
**   This program is free software; you can redistribute it and/or modify
**   it under the terms of the GNU General Public License as published by
**   the Free Software Foundation; either version 2 of the License, or
**   ( at your option ) any later version.
**
***************************************************************************/

#include <kdebug.h>

#include "parser.h"
#include "cvsdiffparser.h"
#include "diffparser.h"
#include "perforceparser.h"
#include "diffmodel.h"

using namespace Diff2;

Parser::Parser( const KompareModelList* list ) :
	m_list( list )
{
}

Parser::~Parser()
{
}

int Parser::cleanUpCrap( TQStringList& diffLines )
{
	TQStringList::Iterator it = diffLines.begin();

	int nol = 0;

	TQString noNewLine( "\\ No newline" );

	for ( ; it != diffLines.end(); ++it )
	{
		if ( (*it).startsWith( noNewLine ) )
		{
			it = diffLines.remove( it );
			// correcting the advance of the iterator because of the remove
			--it;
			TQString temp( *it );
			temp.truncate( temp.tqfind( '\n' ) );
			*it = temp;
			++nol;
		}
	}

	return nol;
} 

DiffModelList* Parser::parse( TQStringList& diffLines )
{
	/* Basically determine the generator then call the parse method */
	ParserBase* parser;

	m_generator = determineGenerator( diffLines );

	int nol = cleanUpCrap( diffLines );
	kdDebug(8101) << "Cleaned up " << nol << " line(s) of crap from the diff..." << endl;

	switch( m_generator )
	{
	case Kompare::CVSDiff :
		kdDebug(8101) << "It is a CVS generated diff..." << endl;
		parser = new CVSDiffParser( m_list, diffLines );
		break;
	case Kompare::Diff :
		kdDebug(8101) << "It is a diff generated diff..." << endl;
		parser = new DiffParser( m_list, diffLines );
		break;
	case Kompare::Perforce :
		kdDebug(8101) << "It is a Perforce generated diff..." << endl;
		parser = new PerforceParser( m_list, diffLines );
		break;
	default:
		// Nothing to delete, just leave...
		return 0L;
	}

	m_format = parser->format();
	DiffModelList* modelList = parser->parse();
	if ( modelList )
	{
		kdDebug(8101) << "Modelcount: " << modelList->count() << endl;
		DiffModelListIterator modelIt = modelList->begin();
		DiffModelListIterator mEnd    = modelList->end();
		for ( ; modelIt != mEnd; ++modelIt )
		{
			kdDebug(8101) << "Hunkcount:  " << (*modelIt)->hunkCount() << endl;
			kdDebug(8101) << "Diffcount:  " << (*modelIt)->differenceCount() << endl;
		}
	}

	delete parser;

	return modelList;
}

enum Kompare::Generator Parser::determineGenerator( const TQStringList& diffLines )
{
	// Shit have to duplicate some code with this method and the ParserBase derived classes
	TQString cvsDiff     ( "Index: " );
	TQString perforceDiff( "==== " );

	TQStringList::ConstIterator it = diffLines.begin();
	TQStringList::ConstIterator linesEnd = diffLines.end();

	while (  it != linesEnd )
	{
		if ( ( *it ).startsWith( cvsDiff ) )
		{
			kdDebug(8101) << "Diff is a CVSDiff" << endl;
			return Kompare::CVSDiff;
		}
		else if ( ( *it ).startsWith( perforceDiff ) )
		{
			kdDebug(8101) << "Diff is a Perforce Diff" << endl;
			return Kompare::Perforce;
		}
		++it;
	}

	kdDebug(8101) << "We'll assume it is a diff Diff" << endl;

	// For now we'll assume it is a diff file diff, later we might
	// try to really determine if it is a diff file diff.
	return Kompare::Diff;
}