diffhunk.cpp 2.86 KB
Newer Older
1
/***************************************************************************
2 3
                                diffhunk.cpp
                                ------------
4
        begin                   : Sun Mar 4 2001
5 6
        Copyright 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
        Copyright 2001-2003 John Firebaugh <jfirebaugh@kde.org>
7 8 9 10 11 12 13 14 15 16 17 18
****************************************************************************/

/***************************************************************************
**
**   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 "diffhunk.h"
19

20 21 22 23
#include "difference.h"

using namespace Diff2;

24
DiffHunk::DiffHunk( int sourceLine, int destinationLine, QString function, Type type ) :
25 26 27 28 29 30 31 32 33
	m_sourceLine( sourceLine ),
	m_destinationLine( destinationLine ),
	m_function( function ),
	m_type( type )
{
}

DiffHunk::~DiffHunk()
{
34
	qDeleteAll( m_differences );
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
}

void DiffHunk::add( Difference* diff )
{
	m_differences.append( diff );
}

int DiffHunk::sourceLineCount() const
{
	DifferenceListConstIterator diffIt = m_differences.begin();
	DifferenceListConstIterator dEnd   = m_differences.end();

	int lineCount = 0;

	for ( ; diffIt != dEnd; ++diffIt )
		lineCount += (*diffIt)->sourceLineCount();

	return lineCount;
}

int DiffHunk::destinationLineCount() const
{
	DifferenceListConstIterator diffIt = m_differences.begin();
	DifferenceListConstIterator dEnd   = m_differences.end();

	int lineCount = 0;

	for ( ; diffIt != dEnd; ++diffIt )
		lineCount += (*diffIt)->destinationLineCount();

	return lineCount;
}

QString DiffHunk::recreateHunk() const
{
	QString hunk;
	QString differences;

	// recreate body
	DifferenceListConstIterator diffIt = m_differences.begin();
	DifferenceListConstIterator dEnd   = m_differences.end();

	int slc = 0; // source line count
	int dlc = 0; // destination line count
	for ( ; diffIt != dEnd; ++diffIt )
	{
		switch ( (*diffIt)->type() )
		{
		case Difference::Unchanged:
		case Difference::Change:
			slc += (*diffIt)->sourceLineCount();
			dlc += (*diffIt)->destinationLineCount();
			break;
		case Difference::Insert:
			dlc += (*diffIt)->destinationLineCount();
			break;
		case Difference::Delete:
			slc += (*diffIt)->sourceLineCount();
			break;
		}
		differences += (*diffIt)->recreateDifference();
	}

	// recreate header
	hunk += QString::fromLatin1( "@@ -%1,%3 +%2,%4 @@" )
	        .arg( m_sourceLine )
	        .arg( m_destinationLine )
	        .arg( slc )
103
	        .arg( dlc ); 
104 105 106 107 108 109 110 111

	if ( !m_function.isEmpty() )
		hunk += ' ' + m_function;

	hunk += QString::fromLatin1( "\n" );

	hunk += differences;

112
	kDebug( 8101 ) << hunk << endl;
113 114
	return hunk;
}