kitchensync

genericdiffalgo.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <tqstringlist.h>
23 
24 #include <tdelocale.h>
25 
26 #include "genericdiffalgo.h"
27 
28 using namespace KSync;
29 
30 #define MAX( a, b ) ( a > b ? a : b )
31 
32 #ifndef KDE_USE_FINAL
33 // With --enable-final, we get the (identical) compareString from
34 // addresseediffalgo.cpp
35 static bool compareString( const TQString &left, const TQString &right )
36 {
37  if ( left.isEmpty() && right.isEmpty() )
38  return true;
39  else
40  return left == right;
41 }
42 #endif
43 
44 GenericDiffAlgo::GenericDiffAlgo( const TQString &leftData, const TQString &rightData )
45  : mLeftData( leftData ), mRightData( rightData )
46 {
47 }
48 
49 void GenericDiffAlgo::run()
50 {
51  begin();
52 
53  TQStringList leftList = TQStringList::split( '\n', mLeftData, true );
54  TQStringList rightList = TQStringList::split( '\n', mRightData, true );
55 
56  uint lines = MAX( leftList.count(), rightList.count() );
57  for ( uint i = 0; i < lines; ++i ) {
58  if ( i < leftList.count() && i < rightList.count() ) {
59  if ( !compareString( leftList[ i ], rightList[ i ] ) )
60  conflictField( i18n( "Line %1" ).arg( i ), leftList[ i ], rightList[ i ] );
61  } else if ( i < leftList.count() && i >= rightList.count() ) {
62  additionalLeftField( i18n( "Line %1" ).arg( i ), leftList[ i ] );
63  } else if ( i >= leftList.count() && i < rightList.count() ) {
64  additionalRightField( i18n( "Line %1" ).arg( i ), rightList[ i ] );
65  }
66  }
67 
68  end();
69 }
70