Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / demo / CoEd / libCoEd / CoEdChangeHistory.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: CoEdChangeHistory.C /main/3 1995/10/20 17:06:11 rswiston $                                                  
28 /*
29  * CoEdChangeHistory.cc
30  *
31  * Copyright (c) 1991 by Sun Microsystems.  All Rights Reserved.
32  *
33  * Permission to use, copy, modify, distribute, and sell this software
34  * and its documentation for any purpose is hereby granted without
35  * fee, provided that the above copyright notice appear in all copies
36  * and that both that copyright notice and this permission notice
37  * appear in supporting documentation, and that the names of Sun
38  * Microsystems and its subsidiaries not be used in advertising or
39  * publicity pertaining to distribution of the software without
40  * specific, written prior permission.  Sun Microsystems and its
41  * subsidiaries make no representations about the suitability of this
42  * software for any purpose.  It is provided "as is" without express
43  * or implied warranty.
44  *
45  * Sun Microsystems and its subsidiaries disclaim all warranties with
46  * regard to this software, including all implied warranties of
47  * merchantability and fitness.  In no event shall Sun Microsystems or
48  * its subsidiaries be liable for any special, indirect or
49  * consequential damages or any damages whatsoever resulting from loss
50  * of use, data or profits, whether in an action of contract,
51  * negligence or other tortious action, arising out of or in
52  * connection with the use or performance of this software.
53  */
54
55 #include <string.h>
56 #include "CoEdChangeHistory.h"
57
58 CoEdChangeHistory::
59 CoEdChangeHistory() : CoEdTextChangeList()
60 {
61 }
62
63 void CoEdChangeHistory::
64 insert( CoEdTextChange *change )
65 {
66         CoEdTextChange *curr = _tail;
67         //
68         // Find the most recent change in the history that the
69         // incoming change knows of.
70         //
71         while (curr != 0) {
72                 if (change->knowsOf( *curr )) {
73                         break;
74                 }
75                 curr = curr->_prev;
76         }
77         //
78         // Bump curr to point to the first change that the incoming
79         // change doesn't know of.
80         //
81         if (curr == 0) {
82                 curr = _head;
83         } else {
84                 curr = curr->_next;
85         }
86         //
87         // The rest of the changes are mutually ignorant with the incoming
88         // change.  Find the first one with a greater site id, and
89         // stick this change in front of it.
90         //
91         while (curr != 0) {
92                 if (*change->_causer < *curr->_causer) {
93                         break;
94                 }
95                 curr = curr->_next;
96         }
97         if (curr == 0) {
98                 append( change );
99         } else {
100                 insertBefore( change, curr );
101         }
102 }
103
104 CoEdTextChange *CoEdChangeHistory::
105 translate( CoEdTextChange &change )
106 {
107         _translateOverEarlierChgs( change );
108         return _translateOverLaterChgs( change );
109 }
110
111 //
112 // Modify <change> so that it takes into account any changes ahead of
113 // it in the change history that it does not know about.
114 //
115 void CoEdChangeHistory::
116 _translateOverEarlierChgs( CoEdTextChange &change )
117 {
118         CoEdTextChange *curr = _head;
119         while (curr != &change) {
120                 if (! change.knowsOf( *curr )) {
121                         change.translateOver( *curr );
122                 }
123                 curr = curr->_next;
124         }
125 }
126
127 //
128 // Take <change>, which is assumed to have been inserted into this
129 // ChangeHistory, and adjust the remaining changes in the history
130 // so that they take into account the change inserted ahead of them.
131 // Also, return a new CoEdTextChange that is a translated version
132 // of <change>, suitable for application to a textbuffer that
133 // has already had the remaining changes in the history applied to it.
134 //
135 CoEdTextChange *CoEdChangeHistory::
136 _translateOverLaterChgs( const CoEdTextChange &change )
137 {
138         CoEdTextChange *xlatdChng = new CoEdTextChange( change );
139         if (xlatdChng == 0) {
140                 return 0;
141         }
142
143         CoEdTextChange *curr = change._next;
144         while (curr != 0) {
145                 curr->interTranslate( *xlatdChng );
146                 curr = curr->_next;
147         }
148         return xlatdChng;
149 }
150