kompare
difference.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 difference.cpp 00003 -------------- 00004 begin : Sun Mar 4 2001 00005 Copyright 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com> 00006 Copyright 2001-2003 John Firebaugh <jfirebaugh@kde.org> 00007 ****************************************************************************/ 00008 00009 /*************************************************************************** 00010 ** 00011 ** This program is free software; you can redistribute it and/or modify 00012 ** it under the terms of the GNU General Public License as published by 00013 ** the Free Software Foundation; either version 2 of the License, or 00014 ** (at your option) any later version. 00015 ** 00016 ***************************************************************************/ 00017 00018 #include "difference.h" 00019 #include "levenshteintable.h" 00020 00021 using namespace Diff2; 00022 00023 Difference::Difference( int sourceLineNo, int destinationLineNo, int type ) : 00024 m_type( type ), 00025 m_sourceLineNo( sourceLineNo ), 00026 m_destinationLineNo( destinationLineNo ), 00027 m_applied( false ), 00028 m_conflicts( false ), 00029 m_unsaved( false ) 00030 { 00031 } 00032 00033 Difference::~Difference() 00034 { 00035 qDeleteAll( m_sourceLines ); 00036 qDeleteAll( m_destinationLines ); 00037 } 00038 00039 void Difference::addSourceLine( QString line ) 00040 { 00041 m_sourceLines.append( new DifferenceString( line ) ); 00042 } 00043 00044 void Difference::addDestinationLine( QString line ) 00045 { 00046 m_destinationLines.append( new DifferenceString( line ) ); 00047 } 00048 00049 int Difference::sourceLineCount() const 00050 { 00051 return m_sourceLines.count(); 00052 } 00053 00054 int Difference::destinationLineCount() const 00055 { 00056 return m_destinationLines.count(); 00057 } 00058 00059 void Difference::apply( bool apply ) 00060 { 00061 m_applied = apply; 00062 m_unsaved = !m_unsaved; 00063 } 00064 00065 void Difference::determineInlineDifferences() 00066 { 00067 if ( m_type != Difference::Change ) 00068 return; 00069 00070 // Do nothing for now when the slc != dlc 00071 // One could try to find the closest matching destination string for any 00072 // of the source strings but this is compute intensive 00073 int slc = sourceLineCount(); 00074 00075 if ( slc != destinationLineCount() ) 00076 return; 00077 00078 LevenshteinTable table; 00079 00080 for ( int i = 0; i < slc; ++i ) 00081 { 00082 DifferenceString* sl = sourceLineAt( i ); 00083 DifferenceString* dl = destinationLineAt( i ); 00084 00085 // return value 0 means something went wrong creating the table so dont bother finding markers 00086 if ( table.createTable( sl, dl ) != 0 ) 00087 table.createListsOfMarkers(); 00088 } 00089 } 00090 00091 QString Difference::recreateDifference() const 00092 { 00093 QString difference; 00094 00095 // source 00096 DifferenceStringListConstIterator stringIt = m_sourceLines.begin(); 00097 DifferenceStringListConstIterator sEnd = m_sourceLines.end(); 00098 00099 for ( ; stringIt != sEnd; ++stringIt ) 00100 { 00101 switch ( m_type ) 00102 { 00103 case Change: 00104 case Delete: 00105 difference += '-'; 00106 break; 00107 default: 00108 // Insert but this is not possible in source 00109 // Unchanged will be handled in destination 00110 // since they are the same 00111 // kDebug( 8101 ) << "Go away, nothing to do for you in source..." << endl; 00112 continue; 00113 } 00114 difference += (*stringIt)->string(); 00115 } 00116 00117 //destination 00118 stringIt = m_destinationLines.begin(); 00119 sEnd = m_destinationLines.end(); 00120 00121 for ( ; stringIt != sEnd; ++stringIt ) 00122 { 00123 switch ( m_type ) 00124 { 00125 case Change: 00126 case Insert: 00127 difference += '+'; 00128 break; 00129 case Unchanged: 00130 difference += ' '; 00131 break; 00132 default: // Delete but this is not possible in destination 00133 // kDebug( 8101 ) << "Go away, nothing to do for you in destination..." << endl; 00134 continue; 00135 } 00136 difference += (*stringIt)->string(); 00137 } 00138 00139 return difference; 00140 } 00141
KDE 4.5 API Reference