summaryrefslogtreecommitdiffstats
path: root/kompare/libdiff2/difference.cpp
blob: c23dd1e974d96ddfe3a117bce8a2adb395d84ad5 (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
/***************************************************************************
                                difference.cpp  -  description
                                -------------------
        begin                   : Sun Mar 4 2001
        copyright               : (C) 2001-2003 by Otto Bruggeman
                                  and John Firebaugh
        email                   : otto.bruggeman@home.nl
                                  jfirebaugh@kde.org
****************************************************************************/

/***************************************************************************
**
**   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 "difference.h"
#include "levenshteintable.h"

using namespace Diff2;

Difference::Difference( int sourceLineNo, int destinationLineNo, int type ) :
	m_type( type ),
	m_sourceLineNo( sourceLineNo ),
	m_destinationLineNo( destinationLineNo ),
	m_applied( false )
{
}

Difference::~Difference()
{
}

void Difference::addSourceLine( TQString line )
{
	m_sourceLines.append( new DifferenceString( line ) );
}

void Difference::addDestinationLine( TQString line )
{
	m_destinationLines.append( new DifferenceString( line ) );
}

int Difference::sourceLineCount() const
{
	return m_sourceLines.count();
}

int Difference::destinationLineCount() const
{
	return m_destinationLines.count();
}

void Difference::apply( bool apply )
{
	m_applied = apply;
}

void Difference::determineInlineDifferences()
{
	LevenshteinTable table;
	if ( m_type != Difference::Change )
		return;

	// Do nothing for now when the slc != dlc
	// One could try to find the closest matching destination string for any
	// of the source strings but this is compute intensive
	if ( sourceLineCount() != destinationLineCount() )
		return;

	int slc = sourceLineCount();

	for ( int i = 0; i < slc; ++i )
	{
		DifferenceString* sl = sourceLineAt( i );
		DifferenceString* dl = destinationLineAt( i );

		// FIXME: If the table cant be created dont do the rest
		table.createTable( sl, dl );

		table.createListsOfMarkers();
	}
}

TQString Difference::recreateDifference() const
{
	TQString difference;

	// source
	DifferenceStringListConstIterator stringIt = m_sourceLines.begin();
	DifferenceStringListConstIterator sEnd     = m_sourceLines.end();

	for ( ; stringIt != sEnd; ++stringIt )
	{
		switch ( m_type )
		{
		case Change:
		case Delete:
			difference += "-";
			break;
		default:
		// Insert but this is not possible in source
		// Unchanged will be handled in destination
		// since they are the same
//			kdDebug( 8101 ) << "Go away, nothing to do for you in source..." << endl;
			continue;
		}
		difference += (*stringIt)->string();
	}

	//destination
	stringIt = m_destinationLines.begin();
	sEnd     = m_destinationLines.end();

	for ( ; stringIt != sEnd; ++stringIt )
	{
		switch ( m_type )
		{
		case Change:
		case Insert:
			difference += "+";
			break;
		case Unchanged:
			difference += " ";
			break;
		default: // Delete but this is not possible in destination
//			kdDebug( 8101 ) << "Go away, nothing to do for you in destination..." << endl;
			continue;
		}
		difference += (*stringIt)->string();
	}

	return difference;
}