xref: /trunk/main/sw/source/core/doc/doccomp.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <hintids.hxx>
29cdf0e10cSrcweir #include <tools/list.hxx>
30cdf0e10cSrcweir #include <vcl/vclenum.hxx>
31cdf0e10cSrcweir #include <editeng/crsditem.hxx>
32cdf0e10cSrcweir #include <editeng/colritem.hxx>
33cdf0e10cSrcweir #include <editeng/boxitem.hxx>
34cdf0e10cSrcweir #include <editeng/udlnitem.hxx>
35cdf0e10cSrcweir #include <doc.hxx>
36cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
37cdf0e10cSrcweir #include <docary.hxx>
38cdf0e10cSrcweir #include <pam.hxx>
39cdf0e10cSrcweir #include <ndtxt.hxx>
40cdf0e10cSrcweir #include <redline.hxx>
41cdf0e10cSrcweir #include <UndoRedline.hxx>
42cdf0e10cSrcweir #include <section.hxx>
43cdf0e10cSrcweir #include <tox.hxx>
44cdf0e10cSrcweir #include <docsh.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
47cdf0e10cSrcweir #include <com/sun/star/document/XDocumentProperties.hpp>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir using namespace ::com::sun::star;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir class CompareLine
53cdf0e10cSrcweir {
54cdf0e10cSrcweir public:
CompareLine()55cdf0e10cSrcweir     CompareLine() {}
56cdf0e10cSrcweir     virtual ~CompareLine();
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     virtual sal_uLong GetHashValue() const = 0;
59cdf0e10cSrcweir     virtual sal_Bool Compare( const CompareLine& rLine ) const = 0;
60cdf0e10cSrcweir };
61cdf0e10cSrcweir 
62cdf0e10cSrcweir DECLARE_LIST( CompareList, CompareLine* )
63cdf0e10cSrcweir 
64cdf0e10cSrcweir class CompareData
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     sal_uLong* pIndex;
67cdf0e10cSrcweir     sal_Bool* pChangedFlag;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir protected:
70cdf0e10cSrcweir     CompareList aLines;
71cdf0e10cSrcweir     sal_uLong nSttLineNum;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     // Anfang und Ende beschneiden und alle anderen in das
74cdf0e10cSrcweir     // LinesArray setzen
75cdf0e10cSrcweir     virtual void CheckRanges( CompareData& ) = 0;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir public:
78cdf0e10cSrcweir     CompareData();
79cdf0e10cSrcweir     virtual ~CompareData();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     // gibt es unterschiede?
82cdf0e10cSrcweir     sal_Bool HasDiffs( const CompareData& rData ) const;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // startet das Vergleichen und Erzeugen der Unterschiede zweier
85cdf0e10cSrcweir     // Dokumente
86cdf0e10cSrcweir     void CompareLines( CompareData& rData );
87cdf0e10cSrcweir     // lasse die Unterschiede anzeigen - ruft die beiden Methoden
88cdf0e10cSrcweir     // ShowInsert / ShowDelete. Diese bekommen die Start und EndLine-Nummer
89cdf0e10cSrcweir     // uebergeben. Die Abbildung auf den tatsaechline Inhalt muss die
90cdf0e10cSrcweir     // Ableitung uebernehmen!
91cdf0e10cSrcweir     sal_uLong ShowDiffs( const CompareData& rData );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     virtual void ShowInsert( sal_uLong nStt, sal_uLong nEnd );
94cdf0e10cSrcweir     virtual void ShowDelete( const CompareData& rData, sal_uLong nStt,
95cdf0e10cSrcweir                                 sal_uLong nEnd, sal_uLong nInsPos );
96cdf0e10cSrcweir     virtual void CheckForChangesInLine( const CompareData& rData,
97cdf0e10cSrcweir                                     sal_uLong& nStt, sal_uLong& nEnd,
98cdf0e10cSrcweir                                     sal_uLong& nThisStt, sal_uLong& nThisEnd );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     // Eindeutigen Index fuer eine Line setzen. Gleiche Lines haben den
101cdf0e10cSrcweir     // selben Index; auch in den anderen CompareData!
102cdf0e10cSrcweir     void SetIndex( sal_uLong nLine, sal_uLong nIndex );
GetIndex(sal_uLong nLine) const103cdf0e10cSrcweir     sal_uLong GetIndex( sal_uLong nLine ) const
104cdf0e10cSrcweir         { return nLine < aLines.Count() ? pIndex[ nLine ] : 0; }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     // setze/erfrage ob eine Zeile veraendert ist
107cdf0e10cSrcweir     void SetChanged( sal_uLong nLine, sal_Bool bFlag = sal_True );
GetChanged(sal_uLong nLine) const108cdf0e10cSrcweir     sal_Bool GetChanged( sal_uLong nLine ) const
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             return (pChangedFlag && nLine < aLines.Count())
111cdf0e10cSrcweir                 ? pChangedFlag[ nLine ]
112cdf0e10cSrcweir                 : 0;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir 
GetLineCount() const115cdf0e10cSrcweir     sal_uLong GetLineCount() const      { return aLines.Count(); }
GetLineOffset() const116cdf0e10cSrcweir     sal_uLong GetLineOffset() const     { return nSttLineNum; }
GetLine(sal_uLong nLine) const117cdf0e10cSrcweir     const CompareLine* GetLine( sal_uLong nLine ) const
118cdf0e10cSrcweir             { return aLines.GetObject( nLine ); }
InsertLine(CompareLine * pLine)119cdf0e10cSrcweir     void InsertLine( CompareLine* pLine )
120cdf0e10cSrcweir         { aLines.Insert( pLine, LIST_APPEND ); }
121cdf0e10cSrcweir };
122cdf0e10cSrcweir 
123cdf0e10cSrcweir class Hash
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     struct _HashData
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         sal_uLong nNext, nHash;
128cdf0e10cSrcweir         const CompareLine* pLine;
129cdf0e10cSrcweir 
_HashDataHash::_HashData130cdf0e10cSrcweir         _HashData()
131cdf0e10cSrcweir             : nNext( 0 ), nHash( 0 ), pLine(0) {}
132cdf0e10cSrcweir     };
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     sal_uLong* pHashArr;
135cdf0e10cSrcweir     _HashData* pDataArr;
136cdf0e10cSrcweir     sal_uLong nCount, nPrime;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir public:
139cdf0e10cSrcweir     Hash( sal_uLong nSize );
140cdf0e10cSrcweir     ~Hash();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     void CalcHashValue( CompareData& rData );
143cdf0e10cSrcweir 
GetCount() const144cdf0e10cSrcweir     sal_uLong GetCount() const { return nCount; }
145cdf0e10cSrcweir };
146cdf0e10cSrcweir 
147cdf0e10cSrcweir class Compare
148cdf0e10cSrcweir {
149cdf0e10cSrcweir public:
150cdf0e10cSrcweir     class MovedData
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         sal_uLong* pIndex;
153cdf0e10cSrcweir         sal_uLong* pLineNum;
154cdf0e10cSrcweir         sal_uLong nCount;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     public:
157cdf0e10cSrcweir         MovedData( CompareData& rData, sal_Char* pDiscard );
158cdf0e10cSrcweir         ~MovedData();
159cdf0e10cSrcweir 
GetIndex(sal_uLong n) const160cdf0e10cSrcweir         sal_uLong GetIndex( sal_uLong n ) const { return pIndex[ n ]; }
GetLineNum(sal_uLong n) const161cdf0e10cSrcweir         sal_uLong GetLineNum( sal_uLong n ) const { return pLineNum[ n ]; }
GetCount() const162cdf0e10cSrcweir         sal_uLong GetCount() const { return nCount; }
163cdf0e10cSrcweir     };
164cdf0e10cSrcweir 
165cdf0e10cSrcweir private:
166cdf0e10cSrcweir     // Suche die verschobenen Lines
167cdf0e10cSrcweir     class CompareSequence
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         CompareData &rData1, &rData2;
170cdf0e10cSrcweir         const MovedData &rMoved1, &rMoved2;
171cdf0e10cSrcweir         long *pMemory, *pFDiag, *pBDiag;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         void Compare( sal_uLong nStt1, sal_uLong nEnd1, sal_uLong nStt2, sal_uLong nEnd2 );
174cdf0e10cSrcweir         sal_uLong CheckDiag( sal_uLong nStt1, sal_uLong nEnd1,
175cdf0e10cSrcweir                         sal_uLong nStt2, sal_uLong nEnd2, sal_uLong* pCost );
176cdf0e10cSrcweir     public:
177cdf0e10cSrcweir         CompareSequence( CompareData& rData1, CompareData& rData2,
178cdf0e10cSrcweir                         const MovedData& rD1, const MovedData& rD2 );
179cdf0e10cSrcweir         ~CompareSequence();
180cdf0e10cSrcweir     };
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     static void CountDifference( const CompareData& rData, sal_uLong* pCounts );
184cdf0e10cSrcweir     static void SetDiscard( const CompareData& rData,
185cdf0e10cSrcweir                             sal_Char* pDiscard, sal_uLong* pCounts );
186cdf0e10cSrcweir     static void CheckDiscard( sal_uLong nLen, sal_Char* pDiscard );
187cdf0e10cSrcweir     static sal_uLong SetChangedFlag( CompareData& rData, sal_Char* pDiscard, int bFirst );
188cdf0e10cSrcweir     static void ShiftBoundaries( CompareData& rData1, CompareData& rData2 );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir public:
191cdf0e10cSrcweir     Compare( sal_uLong nDiff, CompareData& rData1, CompareData& rData2 );
192cdf0e10cSrcweir };
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // ====================================================================
195cdf0e10cSrcweir 
~CompareLine()196cdf0e10cSrcweir CompareLine::~CompareLine() {}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir // ----------------------------------------------------------------------
199cdf0e10cSrcweir 
CompareData()200cdf0e10cSrcweir CompareData::CompareData()
201cdf0e10cSrcweir     : pIndex( 0 ), pChangedFlag( 0 ), nSttLineNum( 0 )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
~CompareData()205cdf0e10cSrcweir CompareData::~CompareData()
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     delete[] pIndex;
208cdf0e10cSrcweir     delete[] pChangedFlag;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
SetIndex(sal_uLong nLine,sal_uLong nIndex)211cdf0e10cSrcweir void CompareData::SetIndex( sal_uLong nLine, sal_uLong nIndex )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     if( !pIndex )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         pIndex = new sal_uLong[ aLines.Count() ];
216cdf0e10cSrcweir         memset( pIndex, 0, aLines.Count() * sizeof( sal_uLong ) );
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     if( nLine < aLines.Count() )
219cdf0e10cSrcweir         pIndex[ nLine ] = nIndex;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
SetChanged(sal_uLong nLine,sal_Bool bFlag)222cdf0e10cSrcweir void CompareData::SetChanged( sal_uLong nLine, sal_Bool bFlag )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     if( !pChangedFlag )
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         pChangedFlag = new sal_Bool[ aLines.Count() +1 ];
227cdf0e10cSrcweir         memset( pChangedFlag, 0, aLines.Count() +1 * sizeof( sal_Bool ) );
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir     if( nLine < aLines.Count() )
230cdf0e10cSrcweir         pChangedFlag[ nLine ] = bFlag;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
CompareLines(CompareData & rData)233cdf0e10cSrcweir void CompareData::CompareLines( CompareData& rData )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     CheckRanges( rData );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     sal_uLong nDifferent;
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         Hash aH( GetLineCount() + rData.GetLineCount() + 1 );
240cdf0e10cSrcweir         aH.CalcHashValue( *this );
241cdf0e10cSrcweir         aH.CalcHashValue( rData );
242cdf0e10cSrcweir         nDifferent = aH.GetCount();
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         Compare aComp( nDifferent, *this, rData );
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
ShowDiffs(const CompareData & rData)249cdf0e10cSrcweir sal_uLong CompareData::ShowDiffs( const CompareData& rData )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir     sal_uLong nLen1 = rData.GetLineCount(), nLen2 = GetLineCount();
252cdf0e10cSrcweir     sal_uLong nStt1 = 0, nStt2 = 0;
253cdf0e10cSrcweir     sal_uLong nCnt = 0;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     while( nStt1 < nLen1 || nStt2 < nLen2 )
256cdf0e10cSrcweir     {
257cdf0e10cSrcweir         if( rData.GetChanged( nStt1 ) || GetChanged( nStt2 ) )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             sal_uLong nSav1 = nStt1, nSav2 = nStt2;
260cdf0e10cSrcweir             while( nStt1 < nLen1 && rData.GetChanged( nStt1 )) ++nStt1;
261cdf0e10cSrcweir             while( nStt2 < nLen2 && GetChanged( nStt2 )) ++nStt2;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             // rData ist das Original,
264cdf0e10cSrcweir             // this ist das, in das die Veraenderungen sollen
265cdf0e10cSrcweir             if( nSav2 != nStt2 && nSav1 != nStt1 )
266cdf0e10cSrcweir                 CheckForChangesInLine( rData, nSav1, nStt1, nSav2, nStt2 );
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             if( nSav2 != nStt2 )
269cdf0e10cSrcweir                 ShowInsert( nSav2, nStt2 );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir             if( nSav1 != nStt1 )
272cdf0e10cSrcweir                 ShowDelete( rData, nSav1, nStt1, nStt2 );
273cdf0e10cSrcweir             ++nCnt;
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir         ++nStt1, ++nStt2;
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir     return nCnt;
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
HasDiffs(const CompareData & rData) const280cdf0e10cSrcweir sal_Bool CompareData::HasDiffs( const CompareData& rData ) const
281cdf0e10cSrcweir {
282cdf0e10cSrcweir     sal_Bool bRet = sal_False;
283cdf0e10cSrcweir     sal_uLong nLen1 = rData.GetLineCount(), nLen2 = GetLineCount();
284cdf0e10cSrcweir     sal_uLong nStt1 = 0, nStt2 = 0;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     while( nStt1 < nLen1 || nStt2 < nLen2 )
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir         if( rData.GetChanged( nStt1 ) || GetChanged( nStt2 ) )
289cdf0e10cSrcweir         {
290cdf0e10cSrcweir             bRet = sal_True;
291cdf0e10cSrcweir             break;
292cdf0e10cSrcweir         }
293cdf0e10cSrcweir         ++nStt1, ++nStt2;
294cdf0e10cSrcweir     }
295cdf0e10cSrcweir     return bRet;
296cdf0e10cSrcweir }
297cdf0e10cSrcweir 
ShowInsert(sal_uLong,sal_uLong)298cdf0e10cSrcweir void CompareData::ShowInsert( sal_uLong, sal_uLong )
299cdf0e10cSrcweir {
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
ShowDelete(const CompareData &,sal_uLong,sal_uLong,sal_uLong)302cdf0e10cSrcweir void CompareData::ShowDelete( const CompareData&, sal_uLong, sal_uLong, sal_uLong )
303cdf0e10cSrcweir {
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
CheckForChangesInLine(const CompareData &,sal_uLong &,sal_uLong &,sal_uLong &,sal_uLong &)306cdf0e10cSrcweir void CompareData::CheckForChangesInLine( const CompareData& ,
307cdf0e10cSrcweir                                     sal_uLong&, sal_uLong&, sal_uLong&, sal_uLong& )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir // ----------------------------------------------------------------------
312cdf0e10cSrcweir 
Hash(sal_uLong nSize)313cdf0e10cSrcweir Hash::Hash( sal_uLong nSize )
314cdf0e10cSrcweir     : nCount( 1 )
315cdf0e10cSrcweir {
316cdf0e10cSrcweir 
317cdf0e10cSrcweir static const sal_uLong primes[] =
318cdf0e10cSrcweir {
319cdf0e10cSrcweir   509,
320cdf0e10cSrcweir   1021,
321cdf0e10cSrcweir   2039,
322cdf0e10cSrcweir   4093,
323cdf0e10cSrcweir   8191,
324cdf0e10cSrcweir   16381,
325cdf0e10cSrcweir   32749,
326cdf0e10cSrcweir   65521,
327cdf0e10cSrcweir   131071,
328cdf0e10cSrcweir   262139,
329cdf0e10cSrcweir   524287,
330cdf0e10cSrcweir   1048573,
331cdf0e10cSrcweir   2097143,
332cdf0e10cSrcweir   4194301,
333cdf0e10cSrcweir   8388593,
334cdf0e10cSrcweir   16777213,
335cdf0e10cSrcweir   33554393,
336cdf0e10cSrcweir   67108859,         /* Preposterously large . . . */
337cdf0e10cSrcweir   134217689,
338cdf0e10cSrcweir   268435399,
339cdf0e10cSrcweir   536870909,
340cdf0e10cSrcweir   1073741789,
341cdf0e10cSrcweir   2147483647,
342cdf0e10cSrcweir   0
343cdf0e10cSrcweir };
344cdf0e10cSrcweir     int i;
345cdf0e10cSrcweir 
346cdf0e10cSrcweir     pDataArr = new _HashData[ nSize ];
347cdf0e10cSrcweir     pDataArr[0].nNext = 0;
348cdf0e10cSrcweir     pDataArr[0].nHash = 0,
349cdf0e10cSrcweir     pDataArr[0].pLine = 0;
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     for( i = 0; primes[i] < nSize / 3;  i++)
352cdf0e10cSrcweir         if( !primes[i] )
353cdf0e10cSrcweir         {
354cdf0e10cSrcweir             pHashArr = 0;
355cdf0e10cSrcweir             return;
356cdf0e10cSrcweir         }
357cdf0e10cSrcweir     nPrime = primes[ i ];
358cdf0e10cSrcweir     pHashArr = new sal_uLong[ nPrime ];
359cdf0e10cSrcweir     memset( pHashArr, 0, nPrime * sizeof( sal_uLong ) );
360cdf0e10cSrcweir }
361cdf0e10cSrcweir 
~Hash()362cdf0e10cSrcweir Hash::~Hash()
363cdf0e10cSrcweir {
364cdf0e10cSrcweir     delete[] pHashArr;
365cdf0e10cSrcweir     delete[] pDataArr;
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
CalcHashValue(CompareData & rData)368cdf0e10cSrcweir void Hash::CalcHashValue( CompareData& rData )
369cdf0e10cSrcweir {
370cdf0e10cSrcweir     if( pHashArr )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         for( sal_uLong n = 0; n < rData.GetLineCount(); ++n )
373cdf0e10cSrcweir         {
374cdf0e10cSrcweir             const CompareLine* pLine = rData.GetLine( n );
375cdf0e10cSrcweir             ASSERT( pLine, "wo ist die Line?" );
376cdf0e10cSrcweir             sal_uLong nH = pLine->GetHashValue();
377cdf0e10cSrcweir 
378cdf0e10cSrcweir             sal_uLong* pFound = &pHashArr[ nH % nPrime ];
379cdf0e10cSrcweir             sal_uLong i;
380cdf0e10cSrcweir             for( i = *pFound;  ;  i = pDataArr[i].nNext )
381cdf0e10cSrcweir                 if( !i )
382cdf0e10cSrcweir                 {
383cdf0e10cSrcweir                     i = nCount++;
384cdf0e10cSrcweir                     pDataArr[i].nNext = *pFound;
385cdf0e10cSrcweir                     pDataArr[i].nHash = nH;
386cdf0e10cSrcweir                     pDataArr[i].pLine = pLine;
387cdf0e10cSrcweir                     *pFound = i;
388cdf0e10cSrcweir                     break;
389cdf0e10cSrcweir                 }
390cdf0e10cSrcweir                 else if( pDataArr[i].nHash == nH &&
391cdf0e10cSrcweir                         pDataArr[i].pLine->Compare( *pLine ))
392cdf0e10cSrcweir                     break;
393cdf0e10cSrcweir 
394cdf0e10cSrcweir             rData.SetIndex( n, i );
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir     }
397cdf0e10cSrcweir }
398cdf0e10cSrcweir 
399cdf0e10cSrcweir // ----------------------------------------------------------------------
400cdf0e10cSrcweir 
Compare(sal_uLong nDiff,CompareData & rData1,CompareData & rData2)401cdf0e10cSrcweir Compare::Compare( sal_uLong nDiff, CompareData& rData1, CompareData& rData2 )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir     MovedData *pMD1, *pMD2;
404cdf0e10cSrcweir     // Suche die unterschiedlichen Lines
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         sal_Char* pDiscard1 = new sal_Char[ rData1.GetLineCount() ];
407cdf0e10cSrcweir         sal_Char* pDiscard2 = new sal_Char[ rData2.GetLineCount() ];
408cdf0e10cSrcweir 
409cdf0e10cSrcweir         sal_uLong* pCount1 = new sal_uLong[ nDiff ];
410cdf0e10cSrcweir         sal_uLong* pCount2 = new sal_uLong[ nDiff ];
411cdf0e10cSrcweir         memset( pCount1, 0, nDiff * sizeof( sal_uLong ));
412cdf0e10cSrcweir         memset( pCount2, 0, nDiff * sizeof( sal_uLong ));
413cdf0e10cSrcweir 
414cdf0e10cSrcweir         // stelle fest, welche Indizies in den CompareData mehrfach vergeben wurden
415cdf0e10cSrcweir         CountDifference( rData1, pCount1 );
416cdf0e10cSrcweir         CountDifference( rData2, pCount2 );
417cdf0e10cSrcweir 
418cdf0e10cSrcweir         // alle die jetzt nur einmal vorhanden sind, sind eingefuegt oder
419cdf0e10cSrcweir         // geloescht worden. Alle die im anderen auch vorhanden sind, sind
420cdf0e10cSrcweir         // verschoben worden
421cdf0e10cSrcweir         SetDiscard( rData1, pDiscard1, pCount2 );
422cdf0e10cSrcweir         SetDiscard( rData2, pDiscard2, pCount1 );
423cdf0e10cSrcweir 
424cdf0e10cSrcweir         // die Arrays koennen wir wieder vergessen
425cdf0e10cSrcweir         delete [] pCount1; delete [] pCount2;
426cdf0e10cSrcweir 
427cdf0e10cSrcweir         CheckDiscard( rData1.GetLineCount(), pDiscard1 );
428cdf0e10cSrcweir         CheckDiscard( rData2.GetLineCount(), pDiscard2 );
429cdf0e10cSrcweir 
430cdf0e10cSrcweir         pMD1 = new MovedData( rData1, pDiscard1 );
431cdf0e10cSrcweir         pMD2 = new MovedData( rData2, pDiscard2 );
432cdf0e10cSrcweir 
433cdf0e10cSrcweir         // die Arrays koennen wir wieder vergessen
434cdf0e10cSrcweir         delete [] pDiscard1; delete [] pDiscard2;
435cdf0e10cSrcweir     }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir     {
438cdf0e10cSrcweir         CompareSequence aTmp( rData1, rData2, *pMD1, *pMD2 );
439cdf0e10cSrcweir     }
440cdf0e10cSrcweir 
441cdf0e10cSrcweir     ShiftBoundaries( rData1, rData2 );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     delete pMD1;
444cdf0e10cSrcweir     delete pMD2;
445cdf0e10cSrcweir }
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 
CountDifference(const CompareData & rData,sal_uLong * pCounts)449cdf0e10cSrcweir void Compare::CountDifference( const CompareData& rData, sal_uLong* pCounts )
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     sal_uLong nLen = rData.GetLineCount();
452cdf0e10cSrcweir     for( sal_uLong n = 0; n < nLen; ++n )
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         sal_uLong nIdx = rData.GetIndex( n );
455cdf0e10cSrcweir         ++pCounts[ nIdx ];
456cdf0e10cSrcweir     }
457cdf0e10cSrcweir }
458cdf0e10cSrcweir 
SetDiscard(const CompareData & rData,sal_Char * pDiscard,sal_uLong * pCounts)459cdf0e10cSrcweir void Compare::SetDiscard( const CompareData& rData,
460cdf0e10cSrcweir                             sal_Char* pDiscard, sal_uLong* pCounts )
461cdf0e10cSrcweir {
462cdf0e10cSrcweir     sal_uLong nLen = rData.GetLineCount();
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     // berechne Max in Abhanegigkeit zur LineAnzahl
465cdf0e10cSrcweir     sal_uInt16 nMax = 5;
466cdf0e10cSrcweir     sal_uLong n;
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     for( n = nLen / 64; ( n = n >> 2 ) > 0; )
469cdf0e10cSrcweir         nMax <<= 1;
470cdf0e10cSrcweir 
471cdf0e10cSrcweir     for( n = 0; n < nLen; ++n )
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir         sal_uLong nIdx = rData.GetIndex( n );
474cdf0e10cSrcweir         if( nIdx )
475cdf0e10cSrcweir         {
476cdf0e10cSrcweir             nIdx = pCounts[ nIdx ];
477cdf0e10cSrcweir             pDiscard[ n ] = !nIdx ? 1 : nIdx > nMax ? 2 : 0;
478cdf0e10cSrcweir         }
479cdf0e10cSrcweir         else
480cdf0e10cSrcweir             pDiscard[ n ] = 0;
481cdf0e10cSrcweir     }
482cdf0e10cSrcweir }
483cdf0e10cSrcweir 
CheckDiscard(sal_uLong nLen,sal_Char * pDiscard)484cdf0e10cSrcweir void Compare::CheckDiscard( sal_uLong nLen, sal_Char* pDiscard )
485cdf0e10cSrcweir {
486cdf0e10cSrcweir     for( sal_uLong n = 0; n < nLen; ++n )
487cdf0e10cSrcweir     {
488cdf0e10cSrcweir         if( 2 == pDiscard[ n ] )
489cdf0e10cSrcweir             pDiscard[n] = 0;
490cdf0e10cSrcweir         else if( pDiscard[ n ] )
491cdf0e10cSrcweir         {
492cdf0e10cSrcweir             sal_uLong j;
493cdf0e10cSrcweir             sal_uLong length;
494cdf0e10cSrcweir             sal_uLong provisional = 0;
495cdf0e10cSrcweir 
496cdf0e10cSrcweir             /* Find end of this run of discardable lines.
497cdf0e10cSrcweir                 Count how many are provisionally discardable.  */
498cdf0e10cSrcweir             for (j = n; j < nLen; j++)
499cdf0e10cSrcweir             {
500cdf0e10cSrcweir                 if( !pDiscard[j] )
501cdf0e10cSrcweir                     break;
502cdf0e10cSrcweir                 if( 2 == pDiscard[j] )
503cdf0e10cSrcweir                     ++provisional;
504cdf0e10cSrcweir             }
505cdf0e10cSrcweir 
506cdf0e10cSrcweir             /* Cancel provisional discards at end, and shrink the run.  */
507cdf0e10cSrcweir             while( j > n && 2 == pDiscard[j - 1] )
508cdf0e10cSrcweir                 pDiscard[ --j ] = 0, --provisional;
509cdf0e10cSrcweir 
510cdf0e10cSrcweir             /* Now we have the length of a run of discardable lines
511cdf0e10cSrcweir                whose first and last are not provisional.  */
512cdf0e10cSrcweir             length = j - n;
513cdf0e10cSrcweir 
514cdf0e10cSrcweir             /* If 1/4 of the lines in the run are provisional,
515cdf0e10cSrcweir                cancel discarding of all provisional lines in the run.  */
516cdf0e10cSrcweir             if (provisional * 4 > length)
517cdf0e10cSrcweir             {
518cdf0e10cSrcweir                 while (j > n)
519cdf0e10cSrcweir                     if (pDiscard[--j] == 2)
520cdf0e10cSrcweir                         pDiscard[j] = 0;
521cdf0e10cSrcweir             }
522cdf0e10cSrcweir             else
523cdf0e10cSrcweir             {
524cdf0e10cSrcweir                 sal_uLong consec;
525cdf0e10cSrcweir                 sal_uLong minimum = 1;
526cdf0e10cSrcweir                 sal_uLong tem = length / 4;
527cdf0e10cSrcweir 
528cdf0e10cSrcweir                 /* MINIMUM is approximate square root of LENGTH/4.
529cdf0e10cSrcweir                    A subrun of two or more provisionals can stand
530cdf0e10cSrcweir                    when LENGTH is at least 16.
531cdf0e10cSrcweir                    A subrun of 4 or more can stand when LENGTH >= 64.  */
532cdf0e10cSrcweir                 while ((tem = tem >> 2) > 0)
533cdf0e10cSrcweir                     minimum *= 2;
534cdf0e10cSrcweir                 minimum++;
535cdf0e10cSrcweir 
536cdf0e10cSrcweir                 /* Cancel any subrun of MINIMUM or more provisionals
537cdf0e10cSrcweir                    within the larger run.  */
538cdf0e10cSrcweir                 for (j = 0, consec = 0; j < length; j++)
539cdf0e10cSrcweir                     if (pDiscard[n + j] != 2)
540cdf0e10cSrcweir                         consec = 0;
541cdf0e10cSrcweir                     else if (minimum == ++consec)
542cdf0e10cSrcweir                         /* Back up to start of subrun, to cancel it all.  */
543cdf0e10cSrcweir                         j -= consec;
544cdf0e10cSrcweir                     else if (minimum < consec)
545cdf0e10cSrcweir                         pDiscard[n + j] = 0;
546cdf0e10cSrcweir 
547cdf0e10cSrcweir                 /* Scan from beginning of run
548cdf0e10cSrcweir                    until we find 3 or more nonprovisionals in a row
549cdf0e10cSrcweir                    or until the first nonprovisional at least 8 lines in.
550cdf0e10cSrcweir                    Until that point, cancel any provisionals.  */
551cdf0e10cSrcweir                 for (j = 0, consec = 0; j < length; j++)
552cdf0e10cSrcweir                 {
553cdf0e10cSrcweir                     if (j >= 8 && pDiscard[n + j] == 1)
554cdf0e10cSrcweir                         break;
555cdf0e10cSrcweir                     if (pDiscard[n + j] == 2)
556cdf0e10cSrcweir                         consec = 0, pDiscard[n + j] = 0;
557cdf0e10cSrcweir                     else if (pDiscard[n + j] == 0)
558cdf0e10cSrcweir                         consec = 0;
559cdf0e10cSrcweir                     else
560cdf0e10cSrcweir                         consec++;
561cdf0e10cSrcweir                     if (consec == 3)
562cdf0e10cSrcweir                         break;
563cdf0e10cSrcweir                 }
564cdf0e10cSrcweir 
565cdf0e10cSrcweir                 /* I advances to the last line of the run.  */
566cdf0e10cSrcweir                 n += length - 1;
567cdf0e10cSrcweir 
568cdf0e10cSrcweir                 /* Same thing, from end.  */
569cdf0e10cSrcweir                 for (j = 0, consec = 0; j < length; j++)
570cdf0e10cSrcweir                 {
571cdf0e10cSrcweir                     if (j >= 8 && pDiscard[n - j] == 1)
572cdf0e10cSrcweir                         break;
573cdf0e10cSrcweir                     if (pDiscard[n - j] == 2)
574cdf0e10cSrcweir                         consec = 0, pDiscard[n - j] = 0;
575cdf0e10cSrcweir                     else if (pDiscard[n - j] == 0)
576cdf0e10cSrcweir                         consec = 0;
577cdf0e10cSrcweir                     else
578cdf0e10cSrcweir                         consec++;
579cdf0e10cSrcweir                     if (consec == 3)
580cdf0e10cSrcweir                         break;
581cdf0e10cSrcweir                 }
582cdf0e10cSrcweir             }
583cdf0e10cSrcweir         }
584cdf0e10cSrcweir     }
585cdf0e10cSrcweir }
586cdf0e10cSrcweir 
587cdf0e10cSrcweir // ----------------------------------------------------------------------
588cdf0e10cSrcweir 
MovedData(CompareData & rData,sal_Char * pDiscard)589cdf0e10cSrcweir Compare::MovedData::MovedData( CompareData& rData, sal_Char* pDiscard )
590cdf0e10cSrcweir     : pIndex( 0 ), pLineNum( 0 ), nCount( 0 )
591cdf0e10cSrcweir {
592cdf0e10cSrcweir     sal_uLong nLen = rData.GetLineCount();
593cdf0e10cSrcweir     sal_uLong n;
594cdf0e10cSrcweir 
595cdf0e10cSrcweir     for( n = 0; n < nLen; ++n )
596cdf0e10cSrcweir         if( pDiscard[ n ] )
597cdf0e10cSrcweir             rData.SetChanged( n );
598cdf0e10cSrcweir         else
599cdf0e10cSrcweir             ++nCount;
600cdf0e10cSrcweir 
601cdf0e10cSrcweir     if( nCount )
602cdf0e10cSrcweir     {
603cdf0e10cSrcweir         pIndex = new sal_uLong[ nCount ];
604cdf0e10cSrcweir         pLineNum = new sal_uLong[ nCount ];
605cdf0e10cSrcweir 
606cdf0e10cSrcweir         for( n = 0, nCount = 0; n < nLen; ++n )
607cdf0e10cSrcweir             if( !pDiscard[ n ] )
608cdf0e10cSrcweir             {
609cdf0e10cSrcweir                 pIndex[ nCount ] = rData.GetIndex( n );
610cdf0e10cSrcweir                 pLineNum[ nCount++ ] = n;
611cdf0e10cSrcweir             }
612cdf0e10cSrcweir     }
613cdf0e10cSrcweir }
614cdf0e10cSrcweir 
~MovedData()615cdf0e10cSrcweir Compare::MovedData::~MovedData()
616cdf0e10cSrcweir {
617d84d9c08SMichael Stahl     delete [] pIndex;
618d84d9c08SMichael Stahl     delete [] pLineNum;
619cdf0e10cSrcweir }
620cdf0e10cSrcweir 
621cdf0e10cSrcweir // ----------------------------------------------------------------------
622cdf0e10cSrcweir 
623cdf0e10cSrcweir     // Suche die verschobenen Lines
CompareSequence(CompareData & rD1,CompareData & rD2,const MovedData & rMD1,const MovedData & rMD2)624cdf0e10cSrcweir Compare::CompareSequence::CompareSequence(
625cdf0e10cSrcweir                             CompareData& rD1, CompareData& rD2,
626cdf0e10cSrcweir                             const MovedData& rMD1, const MovedData& rMD2 )
627cdf0e10cSrcweir     : rData1( rD1 ), rData2( rD2 ), rMoved1( rMD1 ), rMoved2( rMD2 )
628cdf0e10cSrcweir {
629cdf0e10cSrcweir     sal_uLong nSize = rMD1.GetCount() + rMD2.GetCount() + 3;
630cdf0e10cSrcweir     pMemory = new long[ nSize * 2 ];
631cdf0e10cSrcweir     pFDiag = pMemory + ( rMD2.GetCount() + 1 );
632cdf0e10cSrcweir     pBDiag = pMemory + ( nSize + rMD2.GetCount() + 1 );
633cdf0e10cSrcweir 
634cdf0e10cSrcweir     Compare( 0, rMD1.GetCount(), 0, rMD2.GetCount() );
635cdf0e10cSrcweir }
636cdf0e10cSrcweir 
~CompareSequence()637cdf0e10cSrcweir Compare::CompareSequence::~CompareSequence()
638cdf0e10cSrcweir {
639d84d9c08SMichael Stahl     delete [] pMemory;
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
Compare(sal_uLong nStt1,sal_uLong nEnd1,sal_uLong nStt2,sal_uLong nEnd2)642cdf0e10cSrcweir void Compare::CompareSequence::Compare( sal_uLong nStt1, sal_uLong nEnd1,
643cdf0e10cSrcweir                                         sal_uLong nStt2, sal_uLong nEnd2 )
644cdf0e10cSrcweir {
645cdf0e10cSrcweir     /* Slide down the bottom initial diagonal. */
646cdf0e10cSrcweir     while( nStt1 < nEnd1 && nStt2 < nEnd2 &&
647cdf0e10cSrcweir         rMoved1.GetIndex( nStt1 ) == rMoved2.GetIndex( nStt2 ))
648cdf0e10cSrcweir         ++nStt1, ++nStt2;
649cdf0e10cSrcweir 
650cdf0e10cSrcweir     /* Slide up the top initial diagonal. */
651cdf0e10cSrcweir     while( nEnd1 > nStt1 && nEnd2 > nStt2 &&
652cdf0e10cSrcweir         rMoved1.GetIndex( nEnd1 - 1 ) == rMoved2.GetIndex( nEnd2 - 1 ))
653cdf0e10cSrcweir         --nEnd1, --nEnd2;
654cdf0e10cSrcweir 
655cdf0e10cSrcweir     /* Handle simple cases. */
656cdf0e10cSrcweir     if( nStt1 == nEnd1 )
657cdf0e10cSrcweir         while( nStt2 < nEnd2 )
658cdf0e10cSrcweir             rData2.SetChanged( rMoved2.GetLineNum( nStt2++ ));
659cdf0e10cSrcweir 
660cdf0e10cSrcweir     else if (nStt2 == nEnd2)
661cdf0e10cSrcweir         while (nStt1 < nEnd1)
662cdf0e10cSrcweir             rData1.SetChanged( rMoved1.GetLineNum( nStt1++ ));
663cdf0e10cSrcweir 
664cdf0e10cSrcweir     else
665cdf0e10cSrcweir     {
666cdf0e10cSrcweir         sal_uLong c, d, b;
667cdf0e10cSrcweir 
668cdf0e10cSrcweir         /* Find a point of correspondence in the middle of the files.  */
669cdf0e10cSrcweir 
670cdf0e10cSrcweir         d = CheckDiag( nStt1, nEnd1, nStt2, nEnd2, &c );
671cdf0e10cSrcweir         b = pBDiag[ d ];
672cdf0e10cSrcweir 
673cdf0e10cSrcweir         if( 1 != c )
674cdf0e10cSrcweir         {
675cdf0e10cSrcweir             /* Use that point to split this problem into two subproblems.  */
676cdf0e10cSrcweir             Compare( nStt1, b, nStt2, b - d );
677cdf0e10cSrcweir             /* This used to use f instead of b,
678cdf0e10cSrcweir                but that is incorrect!
679cdf0e10cSrcweir                It is not necessarily the case that diagonal d
680cdf0e10cSrcweir                has a snake from b to f.  */
681cdf0e10cSrcweir             Compare( b, nEnd1, b - d, nEnd2 );
682cdf0e10cSrcweir         }
683cdf0e10cSrcweir     }
684cdf0e10cSrcweir }
685cdf0e10cSrcweir 
CheckDiag(sal_uLong nStt1,sal_uLong nEnd1,sal_uLong nStt2,sal_uLong nEnd2,sal_uLong * pCost)686cdf0e10cSrcweir sal_uLong Compare::CompareSequence::CheckDiag( sal_uLong nStt1, sal_uLong nEnd1,
687cdf0e10cSrcweir                                     sal_uLong nStt2, sal_uLong nEnd2, sal_uLong* pCost )
688cdf0e10cSrcweir {
689cdf0e10cSrcweir     const long dmin = nStt1 - nEnd2;    /* Minimum valid diagonal. */
690cdf0e10cSrcweir     const long dmax = nEnd1 - nStt2;    /* Maximum valid diagonal. */
691cdf0e10cSrcweir     const long fmid = nStt1 - nStt2;    /* Center diagonal of top-down search. */
692cdf0e10cSrcweir     const long bmid = nEnd1 - nEnd2;    /* Center diagonal of bottom-up search. */
693cdf0e10cSrcweir 
694cdf0e10cSrcweir     long fmin = fmid, fmax = fmid;  /* Limits of top-down search. */
695cdf0e10cSrcweir     long bmin = bmid, bmax = bmid;  /* Limits of bottom-up search. */
696cdf0e10cSrcweir 
697cdf0e10cSrcweir     long c;         /* Cost. */
698cdf0e10cSrcweir     long odd = (fmid - bmid) & 1;   /* True if southeast corner is on an odd
699cdf0e10cSrcweir                      diagonal with respect to the northwest. */
700cdf0e10cSrcweir 
701cdf0e10cSrcweir     pFDiag[fmid] = nStt1;
702cdf0e10cSrcweir     pBDiag[bmid] = nEnd1;
703cdf0e10cSrcweir 
704cdf0e10cSrcweir     for (c = 1;; ++c)
705cdf0e10cSrcweir     {
706cdf0e10cSrcweir         long d;         /* Active diagonal. */
707cdf0e10cSrcweir         long big_snake = 0;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir         /* Extend the top-down search by an edit step in each diagonal. */
710cdf0e10cSrcweir         fmin > dmin ? pFDiag[--fmin - 1] = -1 : ++fmin;
711cdf0e10cSrcweir         fmax < dmax ? pFDiag[++fmax + 1] = -1 : --fmax;
712cdf0e10cSrcweir         for (d = fmax; d >= fmin; d -= 2)
713cdf0e10cSrcweir         {
714cdf0e10cSrcweir             long x, y, oldx, tlo = pFDiag[d - 1], thi = pFDiag[d + 1];
715cdf0e10cSrcweir 
716cdf0e10cSrcweir             if (tlo >= thi)
717cdf0e10cSrcweir                 x = tlo + 1;
718cdf0e10cSrcweir             else
719cdf0e10cSrcweir                 x = thi;
720cdf0e10cSrcweir             oldx = x;
721cdf0e10cSrcweir             y = x - d;
722cdf0e10cSrcweir             while( sal_uLong(x) < nEnd1 && sal_uLong(y) < nEnd2 &&
723cdf0e10cSrcweir                 rMoved1.GetIndex( x ) == rMoved2.GetIndex( y ))
724cdf0e10cSrcweir                 ++x, ++y;
725cdf0e10cSrcweir             if (x - oldx > 20)
726cdf0e10cSrcweir                 big_snake = 1;
727cdf0e10cSrcweir             pFDiag[d] = x;
728cdf0e10cSrcweir             if( odd && bmin <= d && d <= bmax && pBDiag[d] <= pFDiag[d] )
729cdf0e10cSrcweir             {
730cdf0e10cSrcweir                 *pCost = 2 * c - 1;
731cdf0e10cSrcweir                 return d;
732cdf0e10cSrcweir             }
733cdf0e10cSrcweir         }
734cdf0e10cSrcweir 
735cdf0e10cSrcweir         /* Similar extend the bottom-up search. */
736cdf0e10cSrcweir         bmin > dmin ? pBDiag[--bmin - 1] = INT_MAX : ++bmin;
737cdf0e10cSrcweir         bmax < dmax ? pBDiag[++bmax + 1] = INT_MAX : --bmax;
738cdf0e10cSrcweir         for (d = bmax; d >= bmin; d -= 2)
739cdf0e10cSrcweir         {
740cdf0e10cSrcweir             long x, y, oldx, tlo = pBDiag[d - 1], thi = pBDiag[d + 1];
741cdf0e10cSrcweir 
742cdf0e10cSrcweir             if (tlo < thi)
743cdf0e10cSrcweir                 x = tlo;
744cdf0e10cSrcweir             else
745cdf0e10cSrcweir                 x = thi - 1;
746cdf0e10cSrcweir             oldx = x;
747cdf0e10cSrcweir             y = x - d;
748cdf0e10cSrcweir             while( sal_uLong(x) > nStt1 && sal_uLong(y) > nStt2 &&
749cdf0e10cSrcweir                 rMoved1.GetIndex( x - 1 ) == rMoved2.GetIndex( y - 1 ))
750cdf0e10cSrcweir                 --x, --y;
751cdf0e10cSrcweir             if (oldx - x > 20)
752cdf0e10cSrcweir                 big_snake = 1;
753cdf0e10cSrcweir             pBDiag[d] = x;
754cdf0e10cSrcweir             if (!odd && fmin <= d && d <= fmax && pBDiag[d] <= pFDiag[d])
755cdf0e10cSrcweir             {
756cdf0e10cSrcweir                 *pCost = 2 * c;
757cdf0e10cSrcweir                 return d;
758cdf0e10cSrcweir             }
759cdf0e10cSrcweir         }
760cdf0e10cSrcweir     }
761cdf0e10cSrcweir }
762cdf0e10cSrcweir 
ShiftBoundaries(CompareData & rData1,CompareData & rData2)763cdf0e10cSrcweir void Compare::ShiftBoundaries( CompareData& rData1, CompareData& rData2 )
764cdf0e10cSrcweir {
765cdf0e10cSrcweir     for( int iz = 0; iz < 2; ++iz )
766cdf0e10cSrcweir     {
767cdf0e10cSrcweir         CompareData* pData = &rData1;
768cdf0e10cSrcweir         CompareData* pOtherData = &rData2;
769cdf0e10cSrcweir 
770cdf0e10cSrcweir         sal_uLong i = 0;
771cdf0e10cSrcweir         sal_uLong j = 0;
772cdf0e10cSrcweir         sal_uLong i_end = pData->GetLineCount();
773cdf0e10cSrcweir         sal_uLong preceding = ULONG_MAX;
774cdf0e10cSrcweir         sal_uLong other_preceding = ULONG_MAX;
775cdf0e10cSrcweir 
776cdf0e10cSrcweir         while (1)
777cdf0e10cSrcweir         {
778cdf0e10cSrcweir             sal_uLong start, other_start;
779cdf0e10cSrcweir 
780cdf0e10cSrcweir             /* Scan forwards to find beginning of another run of changes.
781cdf0e10cSrcweir                Also keep track of the corresponding point in the other file.  */
782cdf0e10cSrcweir 
783cdf0e10cSrcweir             while( i < i_end && !pData->GetChanged( i ) )
784cdf0e10cSrcweir             {
785cdf0e10cSrcweir                 while( pOtherData->GetChanged( j++ ))
786cdf0e10cSrcweir                     /* Non-corresponding lines in the other file
787cdf0e10cSrcweir                        will count as the preceding batch of changes.  */
788cdf0e10cSrcweir                     other_preceding = j;
789cdf0e10cSrcweir                 i++;
790cdf0e10cSrcweir             }
791cdf0e10cSrcweir 
792cdf0e10cSrcweir             if (i == i_end)
793cdf0e10cSrcweir                 break;
794cdf0e10cSrcweir 
795cdf0e10cSrcweir             start = i;
796cdf0e10cSrcweir             other_start = j;
797cdf0e10cSrcweir 
798cdf0e10cSrcweir             while (1)
799cdf0e10cSrcweir             {
800cdf0e10cSrcweir                 /* Now find the end of this run of changes.  */
801cdf0e10cSrcweir 
802cdf0e10cSrcweir                 while( pData->GetChanged( ++i ))
803cdf0e10cSrcweir                     ;
804cdf0e10cSrcweir 
805cdf0e10cSrcweir                 /* If the first changed line matches the following unchanged one,
806cdf0e10cSrcweir                    and this run does not follow right after a previous run,
807cdf0e10cSrcweir                    and there are no lines deleted from the other file here,
808cdf0e10cSrcweir                    then classify the first changed line as unchanged
809cdf0e10cSrcweir                    and the following line as changed in its place.  */
810cdf0e10cSrcweir 
811cdf0e10cSrcweir                 /* You might ask, how could this run follow right after another?
812cdf0e10cSrcweir                    Only because the previous run was shifted here.  */
813cdf0e10cSrcweir 
814cdf0e10cSrcweir                 if( i != i_end &&
815cdf0e10cSrcweir                     pData->GetIndex( start ) == pData->GetIndex( i ) &&
816cdf0e10cSrcweir                     !pOtherData->GetChanged( j ) &&
817cdf0e10cSrcweir                     !( start == preceding || other_start == other_preceding ))
818cdf0e10cSrcweir                 {
819cdf0e10cSrcweir                     pData->SetChanged( start++, 0 );
820cdf0e10cSrcweir                     pData->SetChanged(  i );
821cdf0e10cSrcweir                     /* Since one line-that-matches is now before this run
822cdf0e10cSrcweir                        instead of after, we must advance in the other file
823cdf0e10cSrcweir                        to keep in synch.  */
824cdf0e10cSrcweir                     ++j;
825cdf0e10cSrcweir                 }
826cdf0e10cSrcweir                 else
827cdf0e10cSrcweir                     break;
828cdf0e10cSrcweir             }
829cdf0e10cSrcweir 
830cdf0e10cSrcweir             preceding = i;
831cdf0e10cSrcweir             other_preceding = j;
832cdf0e10cSrcweir         }
833cdf0e10cSrcweir 
834cdf0e10cSrcweir         pData = &rData2;
835cdf0e10cSrcweir         pOtherData = &rData1;
836cdf0e10cSrcweir     }
837cdf0e10cSrcweir }
838cdf0e10cSrcweir 
839*1dda6fa0Smseidel /* */
840cdf0e10cSrcweir 
841cdf0e10cSrcweir class SwCompareLine : public CompareLine
842cdf0e10cSrcweir {
843cdf0e10cSrcweir     const SwNode& rNode;
844cdf0e10cSrcweir public:
845cdf0e10cSrcweir     SwCompareLine( const SwNode& rNd );
846cdf0e10cSrcweir     virtual ~SwCompareLine();
847cdf0e10cSrcweir 
848cdf0e10cSrcweir     virtual sal_uLong GetHashValue() const;
849cdf0e10cSrcweir     virtual sal_Bool Compare( const CompareLine& rLine ) const;
850cdf0e10cSrcweir 
851cdf0e10cSrcweir     static sal_uLong GetTxtNodeHashValue( const SwTxtNode& rNd, sal_uLong nVal );
852cdf0e10cSrcweir     static sal_Bool CompareNode( const SwNode& rDstNd, const SwNode& rSrcNd );
853cdf0e10cSrcweir     static sal_Bool CompareTxtNd( const SwTxtNode& rDstNd,
854cdf0e10cSrcweir                               const SwTxtNode& rSrcNd );
855cdf0e10cSrcweir 
856cdf0e10cSrcweir     sal_Bool ChangesInLine( const SwCompareLine& rLine,
857cdf0e10cSrcweir                             SwPaM *& rpInsRing, SwPaM*& rpDelRing ) const;
858cdf0e10cSrcweir 
GetNode() const859cdf0e10cSrcweir     const SwNode& GetNode() const { return rNode; }
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     const SwNode& GetEndNode() const;
862cdf0e10cSrcweir 
863cdf0e10cSrcweir     // fuers Debugging!
864cdf0e10cSrcweir     String GetText() const;
865cdf0e10cSrcweir };
866cdf0e10cSrcweir 
867cdf0e10cSrcweir class SwCompareData : public CompareData
868cdf0e10cSrcweir {
869cdf0e10cSrcweir     SwDoc& rDoc;
870cdf0e10cSrcweir     SwPaM *pInsRing, *pDelRing;
871cdf0e10cSrcweir 
872cdf0e10cSrcweir     sal_uLong PrevIdx( const SwNode* pNd );
873cdf0e10cSrcweir     sal_uLong NextIdx( const SwNode* pNd );
874cdf0e10cSrcweir 
875cdf0e10cSrcweir     virtual void CheckRanges( CompareData& );
876cdf0e10cSrcweir     virtual void ShowInsert( sal_uLong nStt, sal_uLong nEnd );
877cdf0e10cSrcweir     virtual void ShowDelete( const CompareData& rData, sal_uLong nStt,
878cdf0e10cSrcweir                                 sal_uLong nEnd, sal_uLong nInsPos );
879cdf0e10cSrcweir 
880cdf0e10cSrcweir     virtual void CheckForChangesInLine( const CompareData& rData,
881cdf0e10cSrcweir                                     sal_uLong& nStt, sal_uLong& nEnd,
882cdf0e10cSrcweir                                     sal_uLong& nThisStt, sal_uLong& nThisEnd );
883cdf0e10cSrcweir 
884cdf0e10cSrcweir public:
SwCompareData(SwDoc & rD)885cdf0e10cSrcweir     SwCompareData( SwDoc& rD ) : rDoc( rD ), pInsRing(0), pDelRing(0) {}
886cdf0e10cSrcweir     virtual ~SwCompareData();
887cdf0e10cSrcweir 
888cdf0e10cSrcweir     void SetRedlinesToDoc( sal_Bool bUseDocInfo );
889cdf0e10cSrcweir };
890cdf0e10cSrcweir 
891cdf0e10cSrcweir // ----------------------------------------------------------------
892cdf0e10cSrcweir 
SwCompareLine(const SwNode & rNd)893cdf0e10cSrcweir SwCompareLine::SwCompareLine( const SwNode& rNd )
894cdf0e10cSrcweir     : rNode( rNd )
895cdf0e10cSrcweir {
896cdf0e10cSrcweir }
897cdf0e10cSrcweir 
~SwCompareLine()898cdf0e10cSrcweir SwCompareLine::~SwCompareLine()
899cdf0e10cSrcweir {
900cdf0e10cSrcweir }
901cdf0e10cSrcweir 
GetHashValue() const902cdf0e10cSrcweir sal_uLong SwCompareLine::GetHashValue() const
903cdf0e10cSrcweir {
904cdf0e10cSrcweir     sal_uLong nRet = 0;
905cdf0e10cSrcweir     switch( rNode.GetNodeType() )
906cdf0e10cSrcweir     {
907cdf0e10cSrcweir     case ND_TEXTNODE:
908cdf0e10cSrcweir         nRet = GetTxtNodeHashValue( (SwTxtNode&)rNode, nRet );
909cdf0e10cSrcweir         break;
910cdf0e10cSrcweir 
911cdf0e10cSrcweir     case ND_TABLENODE:
912cdf0e10cSrcweir         {
913cdf0e10cSrcweir             const SwNode* pEndNd = rNode.EndOfSectionNode();
914cdf0e10cSrcweir             SwNodeIndex aIdx( rNode );
915cdf0e10cSrcweir             while( &aIdx.GetNode() != pEndNd )
916cdf0e10cSrcweir             {
917cdf0e10cSrcweir                 if( aIdx.GetNode().IsTxtNode() )
918cdf0e10cSrcweir                     nRet = GetTxtNodeHashValue( (SwTxtNode&)aIdx.GetNode(), nRet );
919cdf0e10cSrcweir                 aIdx++;
920cdf0e10cSrcweir             }
921cdf0e10cSrcweir         }
922cdf0e10cSrcweir         break;
923cdf0e10cSrcweir 
924cdf0e10cSrcweir     case ND_SECTIONNODE:
925cdf0e10cSrcweir         {
926cdf0e10cSrcweir             String sStr( GetText() );
927cdf0e10cSrcweir             for( xub_StrLen n = 0; n < sStr.Len(); ++n )
928cdf0e10cSrcweir                 ( nRet <<= 1 ) += sStr.GetChar( n );
929cdf0e10cSrcweir         }
930cdf0e10cSrcweir         break;
931cdf0e10cSrcweir 
932cdf0e10cSrcweir     case ND_GRFNODE:
933cdf0e10cSrcweir     case ND_OLENODE:
934cdf0e10cSrcweir         // feste Id ? sollte aber nie auftauchen
935cdf0e10cSrcweir         break;
936cdf0e10cSrcweir     }
937cdf0e10cSrcweir     return nRet;
938cdf0e10cSrcweir }
939cdf0e10cSrcweir 
GetEndNode() const940cdf0e10cSrcweir const SwNode& SwCompareLine::GetEndNode() const
941cdf0e10cSrcweir {
942cdf0e10cSrcweir     const SwNode* pNd = &rNode;
943cdf0e10cSrcweir     switch( rNode.GetNodeType() )
944cdf0e10cSrcweir     {
945cdf0e10cSrcweir     case ND_TABLENODE:
946cdf0e10cSrcweir         pNd = rNode.EndOfSectionNode();
947cdf0e10cSrcweir         break;
948cdf0e10cSrcweir 
949cdf0e10cSrcweir     case ND_SECTIONNODE:
950cdf0e10cSrcweir         {
951cdf0e10cSrcweir             const SwSectionNode& rSNd = (SwSectionNode&)rNode;
952cdf0e10cSrcweir             const SwSection& rSect = rSNd.GetSection();
953cdf0e10cSrcweir             if( CONTENT_SECTION != rSect.GetType() || rSect.IsProtect() )
954cdf0e10cSrcweir                 pNd = rNode.EndOfSectionNode();
955cdf0e10cSrcweir         }
956cdf0e10cSrcweir         break;
957cdf0e10cSrcweir     }
958cdf0e10cSrcweir     return *pNd;
959cdf0e10cSrcweir }
960cdf0e10cSrcweir 
Compare(const CompareLine & rLine) const961cdf0e10cSrcweir sal_Bool SwCompareLine::Compare( const CompareLine& rLine ) const
962cdf0e10cSrcweir {
963cdf0e10cSrcweir     return CompareNode( rNode, ((SwCompareLine&)rLine).rNode );
964cdf0e10cSrcweir }
965cdf0e10cSrcweir 
966cdf0e10cSrcweir namespace
967cdf0e10cSrcweir {
SimpleTableToText(const SwNode & rNode)968cdf0e10cSrcweir     static String SimpleTableToText(const SwNode &rNode)
969cdf0e10cSrcweir     {
970cdf0e10cSrcweir         String sRet;
971cdf0e10cSrcweir         const SwNode* pEndNd = rNode.EndOfSectionNode();
972cdf0e10cSrcweir         SwNodeIndex aIdx( rNode );
973cdf0e10cSrcweir         while (&aIdx.GetNode() != pEndNd)
974cdf0e10cSrcweir         {
975cdf0e10cSrcweir             if (aIdx.GetNode().IsTxtNode())
976cdf0e10cSrcweir             {
977cdf0e10cSrcweir                 if (sRet.Len())
978cdf0e10cSrcweir                 {
979cdf0e10cSrcweir                     sRet.Append( '\n' );
980cdf0e10cSrcweir                 }
981cdf0e10cSrcweir                 sRet.Append( aIdx.GetNode().GetTxtNode()->GetExpandTxt() );
982cdf0e10cSrcweir             }
983cdf0e10cSrcweir             aIdx++;
984cdf0e10cSrcweir         }
985cdf0e10cSrcweir         return sRet;
986cdf0e10cSrcweir     }
987cdf0e10cSrcweir }
988cdf0e10cSrcweir 
CompareNode(const SwNode & rDstNd,const SwNode & rSrcNd)989cdf0e10cSrcweir sal_Bool SwCompareLine::CompareNode( const SwNode& rDstNd, const SwNode& rSrcNd )
990cdf0e10cSrcweir {
991cdf0e10cSrcweir     if( rSrcNd.GetNodeType() != rDstNd.GetNodeType() )
992cdf0e10cSrcweir         return sal_False;
993cdf0e10cSrcweir 
994cdf0e10cSrcweir     sal_Bool bRet = sal_False;
995cdf0e10cSrcweir 
996cdf0e10cSrcweir     switch( rDstNd.GetNodeType() )
997cdf0e10cSrcweir     {
998cdf0e10cSrcweir     case ND_TEXTNODE:
999cdf0e10cSrcweir         bRet = CompareTxtNd( (SwTxtNode&)rDstNd, (SwTxtNode&)rSrcNd );
1000cdf0e10cSrcweir         break;
1001cdf0e10cSrcweir 
1002cdf0e10cSrcweir     case ND_TABLENODE:
1003cdf0e10cSrcweir         {
1004cdf0e10cSrcweir             const SwTableNode& rTSrcNd = (SwTableNode&)rSrcNd;
1005cdf0e10cSrcweir             const SwTableNode& rTDstNd = (SwTableNode&)rDstNd;
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir             bRet = ( rTSrcNd.EndOfSectionIndex() - rTSrcNd.GetIndex() ) ==
1008cdf0e10cSrcweir                    ( rTDstNd.EndOfSectionIndex() - rTDstNd.GetIndex() );
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir             // --> #i107826#: compare actual table content
1011cdf0e10cSrcweir             if (bRet)
1012cdf0e10cSrcweir             {
1013cdf0e10cSrcweir                 bRet = (SimpleTableToText(rSrcNd) == SimpleTableToText(rDstNd));
1014cdf0e10cSrcweir             }
1015cdf0e10cSrcweir             // <--
1016cdf0e10cSrcweir         }
1017cdf0e10cSrcweir         break;
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir     case ND_SECTIONNODE:
1020cdf0e10cSrcweir         {
1021cdf0e10cSrcweir             const SwSectionNode& rSSrcNd = (SwSectionNode&)rSrcNd,
1022cdf0e10cSrcweir                                & rSDstNd = (SwSectionNode&)rDstNd;
1023cdf0e10cSrcweir             const SwSection& rSrcSect = rSSrcNd.GetSection(),
1024cdf0e10cSrcweir                            & rDstSect = rSDstNd.GetSection();
1025cdf0e10cSrcweir             SectionType eSrcSectType = rSrcSect.GetType(),
1026cdf0e10cSrcweir                         eDstSectType = rDstSect.GetType();
1027cdf0e10cSrcweir             switch( eSrcSectType )
1028cdf0e10cSrcweir             {
1029cdf0e10cSrcweir             case CONTENT_SECTION:
1030cdf0e10cSrcweir                 bRet = CONTENT_SECTION == eDstSectType &&
1031cdf0e10cSrcweir                         rSrcSect.IsProtect() == rDstSect.IsProtect();
1032cdf0e10cSrcweir                 if( bRet && rSrcSect.IsProtect() )
1033cdf0e10cSrcweir                 {
1034cdf0e10cSrcweir                     // the only have they both the same size
1035cdf0e10cSrcweir                     bRet = ( rSSrcNd.EndOfSectionIndex() - rSSrcNd.GetIndex() ) ==
1036cdf0e10cSrcweir                            ( rSDstNd.EndOfSectionIndex() - rSDstNd.GetIndex() );
1037cdf0e10cSrcweir                 }
1038cdf0e10cSrcweir                 break;
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir             case TOX_HEADER_SECTION:
1041cdf0e10cSrcweir             case TOX_CONTENT_SECTION:
1042cdf0e10cSrcweir                 if( TOX_HEADER_SECTION == eDstSectType ||
1043cdf0e10cSrcweir                     TOX_CONTENT_SECTION == eDstSectType )
1044cdf0e10cSrcweir                 {
1045cdf0e10cSrcweir                     // the same type of TOX?
1046cdf0e10cSrcweir                     const SwTOXBase* pSrcTOX = rSrcSect.GetTOXBase();
1047cdf0e10cSrcweir                     const SwTOXBase* pDstTOX = rDstSect.GetTOXBase();
1048cdf0e10cSrcweir                     bRet =  pSrcTOX && pDstTOX
1049cdf0e10cSrcweir                             && pSrcTOX->GetType() == pDstTOX->GetType()
1050cdf0e10cSrcweir                             && pSrcTOX->GetTitle() == pDstTOX->GetTitle()
1051cdf0e10cSrcweir                             && pSrcTOX->GetTypeName() == pDstTOX->GetTypeName()
1052cdf0e10cSrcweir //                          && pSrcTOX->GetTOXName() == pDstTOX->GetTOXName()
1053cdf0e10cSrcweir                             ;
1054cdf0e10cSrcweir                 }
1055cdf0e10cSrcweir                 break;
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir             case DDE_LINK_SECTION:
1058cdf0e10cSrcweir             case FILE_LINK_SECTION:
1059cdf0e10cSrcweir                 bRet = eSrcSectType == eDstSectType &&
1060cdf0e10cSrcweir                         rSrcSect.GetLinkFileName() ==
1061cdf0e10cSrcweir                         rDstSect.GetLinkFileName();
1062cdf0e10cSrcweir                 break;
1063cdf0e10cSrcweir             }
1064cdf0e10cSrcweir         }
1065cdf0e10cSrcweir         break;
1066cdf0e10cSrcweir 
1067cdf0e10cSrcweir     case ND_ENDNODE:
1068cdf0e10cSrcweir         bRet = rSrcNd.StartOfSectionNode()->GetNodeType() ==
1069cdf0e10cSrcweir                rDstNd.StartOfSectionNode()->GetNodeType();
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir         // --> #i107826#: compare actual table content
1072cdf0e10cSrcweir         if (bRet && rSrcNd.StartOfSectionNode()->GetNodeType() == ND_TABLENODE)
1073cdf0e10cSrcweir         {
1074cdf0e10cSrcweir             bRet = CompareNode(
1075cdf0e10cSrcweir                 *rSrcNd.StartOfSectionNode(), *rDstNd.StartOfSectionNode());
1076cdf0e10cSrcweir         }
1077cdf0e10cSrcweir         // <--
1078cdf0e10cSrcweir 
1079cdf0e10cSrcweir         break;
1080cdf0e10cSrcweir     }
1081cdf0e10cSrcweir     return bRet;
1082cdf0e10cSrcweir }
1083cdf0e10cSrcweir 
GetText() const1084cdf0e10cSrcweir String SwCompareLine::GetText() const
1085cdf0e10cSrcweir {
1086cdf0e10cSrcweir     String sRet;
1087cdf0e10cSrcweir     switch( rNode.GetNodeType() )
1088cdf0e10cSrcweir     {
1089cdf0e10cSrcweir     case ND_TEXTNODE:
1090cdf0e10cSrcweir         sRet = ((SwTxtNode&)rNode).GetExpandTxt();
1091cdf0e10cSrcweir         break;
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir     case ND_TABLENODE:
1094cdf0e10cSrcweir         {
1095cdf0e10cSrcweir             sRet = SimpleTableToText(rNode);
1096cdf0e10cSrcweir             sRet.InsertAscii( "Tabelle: ", 0 );
1097cdf0e10cSrcweir         }
1098cdf0e10cSrcweir         break;
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir     case ND_SECTIONNODE:
1101cdf0e10cSrcweir         {
1102cdf0e10cSrcweir             sRet.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "Section - Node:" ));
1103cdf0e10cSrcweir 
1104cdf0e10cSrcweir             const SwSectionNode& rSNd = (SwSectionNode&)rNode;
1105cdf0e10cSrcweir             const SwSection& rSect = rSNd.GetSection();
1106cdf0e10cSrcweir             switch( rSect.GetType() )
1107cdf0e10cSrcweir             {
1108cdf0e10cSrcweir             case CONTENT_SECTION:
1109cdf0e10cSrcweir                 if( rSect.IsProtect() )
1110cdf0e10cSrcweir                     sRet.Append( String::CreateFromInt32(
1111cdf0e10cSrcweir                             rSNd.EndOfSectionIndex() - rSNd.GetIndex() ));
1112cdf0e10cSrcweir                 break;
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir             case TOX_HEADER_SECTION:
1115cdf0e10cSrcweir             case TOX_CONTENT_SECTION:
1116cdf0e10cSrcweir                 {
1117cdf0e10cSrcweir                     const SwTOXBase* pTOX = rSect.GetTOXBase();
1118cdf0e10cSrcweir                     if( pTOX )
1119cdf0e10cSrcweir                         sRet.Append( pTOX->GetTitle() )
1120cdf0e10cSrcweir                             .Append( pTOX->GetTypeName() )
1121cdf0e10cSrcweir //                          .Append( pTOX->GetTOXName() )
1122cdf0e10cSrcweir                             .Append( String::CreateFromInt32( pTOX->GetType() ));
1123cdf0e10cSrcweir                 }
1124cdf0e10cSrcweir                 break;
1125cdf0e10cSrcweir 
1126cdf0e10cSrcweir             case DDE_LINK_SECTION:
1127cdf0e10cSrcweir             case FILE_LINK_SECTION:
1128cdf0e10cSrcweir                 sRet += rSect.GetLinkFileName();
1129cdf0e10cSrcweir                 break;
1130cdf0e10cSrcweir             }
1131cdf0e10cSrcweir         }
1132cdf0e10cSrcweir         break;
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir     case ND_GRFNODE:
1135cdf0e10cSrcweir         sRet.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "Grafik - Node:" ));
1136cdf0e10cSrcweir         break;
1137cdf0e10cSrcweir     case ND_OLENODE:
1138cdf0e10cSrcweir         sRet.AssignAscii( RTL_CONSTASCII_STRINGPARAM( "OLE - Node:" ));
1139cdf0e10cSrcweir         break;
1140cdf0e10cSrcweir     }
1141cdf0e10cSrcweir     return sRet;
1142cdf0e10cSrcweir }
1143cdf0e10cSrcweir 
GetTxtNodeHashValue(const SwTxtNode & rNd,sal_uLong nVal)1144cdf0e10cSrcweir sal_uLong SwCompareLine::GetTxtNodeHashValue( const SwTxtNode& rNd, sal_uLong nVal )
1145cdf0e10cSrcweir {
1146cdf0e10cSrcweir     String sStr( rNd.GetExpandTxt() );
1147cdf0e10cSrcweir     for( xub_StrLen n = 0; n < sStr.Len(); ++n )
1148cdf0e10cSrcweir         ( nVal <<= 1 ) += sStr.GetChar( n );
1149cdf0e10cSrcweir     return nVal;
1150cdf0e10cSrcweir }
1151cdf0e10cSrcweir 
CompareTxtNd(const SwTxtNode & rDstNd,const SwTxtNode & rSrcNd)1152cdf0e10cSrcweir sal_Bool SwCompareLine::CompareTxtNd( const SwTxtNode& rDstNd,
1153cdf0e10cSrcweir                                   const SwTxtNode& rSrcNd )
1154cdf0e10cSrcweir {
1155cdf0e10cSrcweir     sal_Bool bRet = sal_False;
1156cdf0e10cSrcweir     // erstmal ganz einfach!
1157cdf0e10cSrcweir     if( rDstNd.GetTxt() == rSrcNd.GetTxt() )
1158cdf0e10cSrcweir     {
1159cdf0e10cSrcweir         // der Text ist gleich, aber sind die "Sonderattribute" (0xFF) auch
1160cdf0e10cSrcweir         // dieselben??
1161cdf0e10cSrcweir         bRet = sal_True;
1162cdf0e10cSrcweir     }
1163cdf0e10cSrcweir     return bRet;
1164cdf0e10cSrcweir }
1165cdf0e10cSrcweir 
ChangesInLine(const SwCompareLine & rLine,SwPaM * & rpInsRing,SwPaM * & rpDelRing) const1166cdf0e10cSrcweir sal_Bool SwCompareLine::ChangesInLine( const SwCompareLine& rLine,
1167cdf0e10cSrcweir                             SwPaM *& rpInsRing, SwPaM*& rpDelRing ) const
1168cdf0e10cSrcweir {
1169cdf0e10cSrcweir     sal_Bool bRet = sal_False;
1170cdf0e10cSrcweir     if( ND_TEXTNODE == rNode.GetNodeType() &&
1171cdf0e10cSrcweir         ND_TEXTNODE == rLine.GetNode().GetNodeType() )
1172cdf0e10cSrcweir     {
1173cdf0e10cSrcweir         SwTxtNode& rDestNd = *(SwTxtNode*)rNode.GetTxtNode();
1174cdf0e10cSrcweir         const SwTxtNode& rSrcNd = *rLine.GetNode().GetTxtNode();
1175cdf0e10cSrcweir 
1176cdf0e10cSrcweir         xub_StrLen nDEnd = rDestNd.GetTxt().Len(), nSEnd = rSrcNd.GetTxt().Len();
1177cdf0e10cSrcweir         xub_StrLen nStt;
1178cdf0e10cSrcweir         xub_StrLen nEnd;
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir         for( nStt = 0, nEnd = Min( nDEnd, nSEnd ); nStt < nEnd; ++nStt )
1181cdf0e10cSrcweir             if( rDestNd.GetTxt().GetChar( nStt ) !=
1182cdf0e10cSrcweir                 rSrcNd.GetTxt().GetChar( nStt ) )
1183cdf0e10cSrcweir                 break;
1184cdf0e10cSrcweir 
1185cdf0e10cSrcweir         while( nStt < nDEnd && nStt < nSEnd )
1186cdf0e10cSrcweir         {
1187cdf0e10cSrcweir             --nDEnd, --nSEnd;
1188cdf0e10cSrcweir             if( rDestNd.GetTxt().GetChar( nDEnd ) !=
1189cdf0e10cSrcweir                 rSrcNd.GetTxt().GetChar( nSEnd ) )
1190cdf0e10cSrcweir             {
1191cdf0e10cSrcweir                 ++nDEnd, ++nSEnd;
1192cdf0e10cSrcweir                 break;
1193cdf0e10cSrcweir             }
1194cdf0e10cSrcweir         }
1195cdf0e10cSrcweir 
1196cdf0e10cSrcweir         if( nStt || !nDEnd || !nSEnd || nDEnd < rDestNd.GetTxt().Len() ||
1197cdf0e10cSrcweir             nSEnd < rSrcNd.GetTxt().Len() )
1198cdf0e10cSrcweir         {
1199cdf0e10cSrcweir             // jetzt ist zwischen nStt bis nDEnd das neu eingefuegte
1200cdf0e10cSrcweir             // und zwischen nStt und nSEnd das geloeschte
1201cdf0e10cSrcweir             SwDoc* pDoc = rDestNd.GetDoc();
1202cdf0e10cSrcweir             SwPaM aPam( rDestNd, nDEnd );
1203cdf0e10cSrcweir             if( nStt != nDEnd )
1204cdf0e10cSrcweir             {
1205cdf0e10cSrcweir                 SwPaM* pTmp = new SwPaM( *aPam.GetPoint(), rpInsRing );
1206cdf0e10cSrcweir                 if( !rpInsRing )
1207cdf0e10cSrcweir                     rpInsRing = pTmp;
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir                 pTmp->SetMark();
1210cdf0e10cSrcweir                 pTmp->GetMark()->nContent = nStt;
1211cdf0e10cSrcweir             }
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir             if( nStt != nSEnd )
1214cdf0e10cSrcweir             {
1215cdf0e10cSrcweir                 {
1216cdf0e10cSrcweir                     ::sw::UndoGuard const ug(pDoc->GetIDocumentUndoRedo());
1217cdf0e10cSrcweir                     SwPaM aCpyPam( rSrcNd, nStt );
1218cdf0e10cSrcweir                     aCpyPam.SetMark();
1219cdf0e10cSrcweir                     aCpyPam.GetPoint()->nContent = nSEnd;
1220cdf0e10cSrcweir                     aCpyPam.GetDoc()->CopyRange( aCpyPam, *aPam.GetPoint(),
1221cdf0e10cSrcweir                             false );
1222cdf0e10cSrcweir                 }
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir                 SwPaM* pTmp = new SwPaM( *aPam.GetPoint(), rpDelRing );
1225cdf0e10cSrcweir                 if( !rpDelRing )
1226cdf0e10cSrcweir                     rpDelRing = pTmp;
1227cdf0e10cSrcweir 
1228cdf0e10cSrcweir                 pTmp->SetMark();
1229cdf0e10cSrcweir                 pTmp->GetMark()->nContent = nDEnd;
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir                 if( rpInsRing )
1232cdf0e10cSrcweir                 {
1233cdf0e10cSrcweir                     SwPaM* pCorr = (SwPaM*)rpInsRing->GetPrev();
1234cdf0e10cSrcweir                     if( *pCorr->GetPoint() == *pTmp->GetPoint() )
1235cdf0e10cSrcweir                         *pCorr->GetPoint() = *pTmp->GetMark();
1236cdf0e10cSrcweir                 }
1237cdf0e10cSrcweir             }
1238cdf0e10cSrcweir             bRet = sal_True;
1239cdf0e10cSrcweir         }
1240cdf0e10cSrcweir     }
1241cdf0e10cSrcweir     return bRet;
1242cdf0e10cSrcweir }
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir // ----------------------------------------------------------------
1245cdf0e10cSrcweir 
~SwCompareData()1246cdf0e10cSrcweir SwCompareData::~SwCompareData()
1247cdf0e10cSrcweir {
1248cdf0e10cSrcweir     if( pDelRing )
1249cdf0e10cSrcweir     {
1250cdf0e10cSrcweir         while( pDelRing->GetNext() != pDelRing )
1251cdf0e10cSrcweir             delete pDelRing->GetNext();
1252cdf0e10cSrcweir         delete pDelRing;
1253cdf0e10cSrcweir     }
1254cdf0e10cSrcweir     if( pInsRing )
1255cdf0e10cSrcweir     {
1256cdf0e10cSrcweir         while( pInsRing->GetNext() != pInsRing )
1257cdf0e10cSrcweir             delete pInsRing->GetNext();
1258cdf0e10cSrcweir         delete pInsRing;
1259cdf0e10cSrcweir     }
1260cdf0e10cSrcweir }
1261cdf0e10cSrcweir 
NextIdx(const SwNode * pNd)1262cdf0e10cSrcweir sal_uLong SwCompareData::NextIdx( const SwNode* pNd )
1263cdf0e10cSrcweir {
1264cdf0e10cSrcweir     if( pNd->IsStartNode() )
1265cdf0e10cSrcweir     {
1266cdf0e10cSrcweir         const SwSectionNode* pSNd;
1267cdf0e10cSrcweir         if( pNd->IsTableNode() ||
1268cdf0e10cSrcweir             ( 0 != (pSNd = pNd->GetSectionNode() ) &&
1269cdf0e10cSrcweir                 ( CONTENT_SECTION != pSNd->GetSection().GetType() ||
1270cdf0e10cSrcweir                     pSNd->GetSection().IsProtect() ) ) )
1271cdf0e10cSrcweir             pNd = pNd->EndOfSectionNode();
1272cdf0e10cSrcweir     }
1273cdf0e10cSrcweir     return pNd->GetIndex() + 1;
1274cdf0e10cSrcweir }
1275cdf0e10cSrcweir 
PrevIdx(const SwNode * pNd)1276cdf0e10cSrcweir sal_uLong SwCompareData::PrevIdx( const SwNode* pNd )
1277cdf0e10cSrcweir {
1278cdf0e10cSrcweir     if( pNd->IsEndNode() )
1279cdf0e10cSrcweir     {
1280cdf0e10cSrcweir         const SwSectionNode* pSNd;
1281cdf0e10cSrcweir         if( pNd->StartOfSectionNode()->IsTableNode() ||
1282cdf0e10cSrcweir             ( 0 != (pSNd = pNd->StartOfSectionNode()->GetSectionNode() ) &&
1283cdf0e10cSrcweir                 ( CONTENT_SECTION != pSNd->GetSection().GetType() ||
1284cdf0e10cSrcweir                     pSNd->GetSection().IsProtect() ) ) )
1285cdf0e10cSrcweir             pNd = pNd->StartOfSectionNode();
1286cdf0e10cSrcweir     }
1287cdf0e10cSrcweir     return pNd->GetIndex() - 1;
1288cdf0e10cSrcweir }
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir 
CheckRanges(CompareData & rData)1291cdf0e10cSrcweir void SwCompareData::CheckRanges( CompareData& rData )
1292cdf0e10cSrcweir {
1293cdf0e10cSrcweir     const SwNodes& rSrcNds = ((SwCompareData&)rData).rDoc.GetNodes();
1294cdf0e10cSrcweir     const SwNodes& rDstNds = rDoc.GetNodes();
1295cdf0e10cSrcweir 
1296cdf0e10cSrcweir     const SwNode& rSrcEndNd = rSrcNds.GetEndOfContent();
1297cdf0e10cSrcweir     const SwNode& rDstEndNd = rDstNds.GetEndOfContent();
1298cdf0e10cSrcweir 
1299cdf0e10cSrcweir     sal_uLong nSrcSttIdx = NextIdx( rSrcEndNd.StartOfSectionNode() );
1300cdf0e10cSrcweir     sal_uLong nSrcEndIdx = rSrcEndNd.GetIndex();
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir     sal_uLong nDstSttIdx = NextIdx( rDstEndNd.StartOfSectionNode() );
1303cdf0e10cSrcweir     sal_uLong nDstEndIdx = rDstEndNd.GetIndex();
1304cdf0e10cSrcweir 
1305cdf0e10cSrcweir     while( nSrcSttIdx < nSrcEndIdx && nDstSttIdx < nDstEndIdx )
1306cdf0e10cSrcweir     {
1307cdf0e10cSrcweir         const SwNode* pSrcNd = rSrcNds[ nSrcSttIdx ];
1308cdf0e10cSrcweir         const SwNode* pDstNd = rDstNds[ nDstSttIdx ];
1309cdf0e10cSrcweir         if( !SwCompareLine::CompareNode( *pSrcNd, *pDstNd ))
1310cdf0e10cSrcweir             break;
1311cdf0e10cSrcweir 
1312cdf0e10cSrcweir         nSrcSttIdx = NextIdx( pSrcNd );
1313cdf0e10cSrcweir         nDstSttIdx = NextIdx( pDstNd );
1314cdf0e10cSrcweir     }
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir     nSrcEndIdx = PrevIdx( &rSrcEndNd );
1317cdf0e10cSrcweir     nDstEndIdx = PrevIdx( &rDstEndNd );
1318cdf0e10cSrcweir     while( nSrcSttIdx < nSrcEndIdx && nDstSttIdx < nDstEndIdx )
1319cdf0e10cSrcweir     {
1320cdf0e10cSrcweir         const SwNode* pSrcNd = rSrcNds[ nSrcEndIdx ];
1321cdf0e10cSrcweir         const SwNode* pDstNd = rDstNds[ nDstEndIdx ];
1322cdf0e10cSrcweir         if( !SwCompareLine::CompareNode( *pSrcNd, *pDstNd ))
1323cdf0e10cSrcweir             break;
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir         nSrcEndIdx = PrevIdx( pSrcNd );
1326cdf0e10cSrcweir         nDstEndIdx = PrevIdx( pDstNd );
1327cdf0e10cSrcweir     }
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir     while( nSrcSttIdx <= nSrcEndIdx )
1330cdf0e10cSrcweir     {
1331cdf0e10cSrcweir         const SwNode* pNd = rSrcNds[ nSrcSttIdx ];
1332cdf0e10cSrcweir         rData.InsertLine( new SwCompareLine( *pNd ) );
1333cdf0e10cSrcweir         nSrcSttIdx = NextIdx( pNd );
1334cdf0e10cSrcweir     }
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir     while( nDstSttIdx <= nDstEndIdx )
1337cdf0e10cSrcweir     {
1338cdf0e10cSrcweir         const SwNode* pNd = rDstNds[ nDstSttIdx ];
1339cdf0e10cSrcweir         InsertLine( new SwCompareLine( *pNd ) );
1340cdf0e10cSrcweir         nDstSttIdx = NextIdx( pNd );
1341cdf0e10cSrcweir     }
1342cdf0e10cSrcweir }
1343cdf0e10cSrcweir 
1344cdf0e10cSrcweir 
ShowInsert(sal_uLong nStt,sal_uLong nEnd)1345cdf0e10cSrcweir void SwCompareData::ShowInsert( sal_uLong nStt, sal_uLong nEnd )
1346cdf0e10cSrcweir {
1347cdf0e10cSrcweir     SwPaM* pTmp = new SwPaM( ((SwCompareLine*)GetLine( nStt ))->GetNode(), 0,
1348cdf0e10cSrcweir                             ((SwCompareLine*)GetLine( nEnd-1 ))->GetEndNode(), 0,
1349cdf0e10cSrcweir                              pInsRing );
1350cdf0e10cSrcweir     if( !pInsRing )
1351cdf0e10cSrcweir         pInsRing = pTmp;
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir     // #i65201#: These SwPaMs are calculated smaller than needed, see comment below
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir }
1356cdf0e10cSrcweir 
ShowDelete(const CompareData & rData,sal_uLong nStt,sal_uLong nEnd,sal_uLong nInsPos)1357dec99bbdSOliver-Rainer Wittmann void SwCompareData::ShowDelete(
1358dec99bbdSOliver-Rainer Wittmann     const CompareData& rData,
1359dec99bbdSOliver-Rainer Wittmann     sal_uLong nStt,
1360dec99bbdSOliver-Rainer Wittmann     sal_uLong nEnd,
1361dec99bbdSOliver-Rainer Wittmann     sal_uLong nInsPos )
1362cdf0e10cSrcweir {
1363cdf0e10cSrcweir     SwNodeRange aRg(
1364cdf0e10cSrcweir         ((SwCompareLine*)rData.GetLine( nStt ))->GetNode(), 0,
1365cdf0e10cSrcweir         ((SwCompareLine*)rData.GetLine( nEnd-1 ))->GetEndNode(), 1 );
1366cdf0e10cSrcweir 
1367cdf0e10cSrcweir     sal_uInt16 nOffset = 0;
1368cdf0e10cSrcweir     const CompareLine* pLine;
1369cdf0e10cSrcweir     if( GetLineCount() == nInsPos )
1370cdf0e10cSrcweir     {
1371cdf0e10cSrcweir         pLine = GetLine( nInsPos-1 );
1372cdf0e10cSrcweir         nOffset = 1;
1373cdf0e10cSrcweir     }
1374cdf0e10cSrcweir     else
1375cdf0e10cSrcweir         pLine = GetLine( nInsPos );
1376cdf0e10cSrcweir 
1377cdf0e10cSrcweir     const SwNode* pLineNd;
1378cdf0e10cSrcweir     if( pLine )
1379cdf0e10cSrcweir     {
1380cdf0e10cSrcweir         if( nOffset )
1381cdf0e10cSrcweir             pLineNd = &((SwCompareLine*)pLine)->GetEndNode();
1382cdf0e10cSrcweir         else
1383cdf0e10cSrcweir             pLineNd = &((SwCompareLine*)pLine)->GetNode();
1384cdf0e10cSrcweir     }
1385cdf0e10cSrcweir     else
1386cdf0e10cSrcweir     {
1387cdf0e10cSrcweir         pLineNd = &rDoc.GetNodes().GetEndOfContent();
1388cdf0e10cSrcweir         nOffset = 0;
1389cdf0e10cSrcweir     }
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir     SwNodeIndex aInsPos( *pLineNd, nOffset );
1392cdf0e10cSrcweir     SwNodeIndex aSavePos( aInsPos, -1 );
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir     ((SwCompareData&)rData).rDoc.CopyWithFlyInFly( aRg, 0, aInsPos );
1395cdf0e10cSrcweir     rDoc.SetModified();
1396cdf0e10cSrcweir     aSavePos++;
1397cdf0e10cSrcweir 
1398cdf0e10cSrcweir     // #i65201#: These SwPaMs are calculated when the (old) delete-redlines are hidden,
1399cdf0e10cSrcweir     // they will be inserted when the delete-redlines are shown again.
1400cdf0e10cSrcweir     // To avoid unwanted insertions of delete-redlines into these new redlines, what happens
1401cdf0e10cSrcweir     // especially at the end of the document, I reduce the SwPaM by one node.
1402cdf0e10cSrcweir     // Before the new redlines are inserted, they have to expand again.
1403cdf0e10cSrcweir     SwPaM* pTmp = new SwPaM( aSavePos.GetNode(), aInsPos.GetNode(), 0, -1, pDelRing );
1404cdf0e10cSrcweir     if( !pDelRing )
1405cdf0e10cSrcweir         pDelRing = pTmp;
1406cdf0e10cSrcweir 
1407cdf0e10cSrcweir     if( pInsRing )
1408cdf0e10cSrcweir     {
1409cdf0e10cSrcweir         SwPaM* pCorr = (SwPaM*)pInsRing->GetPrev();
1410cdf0e10cSrcweir         if( *pCorr->GetPoint() == *pTmp->GetPoint() )
1411cdf0e10cSrcweir         {
1412cdf0e10cSrcweir             SwNodeIndex aTmpPos( pTmp->GetMark()->nNode, -1 );
1413cdf0e10cSrcweir             *pCorr->GetPoint() = SwPosition( aTmpPos );
1414cdf0e10cSrcweir         }
1415cdf0e10cSrcweir     }
1416cdf0e10cSrcweir }
1417cdf0e10cSrcweir 
CheckForChangesInLine(const CompareData & rData,sal_uLong & rStt,sal_uLong & rEnd,sal_uLong & rThisStt,sal_uLong & rThisEnd)1418cdf0e10cSrcweir void SwCompareData::CheckForChangesInLine( const CompareData& rData,
1419cdf0e10cSrcweir                                     sal_uLong& rStt, sal_uLong& rEnd,
1420cdf0e10cSrcweir                                     sal_uLong& rThisStt, sal_uLong& rThisEnd )
1421cdf0e10cSrcweir {
1422cdf0e10cSrcweir     while( rStt < rEnd && rThisStt < rThisEnd )
1423cdf0e10cSrcweir     {
1424cdf0e10cSrcweir         SwCompareLine* pDstLn = (SwCompareLine*)GetLine( rThisStt );
1425cdf0e10cSrcweir         SwCompareLine* pSrcLn = (SwCompareLine*)rData.GetLine( rStt );
1426cdf0e10cSrcweir         if( !pDstLn->ChangesInLine( *pSrcLn, pInsRing, pDelRing ) )
1427cdf0e10cSrcweir             break;
1428cdf0e10cSrcweir 
1429cdf0e10cSrcweir         ++rStt;
1430cdf0e10cSrcweir         ++rThisStt;
1431cdf0e10cSrcweir     }
1432cdf0e10cSrcweir }
1433cdf0e10cSrcweir 
SetRedlinesToDoc(sal_Bool bUseDocInfo)1434cdf0e10cSrcweir void SwCompareData::SetRedlinesToDoc( sal_Bool bUseDocInfo )
1435cdf0e10cSrcweir {
1436cdf0e10cSrcweir     SwPaM* pTmp = pDelRing;
1437cdf0e10cSrcweir 
1438cdf0e10cSrcweir     // Bug #83296#: get the Author / TimeStamp from the "other"
1439cdf0e10cSrcweir     //              document info
1440cdf0e10cSrcweir     sal_uInt16 nAuthor = rDoc.GetRedlineAuthor();
1441cdf0e10cSrcweir     DateTime aTimeStamp;
1442cdf0e10cSrcweir     SwDocShell *pDocShell(rDoc.GetDocShell());
1443cdf0e10cSrcweir     DBG_ASSERT(pDocShell, "no SwDocShell");
1444cdf0e10cSrcweir     if (pDocShell) {
1445cdf0e10cSrcweir         uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
1446cdf0e10cSrcweir             pDocShell->GetModel(), uno::UNO_QUERY_THROW);
1447cdf0e10cSrcweir         uno::Reference<document::XDocumentProperties> xDocProps(
1448cdf0e10cSrcweir             xDPS->getDocumentProperties());
1449cdf0e10cSrcweir         DBG_ASSERT(xDocProps.is(), "Doc has no DocumentProperties");
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir         if( bUseDocInfo && xDocProps.is() ) {
1452cdf0e10cSrcweir             String aTmp( 1 == xDocProps->getEditingCycles()
1453cdf0e10cSrcweir                                 ? xDocProps->getAuthor()
1454cdf0e10cSrcweir                                 : xDocProps->getModifiedBy() );
1455cdf0e10cSrcweir             util::DateTime uDT( 1 == xDocProps->getEditingCycles()
1456cdf0e10cSrcweir                                 ? xDocProps->getCreationDate()
1457cdf0e10cSrcweir                                 : xDocProps->getModificationDate() );
1458cdf0e10cSrcweir             Date d(uDT.Day, uDT.Month, uDT.Year);
1459cdf0e10cSrcweir             Time t(uDT.Hours, uDT.Minutes, uDT.Seconds, uDT.HundredthSeconds);
1460cdf0e10cSrcweir             DateTime aDT(d,t);
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir             if( aTmp.Len() )
1463cdf0e10cSrcweir             {
1464cdf0e10cSrcweir                 nAuthor = rDoc.InsertRedlineAuthor( aTmp );
1465cdf0e10cSrcweir                 aTimeStamp = aDT;
1466cdf0e10cSrcweir             }
1467cdf0e10cSrcweir         }
1468cdf0e10cSrcweir     }
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir     if( pTmp )
1471cdf0e10cSrcweir     {
1472cdf0e10cSrcweir         SwRedlineData aRedlnData( nsRedlineType_t::REDLINE_DELETE, nAuthor, aTimeStamp,
1473cdf0e10cSrcweir                                     aEmptyStr, 0, 0 );
1474cdf0e10cSrcweir         do {
1475cdf0e10cSrcweir             // #i65201#: Expand again, see comment above.
1476cdf0e10cSrcweir             if( pTmp->GetPoint()->nContent == 0 )
1477cdf0e10cSrcweir             {
1478cdf0e10cSrcweir                 pTmp->GetPoint()->nNode++;
1479cdf0e10cSrcweir                 pTmp->GetPoint()->nContent.Assign( pTmp->GetCntntNode(), 0 );
1480cdf0e10cSrcweir             }
1481cdf0e10cSrcweir             // --> mst 2010-05-17 #i101009#
1482cdf0e10cSrcweir             // prevent redlines that end on structural end node
1483cdf0e10cSrcweir             if (& rDoc.GetNodes().GetEndOfContent() ==
1484cdf0e10cSrcweir                 & pTmp->GetPoint()->nNode.GetNode())
1485cdf0e10cSrcweir             {
1486cdf0e10cSrcweir                 pTmp->GetPoint()->nNode--;
1487cdf0e10cSrcweir                 SwCntntNode *const pContentNode( pTmp->GetCntntNode() );
1488cdf0e10cSrcweir                 pTmp->GetPoint()->nContent.Assign( pContentNode,
1489cdf0e10cSrcweir                         (pContentNode) ? pContentNode->Len() : 0 );
1490cdf0e10cSrcweir             }
1491cdf0e10cSrcweir             // <--
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir             rDoc.DeleteRedline( *pTmp, false, USHRT_MAX );
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir             if (rDoc.GetIDocumentUndoRedo().DoesUndo())
1496cdf0e10cSrcweir             {
1497cdf0e10cSrcweir                 SwUndo *const pUndo(new SwUndoCompDoc( *pTmp, sal_False )) ;
1498cdf0e10cSrcweir                 rDoc.GetIDocumentUndoRedo().AppendUndo(pUndo);
1499cdf0e10cSrcweir             }
1500cdf0e10cSrcweir             rDoc.AppendRedline( new SwRedline( aRedlnData, *pTmp ), true );
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir         } while( pDelRing != ( pTmp = (SwPaM*)pTmp->GetNext() ));
1503cdf0e10cSrcweir     }
1504cdf0e10cSrcweir 
1505cdf0e10cSrcweir     pTmp = pInsRing;
1506cdf0e10cSrcweir     if( pTmp )
1507cdf0e10cSrcweir     {
1508cdf0e10cSrcweir         do {
1509cdf0e10cSrcweir             if( pTmp->GetPoint()->nContent == 0 )
1510cdf0e10cSrcweir             {
1511cdf0e10cSrcweir                 pTmp->GetPoint()->nNode++;
1512cdf0e10cSrcweir                 pTmp->GetPoint()->nContent.Assign( pTmp->GetCntntNode(), 0 );
1513cdf0e10cSrcweir             }
1514cdf0e10cSrcweir             // --> mst 2010-05-17 #i101009#
1515cdf0e10cSrcweir             // prevent redlines that end on structural end node
1516cdf0e10cSrcweir             if (& rDoc.GetNodes().GetEndOfContent() ==
1517cdf0e10cSrcweir                 & pTmp->GetPoint()->nNode.GetNode())
1518cdf0e10cSrcweir             {
1519cdf0e10cSrcweir                 pTmp->GetPoint()->nNode--;
1520cdf0e10cSrcweir                 SwCntntNode *const pContentNode( pTmp->GetCntntNode() );
1521cdf0e10cSrcweir                 pTmp->GetPoint()->nContent.Assign( pContentNode,
1522cdf0e10cSrcweir                         (pContentNode) ? pContentNode->Len() : 0 );
1523cdf0e10cSrcweir             }
1524cdf0e10cSrcweir             // <--
1525cdf0e10cSrcweir         } while( pInsRing != ( pTmp = (SwPaM*)pTmp->GetNext() ));
1526cdf0e10cSrcweir         SwRedlineData aRedlnData( nsRedlineType_t::REDLINE_INSERT, nAuthor, aTimeStamp,
1527cdf0e10cSrcweir                                     aEmptyStr, 0, 0 );
1528cdf0e10cSrcweir 
1529cdf0e10cSrcweir         // zusammenhaengende zusammenfassen
1530cdf0e10cSrcweir         if( pTmp->GetNext() != pInsRing )
1531cdf0e10cSrcweir         {
1532cdf0e10cSrcweir             const SwCntntNode* pCNd;
1533cdf0e10cSrcweir             do {
1534cdf0e10cSrcweir                 SwPosition& rSttEnd = *pTmp->End(),
1535cdf0e10cSrcweir                           & rEndStt = *((SwPaM*)pTmp->GetNext())->Start();
1536cdf0e10cSrcweir                 if( rSttEnd == rEndStt ||
1537cdf0e10cSrcweir                     (!rEndStt.nContent.GetIndex() &&
1538cdf0e10cSrcweir                     rEndStt.nNode.GetIndex() - 1 == rSttEnd.nNode.GetIndex() &&
1539cdf0e10cSrcweir                     0 != ( pCNd = rSttEnd.nNode.GetNode().GetCntntNode() )
1540cdf0e10cSrcweir                         ? rSttEnd.nContent.GetIndex() == pCNd->Len()
1541cdf0e10cSrcweir                         : 0 ))
1542cdf0e10cSrcweir                 {
1543cdf0e10cSrcweir                     if( pTmp->GetNext() == pInsRing )
1544cdf0e10cSrcweir                     {
1545cdf0e10cSrcweir                         // liegen hintereinander also zusammen fassen
1546cdf0e10cSrcweir                         rEndStt = *pTmp->Start();
1547cdf0e10cSrcweir                         delete pTmp;
1548cdf0e10cSrcweir                         pTmp = pInsRing;
1549cdf0e10cSrcweir                     }
1550cdf0e10cSrcweir                     else
1551cdf0e10cSrcweir                     {
1552cdf0e10cSrcweir                         // liegen hintereinander also zusammen fassen
1553cdf0e10cSrcweir                         rSttEnd = *((SwPaM*)pTmp->GetNext())->End();
1554cdf0e10cSrcweir                         delete pTmp->GetNext();
1555cdf0e10cSrcweir                     }
1556cdf0e10cSrcweir                 }
1557cdf0e10cSrcweir                 else
1558cdf0e10cSrcweir                     pTmp = (SwPaM*)pTmp->GetNext();
1559cdf0e10cSrcweir             } while( pInsRing != pTmp );
1560cdf0e10cSrcweir         }
1561cdf0e10cSrcweir 
1562cdf0e10cSrcweir         do {
1563cdf0e10cSrcweir             if( rDoc.AppendRedline( new SwRedline( aRedlnData, *pTmp ), true) &&
1564cdf0e10cSrcweir                 rDoc.GetIDocumentUndoRedo().DoesUndo())
1565cdf0e10cSrcweir             {
1566cdf0e10cSrcweir                 SwUndo *const pUndo(new SwUndoCompDoc( *pTmp, sal_True ));
1567cdf0e10cSrcweir                 rDoc.GetIDocumentUndoRedo().AppendUndo(pUndo);
1568cdf0e10cSrcweir             }
1569cdf0e10cSrcweir         } while( pInsRing != ( pTmp = (SwPaM*)pTmp->GetNext() ));
1570cdf0e10cSrcweir     }
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir 
1573*1dda6fa0Smseidel /* */
1574cdf0e10cSrcweir 
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir 
1577cdf0e10cSrcweir     // returnt (?die Anzahl der Unterschiede?) ob etwas unterschiedlich ist
CompareDoc(const SwDoc & rDoc)1578cdf0e10cSrcweir long SwDoc::CompareDoc( const SwDoc& rDoc )
1579cdf0e10cSrcweir {
1580cdf0e10cSrcweir     if( &rDoc == this )
1581cdf0e10cSrcweir         return 0;
1582cdf0e10cSrcweir 
1583cdf0e10cSrcweir     long nRet = 0;
1584cdf0e10cSrcweir 
1585cdf0e10cSrcweir     GetIDocumentUndoRedo().StartUndo(UNDO_EMPTY, NULL);
1586cdf0e10cSrcweir     sal_Bool bDocWasModified = IsModified();
1587cdf0e10cSrcweir     SwDoc& rSrcDoc = (SwDoc&)rDoc;
1588cdf0e10cSrcweir     sal_Bool bSrcModified = rSrcDoc.IsModified();
1589cdf0e10cSrcweir 
1590cdf0e10cSrcweir     RedlineMode_t eSrcRedlMode = rSrcDoc.GetRedlineMode();
1591cdf0e10cSrcweir     rSrcDoc.SetRedlineMode( nsRedlineMode_t::REDLINE_SHOW_INSERT );
1592cdf0e10cSrcweir     SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_INSERT));
1593cdf0e10cSrcweir 
1594cdf0e10cSrcweir     SwCompareData aD0( rSrcDoc );
1595cdf0e10cSrcweir     SwCompareData aD1( *this );
1596cdf0e10cSrcweir 
1597cdf0e10cSrcweir     aD1.CompareLines( aD0 );
1598cdf0e10cSrcweir 
1599cdf0e10cSrcweir     nRet = aD1.ShowDiffs( aD0 );
1600cdf0e10cSrcweir 
1601cdf0e10cSrcweir     if( nRet )
1602cdf0e10cSrcweir     {
1603cdf0e10cSrcweir       SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_ON |
1604cdf0e10cSrcweir                        nsRedlineMode_t::REDLINE_SHOW_INSERT | nsRedlineMode_t::REDLINE_SHOW_DELETE));
1605cdf0e10cSrcweir 
1606cdf0e10cSrcweir         aD1.SetRedlinesToDoc( !bDocWasModified );
1607cdf0e10cSrcweir         SetModified();
1608cdf0e10cSrcweir     }
1609cdf0e10cSrcweir 
1610cdf0e10cSrcweir     rSrcDoc.SetRedlineMode( eSrcRedlMode );
1611cdf0e10cSrcweir     SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_SHOW_INSERT | nsRedlineMode_t::REDLINE_SHOW_DELETE));
1612cdf0e10cSrcweir 
1613cdf0e10cSrcweir     if( !bSrcModified )
1614cdf0e10cSrcweir         rSrcDoc.ResetModified();
1615cdf0e10cSrcweir 
1616cdf0e10cSrcweir     GetIDocumentUndoRedo().EndUndo(UNDO_EMPTY, NULL);
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir     return nRet;
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir class _SaveMergeRedlines : public Ring
1623cdf0e10cSrcweir {
1624cdf0e10cSrcweir     const SwRedline* pSrcRedl;
1625cdf0e10cSrcweir     SwRedline* pDestRedl;
1626cdf0e10cSrcweir public:
1627cdf0e10cSrcweir     _SaveMergeRedlines( const SwNode& rDstNd,
1628cdf0e10cSrcweir                         const SwRedline& rSrcRedl, Ring* pRing );
1629cdf0e10cSrcweir     sal_uInt16 InsertRedline();
1630cdf0e10cSrcweir 
GetDestRedline()1631cdf0e10cSrcweir     SwRedline* GetDestRedline() { return pDestRedl; }
1632cdf0e10cSrcweir };
1633cdf0e10cSrcweir 
_SaveMergeRedlines(const SwNode & rDstNd,const SwRedline & rSrcRedl,Ring * pRing)1634cdf0e10cSrcweir _SaveMergeRedlines::_SaveMergeRedlines( const SwNode& rDstNd,
1635cdf0e10cSrcweir                         const SwRedline& rSrcRedl, Ring* pRing )
1636cdf0e10cSrcweir     : Ring( pRing ), pSrcRedl( &rSrcRedl )
1637cdf0e10cSrcweir {
1638cdf0e10cSrcweir     SwPosition aPos( rDstNd );
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir     const SwPosition* pStt = rSrcRedl.Start();
1641cdf0e10cSrcweir     if( rDstNd.IsCntntNode() )
1642cdf0e10cSrcweir         aPos.nContent.Assign( ((SwCntntNode*)&rDstNd), pStt->nContent.GetIndex() );
1643cdf0e10cSrcweir     pDestRedl = new SwRedline( rSrcRedl.GetRedlineData(), aPos );
1644cdf0e10cSrcweir 
1645cdf0e10cSrcweir     if( nsRedlineType_t::REDLINE_DELETE == pDestRedl->GetType() )
1646cdf0e10cSrcweir     {
1647cdf0e10cSrcweir         // den Bereich als geloescht kennzeichnen
1648cdf0e10cSrcweir         const SwPosition* pEnd = pStt == rSrcRedl.GetPoint()
1649cdf0e10cSrcweir                                             ? rSrcRedl.GetMark()
1650cdf0e10cSrcweir                                             : rSrcRedl.GetPoint();
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir         pDestRedl->SetMark();
1653cdf0e10cSrcweir         pDestRedl->GetPoint()->nNode += pEnd->nNode.GetIndex() -
1654cdf0e10cSrcweir                                         pStt->nNode.GetIndex();
1655cdf0e10cSrcweir         pDestRedl->GetPoint()->nContent.Assign( pDestRedl->GetCntntNode(),
1656cdf0e10cSrcweir                                                 pEnd->nContent.GetIndex() );
1657cdf0e10cSrcweir     }
1658cdf0e10cSrcweir }
1659cdf0e10cSrcweir 
InsertRedline()1660cdf0e10cSrcweir sal_uInt16 _SaveMergeRedlines::InsertRedline()
1661cdf0e10cSrcweir {
1662cdf0e10cSrcweir     sal_uInt16 nIns = 0;
1663cdf0e10cSrcweir     SwDoc* pDoc = pDestRedl->GetDoc();
1664cdf0e10cSrcweir 
1665cdf0e10cSrcweir     if( nsRedlineType_t::REDLINE_INSERT == pDestRedl->GetType() )
1666cdf0e10cSrcweir     {
1667cdf0e10cSrcweir         // der Teil wurde eingefuegt, also kopiere ihn aus dem SourceDoc
1668cdf0e10cSrcweir         ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
1669cdf0e10cSrcweir 
1670cdf0e10cSrcweir         SwNodeIndex aSaveNd( pDestRedl->GetPoint()->nNode, -1 );
1671cdf0e10cSrcweir         xub_StrLen nSaveCnt = pDestRedl->GetPoint()->nContent.GetIndex();
1672cdf0e10cSrcweir 
1673cdf0e10cSrcweir         RedlineMode_t eOld = pDoc->GetRedlineMode();
1674cdf0e10cSrcweir         pDoc->SetRedlineMode_intern((RedlineMode_t)(eOld | nsRedlineMode_t::REDLINE_IGNORE));
1675cdf0e10cSrcweir 
1676cdf0e10cSrcweir         pSrcRedl->GetDoc()->CopyRange(
1677cdf0e10cSrcweir                 *const_cast<SwPaM*>(static_cast<const SwPaM*>(pSrcRedl)),
1678cdf0e10cSrcweir                 *pDestRedl->GetPoint(), false );
1679cdf0e10cSrcweir 
1680cdf0e10cSrcweir         pDoc->SetRedlineMode_intern( eOld );
1681cdf0e10cSrcweir 
1682cdf0e10cSrcweir         pDestRedl->SetMark();
1683cdf0e10cSrcweir         aSaveNd++;
1684cdf0e10cSrcweir         pDestRedl->GetMark()->nNode = aSaveNd;
1685cdf0e10cSrcweir         pDestRedl->GetMark()->nContent.Assign( aSaveNd.GetNode().GetCntntNode(),
1686cdf0e10cSrcweir                                                 nSaveCnt );
1687cdf0e10cSrcweir 
1688cdf0e10cSrcweir         if( GetPrev() != this )
1689cdf0e10cSrcweir         {
1690cdf0e10cSrcweir             SwPaM* pTmpPrev = ((_SaveMergeRedlines*)GetPrev())->pDestRedl;
1691cdf0e10cSrcweir             if( pTmpPrev && *pTmpPrev->GetPoint() == *pDestRedl->GetPoint() )
1692cdf0e10cSrcweir                 *pTmpPrev->GetPoint() = *pDestRedl->GetMark();
1693cdf0e10cSrcweir         }
1694cdf0e10cSrcweir     }
1695cdf0e10cSrcweir     else
1696cdf0e10cSrcweir     {
1697cdf0e10cSrcweir         //JP 21.09.98: Bug 55909
1698cdf0e10cSrcweir         // falls im Doc auf gleicher Pos aber schon ein geloeschter oder
1699cdf0e10cSrcweir         // eingefuegter ist, dann muss dieser gesplittet werden!
1700cdf0e10cSrcweir         SwPosition* pDStt = pDestRedl->GetMark(),
1701cdf0e10cSrcweir                   * pDEnd = pDestRedl->GetPoint();
1702cdf0e10cSrcweir         sal_uInt16 n = 0;
1703cdf0e10cSrcweir 
1704cdf0e10cSrcweir             // zur StartPos das erste Redline suchen
1705cdf0e10cSrcweir         if( !pDoc->GetRedline( *pDStt, &n ) && n )
1706cdf0e10cSrcweir             --n;
1707cdf0e10cSrcweir 
1708cdf0e10cSrcweir         const SwRedlineTbl& rRedlineTbl = pDoc->GetRedlineTbl();
1709cdf0e10cSrcweir         for( ; n < rRedlineTbl.Count(); ++n )
1710cdf0e10cSrcweir         {
1711cdf0e10cSrcweir             SwRedline* pRedl = rRedlineTbl[ n ];
1712cdf0e10cSrcweir             SwPosition* pRStt = pRedl->Start(),
1713cdf0e10cSrcweir                       * pREnd = pRStt == pRedl->GetPoint() ? pRedl->GetMark()
1714cdf0e10cSrcweir                                                            : pRedl->GetPoint();
1715cdf0e10cSrcweir             if( nsRedlineType_t::REDLINE_DELETE == pRedl->GetType() ||
1716cdf0e10cSrcweir                 nsRedlineType_t::REDLINE_INSERT == pRedl->GetType() )
1717cdf0e10cSrcweir             {
1718cdf0e10cSrcweir                 SwComparePosition eCmpPos = ComparePosition( *pDStt, *pDEnd, *pRStt, *pREnd );
1719cdf0e10cSrcweir                 switch( eCmpPos )
1720cdf0e10cSrcweir                 {
1721cdf0e10cSrcweir                 case POS_COLLIDE_START:
1722cdf0e10cSrcweir                 case POS_BEHIND:
1723cdf0e10cSrcweir                     break;
1724cdf0e10cSrcweir 
1725cdf0e10cSrcweir                 case POS_INSIDE:
1726cdf0e10cSrcweir                 case POS_EQUAL:
1727cdf0e10cSrcweir                     delete pDestRedl, pDestRedl = 0;
1728cdf0e10cSrcweir                     // break; -> kein break !!!!
1729cdf0e10cSrcweir 
1730cdf0e10cSrcweir                 case POS_COLLIDE_END:
1731cdf0e10cSrcweir                 case POS_BEFORE:
1732cdf0e10cSrcweir                     n = rRedlineTbl.Count();
1733cdf0e10cSrcweir                     break;
1734cdf0e10cSrcweir 
1735cdf0e10cSrcweir                 case POS_OUTSIDE:
1736cdf0e10cSrcweir                     {
1737cdf0e10cSrcweir                         SwRedline* pCpyRedl = new SwRedline(
1738cdf0e10cSrcweir                             pDestRedl->GetRedlineData(), *pDStt );
1739cdf0e10cSrcweir                         pCpyRedl->SetMark();
1740cdf0e10cSrcweir                         *pCpyRedl->GetPoint() = *pRStt;
1741cdf0e10cSrcweir 
1742cdf0e10cSrcweir                         SwUndoCompDoc *const pUndo =
1743cdf0e10cSrcweir                             (pDoc->GetIDocumentUndoRedo().DoesUndo())
1744cdf0e10cSrcweir                                     ? new SwUndoCompDoc( *pCpyRedl ) : 0;
1745cdf0e10cSrcweir 
1746cdf0e10cSrcweir                         // now modify doc: append redline, undo (and count)
1747cdf0e10cSrcweir                         pDoc->AppendRedline( pCpyRedl, true );
1748cdf0e10cSrcweir                         if( pUndo )
1749cdf0e10cSrcweir                         {
1750cdf0e10cSrcweir                             pDoc->GetIDocumentUndoRedo().AppendUndo(pUndo);
1751cdf0e10cSrcweir                         }
1752cdf0e10cSrcweir                         ++nIns;
1753cdf0e10cSrcweir 
1754cdf0e10cSrcweir                         *pDStt = *pREnd;
1755cdf0e10cSrcweir 
1756cdf0e10cSrcweir                         // dann solle man neu anfangen
1757cdf0e10cSrcweir                         n = USHRT_MAX;
1758cdf0e10cSrcweir                     }
1759cdf0e10cSrcweir                     break;
1760cdf0e10cSrcweir 
1761cdf0e10cSrcweir                 case POS_OVERLAP_BEFORE:
1762cdf0e10cSrcweir                     *pDEnd = *pRStt;
1763cdf0e10cSrcweir                     break;
1764cdf0e10cSrcweir 
1765cdf0e10cSrcweir                 case POS_OVERLAP_BEHIND:
1766cdf0e10cSrcweir                     *pDStt = *pREnd;
1767cdf0e10cSrcweir                     break;
1768cdf0e10cSrcweir                 }
1769cdf0e10cSrcweir             }
1770cdf0e10cSrcweir             else if( *pDEnd <= *pRStt )
1771cdf0e10cSrcweir                 break;
1772cdf0e10cSrcweir         }
1773cdf0e10cSrcweir 
1774cdf0e10cSrcweir     }
1775cdf0e10cSrcweir 
1776cdf0e10cSrcweir     if( pDestRedl )
1777cdf0e10cSrcweir     {
1778cdf0e10cSrcweir         SwUndoCompDoc *const pUndo = (pDoc->GetIDocumentUndoRedo().DoesUndo())
1779cdf0e10cSrcweir             ? new SwUndoCompDoc( *pDestRedl ) : 0;
1780cdf0e10cSrcweir 
1781cdf0e10cSrcweir         // now modify doc: append redline, undo (and count)
1782cdf0e10cSrcweir         bool bRedlineAccepted = pDoc->AppendRedline( pDestRedl, true );
1783cdf0e10cSrcweir         if( pUndo )
1784cdf0e10cSrcweir         {
1785cdf0e10cSrcweir             pDoc->GetIDocumentUndoRedo().AppendUndo( pUndo );
1786cdf0e10cSrcweir         }
1787cdf0e10cSrcweir         ++nIns;
1788cdf0e10cSrcweir 
1789cdf0e10cSrcweir         // if AppendRedline has deleted our redline, we may not keep a
1790cdf0e10cSrcweir         // reference to it
1791cdf0e10cSrcweir         if( ! bRedlineAccepted )
1792cdf0e10cSrcweir             pDestRedl = NULL;
1793cdf0e10cSrcweir     }
1794cdf0e10cSrcweir     return nIns;
1795cdf0e10cSrcweir }
1796cdf0e10cSrcweir 
1797cdf0e10cSrcweir // merge zweier Dokumente
MergeDoc(const SwDoc & rDoc)1798cdf0e10cSrcweir long SwDoc::MergeDoc( const SwDoc& rDoc )
1799cdf0e10cSrcweir {
1800cdf0e10cSrcweir     if( &rDoc == this )
1801cdf0e10cSrcweir         return 0;
1802cdf0e10cSrcweir 
1803cdf0e10cSrcweir     long nRet = 0;
1804cdf0e10cSrcweir 
1805cdf0e10cSrcweir     GetIDocumentUndoRedo().StartUndo(UNDO_EMPTY, NULL);
1806cdf0e10cSrcweir 
1807cdf0e10cSrcweir     SwDoc& rSrcDoc = (SwDoc&)rDoc;
1808cdf0e10cSrcweir     sal_Bool bSrcModified = rSrcDoc.IsModified();
1809cdf0e10cSrcweir 
1810cdf0e10cSrcweir     RedlineMode_t eSrcRedlMode = rSrcDoc.GetRedlineMode();
1811cdf0e10cSrcweir     rSrcDoc.SetRedlineMode( nsRedlineMode_t::REDLINE_SHOW_DELETE );
1812cdf0e10cSrcweir     SetRedlineMode( nsRedlineMode_t::REDLINE_SHOW_DELETE );
1813cdf0e10cSrcweir 
1814cdf0e10cSrcweir     SwCompareData aD0( rSrcDoc );
1815cdf0e10cSrcweir     SwCompareData aD1( *this );
1816cdf0e10cSrcweir 
1817cdf0e10cSrcweir     aD1.CompareLines( aD0 );
1818cdf0e10cSrcweir 
1819cdf0e10cSrcweir     if( !aD1.HasDiffs( aD0 ) )
1820cdf0e10cSrcweir     {
1821cdf0e10cSrcweir         // jetzt wollen wir alle Redlines aus dem SourceDoc zu uns bekommen
1822cdf0e10cSrcweir 
1823cdf0e10cSrcweir         // suche alle Insert - Redlines aus dem SourceDoc und bestimme
1824cdf0e10cSrcweir         // deren Position im DestDoc
1825cdf0e10cSrcweir         _SaveMergeRedlines* pRing = 0;
1826cdf0e10cSrcweir         const SwRedlineTbl& rSrcRedlTbl = rSrcDoc.GetRedlineTbl();
1827cdf0e10cSrcweir         sal_uLong nEndOfExtra = rSrcDoc.GetNodes().GetEndOfExtras().GetIndex();
1828cdf0e10cSrcweir         sal_uLong nMyEndOfExtra = GetNodes().GetEndOfExtras().GetIndex();
1829cdf0e10cSrcweir         for( sal_uInt16 n = 0; n < rSrcRedlTbl.Count(); ++n )
1830cdf0e10cSrcweir         {
1831cdf0e10cSrcweir             const SwRedline* pRedl = rSrcRedlTbl[ n ];
1832cdf0e10cSrcweir             sal_uLong nNd = pRedl->GetPoint()->nNode.GetIndex();
1833cdf0e10cSrcweir             RedlineType_t eType = pRedl->GetType();
1834cdf0e10cSrcweir             if( nEndOfExtra < nNd &&
1835cdf0e10cSrcweir                 ( nsRedlineType_t::REDLINE_INSERT == eType || nsRedlineType_t::REDLINE_DELETE == eType ))
1836cdf0e10cSrcweir             {
1837cdf0e10cSrcweir                 const SwNode* pDstNd = GetNodes()[
1838cdf0e10cSrcweir                                         nMyEndOfExtra + nNd - nEndOfExtra ];
1839cdf0e10cSrcweir 
1840cdf0e10cSrcweir                 // Position gefunden. Dann muss im DestDoc auch
1841cdf0e10cSrcweir                 // in der Line das Redline eingefuegt werden
1842cdf0e10cSrcweir                 _SaveMergeRedlines* pTmp = new _SaveMergeRedlines(
1843cdf0e10cSrcweir                                                     *pDstNd, *pRedl, pRing );
1844cdf0e10cSrcweir                 if( !pRing )
1845cdf0e10cSrcweir                     pRing = pTmp;
1846cdf0e10cSrcweir             }
1847cdf0e10cSrcweir         }
1848cdf0e10cSrcweir 
1849cdf0e10cSrcweir         if( pRing )
1850cdf0e10cSrcweir         {
1851cdf0e10cSrcweir             // dann alle ins DestDoc ueber nehmen
1852cdf0e10cSrcweir           rSrcDoc.SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_SHOW_INSERT | nsRedlineMode_t::REDLINE_SHOW_DELETE));
1853cdf0e10cSrcweir 
1854cdf0e10cSrcweir           SetRedlineMode((RedlineMode_t)(
1855cdf0e10cSrcweir                                       nsRedlineMode_t::REDLINE_ON |
1856cdf0e10cSrcweir                                       nsRedlineMode_t::REDLINE_SHOW_INSERT |
1857cdf0e10cSrcweir                                       nsRedlineMode_t::REDLINE_SHOW_DELETE));
1858cdf0e10cSrcweir 
1859cdf0e10cSrcweir             _SaveMergeRedlines* pTmp = pRing;
1860cdf0e10cSrcweir 
1861cdf0e10cSrcweir             do {
1862cdf0e10cSrcweir                 nRet += pTmp->InsertRedline();
1863cdf0e10cSrcweir             } while( pRing != ( pTmp = (_SaveMergeRedlines*)pTmp->GetNext() ));
1864cdf0e10cSrcweir 
1865cdf0e10cSrcweir             while( pRing != pRing->GetNext() )
1866cdf0e10cSrcweir                 delete pRing->GetNext();
1867cdf0e10cSrcweir             delete pRing;
1868cdf0e10cSrcweir         }
1869cdf0e10cSrcweir     }
1870cdf0e10cSrcweir 
1871cdf0e10cSrcweir     rSrcDoc.SetRedlineMode( eSrcRedlMode );
1872cdf0e10cSrcweir     if( !bSrcModified )
1873cdf0e10cSrcweir         rSrcDoc.ResetModified();
1874cdf0e10cSrcweir 
1875cdf0e10cSrcweir     SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_SHOW_INSERT | nsRedlineMode_t::REDLINE_SHOW_DELETE));
1876cdf0e10cSrcweir 
1877cdf0e10cSrcweir     GetIDocumentUndoRedo().EndUndo(UNDO_EMPTY, NULL);
1878cdf0e10cSrcweir 
1879cdf0e10cSrcweir     return nRet;
1880cdf0e10cSrcweir }
1881