Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / demo / CoEd / libCoEd / CoEdTextVersion.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: CoEdTextVersion.C /main/3 1995/10/23 09:43:59 rswiston $                                                    
28 /*
29  * CoEdTextVersion.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 "CoEdTextVersion.h"
56
57 CoEdTextVersion::
58 CoEdTextVersion() : SiteChangeList()
59 {
60 }
61
62 CoEdTextVersion::
63 CoEdTextVersion( unsigned char *data, int len, CoEdStatus &status )
64         : SiteChangeList( data, len, status )
65 {
66 }
67
68 void CoEdTextVersion::
69 update( const CoEdSiteID &site, int numChanges )
70 {
71         SiteChange *curr = _head;
72         int found = 0;
73         while (curr != 0) {
74                 if ( *(CoEdSiteID *)curr == site ) {
75                         curr->_changeNum = numChanges;
76                         found = 1;
77                 }
78                 curr = (SiteChange *)curr->_next;
79         }
80         if (! found) {
81                 curr = new SiteChange( site, numChanges );
82                 insert( curr );
83         }
84 }
85
86 CoEdTextVersion *CoEdTextVersion::
87 copy() const
88 {
89         CoEdTextVersion *newVersion = new CoEdTextVersion;
90         newVersion->SiteChangeList::copy( (const SiteChangeList *)this );
91         return newVersion;
92 }
93
94 //
95 // This method assumes that the CoEdSiteID of <change> is _not_ already
96 // in the list of SiteChanges!
97 //
98 void CoEdTextVersion::
99 insert( SiteChange *change )
100 {
101         SiteChange *curr = _head;
102         int inserted     = 0;
103         while ((curr != 0) && (! inserted)) {
104                 if (*change < *curr) {
105                         insertBefore( change, curr );
106                         inserted = 1;
107                 }
108                 curr = (SiteChange *)curr->_next;
109         }
110         if (! inserted) {
111                 append( change );
112         }
113 }
114
115 int CoEdTextVersion::
116 knowsOfNewerChangesThan( const CoEdTextVersion &version ) const
117 {
118         SiteChange *curr1 = _head;
119         SiteChange *curr2 = version._head;
120         while ((curr1 != 0) && (curr2 != 0)) {
121                 int wait = 0;
122                 int diff = curr1->cmp( *curr2 );
123                 switch (diff) {
124                     case -2:
125                         // I know of a change from a site that he
126                         // has not even heard from.
127                     case 1:
128                         // I know of a change from this site that
129                         // is later than the one he knows.
130                         return 1;
131                     case -1:
132                         // He knows of a change from this site that
133                         // is later than the one I know. Big deal.
134                         break;
135                     case 2:
136                         // He knows of a change from a site that I
137                         // have not even heard from.  So wait until
138                         // my next site comes up in his list.
139                         wait = 1;
140                         break;
141                     case 0:
142                         break;
143                 }
144                 if (! wait) {
145                         curr1 = (SiteChange *)curr1->_next;
146                 }
147                 curr2 = (SiteChange *)curr2->_next;
148         }
149         if ((curr1 != 0) && (curr2 == 0)) {
150                 //
151                 // I still have a change from a site that he has
152                 // never even heard from.
153                 //
154                 return 1;
155         }
156         return 0;
157 }
158
159 int CoEdTextVersion::
160 knowsOf( const CoEdSiteID &site, int numChanges ) const
161 {
162         SiteChange *curr = _head;
163         while (curr != 0) {
164                 if ( *(CoEdSiteID *)curr == site ) {
165                         return (curr->_changeNum >= numChanges);
166                 }
167                 curr = (SiteChange *)curr->_next;
168         }
169         return 0;
170 }