CoEdChangeHistory.C 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 libraries 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. #include <string.h>
  55. #include "CoEdChangeHistory.h"
  56. CoEdChangeHistory::
  57. CoEdChangeHistory() : CoEdTextChangeList()
  58. {
  59. }
  60. void CoEdChangeHistory::
  61. insert( CoEdTextChange *change )
  62. {
  63. CoEdTextChange *curr = _tail;
  64. //
  65. // Find the most recent change in the history that the
  66. // incoming change knows of.
  67. //
  68. while (curr != 0) {
  69. if (change->knowsOf( *curr )) {
  70. break;
  71. }
  72. curr = curr->_prev;
  73. }
  74. //
  75. // Bump curr to point to the first change that the incoming
  76. // change doesn't know of.
  77. //
  78. if (curr == 0) {
  79. curr = _head;
  80. } else {
  81. curr = curr->_next;
  82. }
  83. //
  84. // The rest of the changes are mutually ignorant with the incoming
  85. // change. Find the first one with a greater site id, and
  86. // stick this change in front of it.
  87. //
  88. while (curr != 0) {
  89. if (*change->_causer < *curr->_causer) {
  90. break;
  91. }
  92. curr = curr->_next;
  93. }
  94. if (curr == 0) {
  95. append( change );
  96. } else {
  97. insertBefore( change, curr );
  98. }
  99. }
  100. CoEdTextChange *CoEdChangeHistory::
  101. translate( CoEdTextChange &change )
  102. {
  103. _translateOverEarlierChgs( change );
  104. return _translateOverLaterChgs( change );
  105. }
  106. //
  107. // Modify <change> so that it takes into account any changes ahead of
  108. // it in the change history that it does not know about.
  109. //
  110. void CoEdChangeHistory::
  111. _translateOverEarlierChgs( CoEdTextChange &change )
  112. {
  113. CoEdTextChange *curr = _head;
  114. while (curr != &change) {
  115. if (! change.knowsOf( *curr )) {
  116. change.translateOver( *curr );
  117. }
  118. curr = curr->_next;
  119. }
  120. }
  121. //
  122. // Take <change>, which is assumed to have been inserted into this
  123. // ChangeHistory, and adjust the remaining changes in the history
  124. // so that they take into account the change inserted ahead of them.
  125. // Also, return a new CoEdTextChange that is a translated version
  126. // of <change>, suitable for application to a textbuffer that
  127. // has already had the remaining changes in the history applied to it.
  128. //
  129. CoEdTextChange *CoEdChangeHistory::
  130. _translateOverLaterChgs( const CoEdTextChange &change )
  131. {
  132. CoEdTextChange *xlatdChng = new CoEdTextChange( change );
  133. if (xlatdChng == 0) {
  134. return 0;
  135. }
  136. CoEdTextChange *curr = change._next;
  137. while (curr != 0) {
  138. curr->interTranslate( *xlatdChng );
  139. curr = curr->_next;
  140. }
  141. return xlatdChng;
  142. }