kompare
diffhunk.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "diffhunk.h"
00019
00020 #include "difference.h"
00021
00022 using namespace Diff2;
00023
00024 DiffHunk::DiffHunk( int sourceLine, int destinationLine, QString function, Type type ) :
00025 m_sourceLine( sourceLine ),
00026 m_destinationLine( destinationLine ),
00027 m_function( function ),
00028 m_type( type )
00029 {
00030 }
00031
00032 DiffHunk::~DiffHunk()
00033 {
00034 }
00035
00036 void DiffHunk::add( Difference* diff )
00037 {
00038 m_differences.append( diff );
00039 }
00040
00041 int DiffHunk::sourceLineCount() const
00042 {
00043 DifferenceListConstIterator diffIt = m_differences.begin();
00044 DifferenceListConstIterator dEnd = m_differences.end();
00045
00046 int lineCount = 0;
00047
00048 for ( ; diffIt != dEnd; ++diffIt )
00049 lineCount += (*diffIt)->sourceLineCount();
00050
00051 return lineCount;
00052 }
00053
00054 int DiffHunk::destinationLineCount() const
00055 {
00056 DifferenceListConstIterator diffIt = m_differences.begin();
00057 DifferenceListConstIterator dEnd = m_differences.end();
00058
00059 int lineCount = 0;
00060
00061 for ( ; diffIt != dEnd; ++diffIt )
00062 lineCount += (*diffIt)->destinationLineCount();
00063
00064 return lineCount;
00065 }
00066
00067 QString DiffHunk::recreateHunk() const
00068 {
00069 QString hunk;
00070 QString differences;
00071
00072
00073 DifferenceListConstIterator diffIt = m_differences.begin();
00074 DifferenceListConstIterator dEnd = m_differences.end();
00075
00076 int slc = 0;
00077 int dlc = 0;
00078 for ( ; diffIt != dEnd; ++diffIt )
00079 {
00080 switch ( (*diffIt)->type() )
00081 {
00082 case Difference::Unchanged:
00083 case Difference::Change:
00084 slc += (*diffIt)->sourceLineCount();
00085 dlc += (*diffIt)->destinationLineCount();
00086 break;
00087 case Difference::Insert:
00088 dlc += (*diffIt)->destinationLineCount();
00089 break;
00090 case Difference::Delete:
00091 slc += (*diffIt)->sourceLineCount();
00092 break;
00093 }
00094 differences += (*diffIt)->recreateDifference();
00095 }
00096
00097
00098 hunk += QString::fromLatin1( "@@ -%1,%3 +%2,%4 @@" )
00099 .arg( m_sourceLine )
00100 .arg( m_destinationLine )
00101 .arg( slc )
00102 .arg( dlc );
00103
00104 if ( !m_function.isEmpty() )
00105 hunk += ' ' + m_function;
00106
00107 hunk += QString::fromLatin1( "\n" );
00108
00109 hunk += differences;
00110
00111 kDebug( 8101 ) << hunk << endl;
00112 return hunk;
00113 }