CoEdTextChange.C 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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: CoEdTextChange.C /main/3 1995/10/20 17:07:30 rswiston $
  28. /*
  29. * CoEdTextChange.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 <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <limits.h>
  58. #include "CoEdTextVersion.h"
  59. #include "CoEdTextChange.h"
  60. #undef DEBUG
  61. CoEdTextChange::
  62. CoEdTextChange()
  63. {
  64. _start = 0;
  65. _end = 0;
  66. _text = 0;
  67. _appliesTo = 0;
  68. _causer = 0;
  69. _changeNum = 0;
  70. _charsAdded = 0;
  71. _next = 0;
  72. _prev = 0;
  73. }
  74. CoEdTextChange::
  75. CoEdTextChange( long start, long end, const char *text,
  76. const CoEdTextVersion *appliesTo, const CoEdSiteID *causer,
  77. int changeNum )
  78. {
  79. _next = 0;
  80. _prev = 0;
  81. _start = start;
  82. _end = end;
  83. if (text != 0) {
  84. _text = strdup( text );
  85. } else {
  86. _text = 0;
  87. }
  88. _charsAdded = ((_text == 0) ? 0 : strlen( _text ))
  89. - (_end - _start);
  90. _appliesTo = appliesTo->copy();
  91. _causer = causer->copy();
  92. _changeNum = changeNum;
  93. }
  94. CoEdTextChange::
  95. CoEdTextChange( const CoEdTextChange &change )
  96. {
  97. _next = 0;
  98. _prev = 0;
  99. _start = change._start;
  100. _end = change._end;
  101. if (change._text != 0) {
  102. _text = strdup( change._text );
  103. } else {
  104. _text = 0;
  105. }
  106. _charsAdded = change._charsAdded;
  107. _appliesTo = change._appliesTo->copy();
  108. _causer = change._causer->copy();
  109. _changeNum = change._changeNum;
  110. }
  111. CoEdTextChange::
  112. CoEdTextChange( Tt_message msg, CoEdStatus &status )
  113. {
  114. unsigned char *data;
  115. int len;
  116. _next = 0;
  117. _prev = 0;
  118. _charsAdded = 0;
  119. //
  120. // Extract arg 0: CoEdTextVersion
  121. //
  122. Tt_status ttErr = tt_message_arg_bval( msg, 0, &data, &len );
  123. if (ttErr != TT_OK) {
  124. fprintf( stderr, "libCoEd: tt_message_arg_bval(): %s\n",
  125. tt_status_message( ttErr ));
  126. status = (CoEdStatus)ttErr;
  127. return;
  128. }
  129. _appliesTo = new CoEdTextVersion( data, len, status );
  130. if (status != CoEdOK) {
  131. return;
  132. }
  133. //
  134. // Extract arg 1: changeNum
  135. //
  136. int argVal;
  137. ttErr = tt_message_arg_ival( msg, 1, &argVal );
  138. if (ttErr != TT_OK) {
  139. fprintf( stderr, "libCoEd: tt_message_arg_ival(): %s\n",
  140. tt_status_message( ttErr ));
  141. status = (CoEdStatus)ttErr;
  142. return;
  143. }
  144. _changeNum = argVal;
  145. //
  146. // Extract arg 2: start
  147. //
  148. ttErr = tt_message_arg_ival( msg, 2, &argVal );
  149. if (ttErr != TT_OK) {
  150. fprintf( stderr, "libCoEd: tt_message_arg_ival(): %s\n",
  151. tt_status_message( ttErr ));
  152. status = (CoEdStatus)ttErr;
  153. return;
  154. }
  155. _start = argVal;
  156. //
  157. // Extract arg 3: end
  158. //
  159. ttErr = tt_message_arg_ival( msg, 3, &argVal );
  160. if (ttErr != TT_OK) {
  161. fprintf( stderr, "libCoEd: tt_message_arg_ival(): %s\n",
  162. tt_status_message( ttErr ));
  163. status = (CoEdStatus)ttErr;
  164. return;
  165. }
  166. _end = argVal;
  167. //
  168. // Extract arg 4: text
  169. //
  170. char *temp = tt_message_arg_val( msg, 4 );
  171. ttErr = tt_ptr_error( temp );
  172. if (ttErr != TT_OK) {
  173. fprintf( stderr, "libCoEd: tt_message_arg_val(): %s\n",
  174. tt_status_message( ttErr ));
  175. status = (CoEdStatus)ttErr;
  176. return;
  177. }
  178. if (temp == 0) {
  179. _text = 0;
  180. } else {
  181. _text = strdup( temp );
  182. if (_text == 0) {
  183. fprintf( stderr, "libCoEd: ran out of memory!\n" );
  184. status = CoEdErrNoMem;
  185. return;
  186. }
  187. tt_free( temp );
  188. }
  189. _charsAdded = _end - _start
  190. + ((_text == 0) ? 0 : strlen( _text ));
  191. //
  192. // Extract who caused the change.
  193. //
  194. temp = tt_message_sender( msg );
  195. ttErr = tt_ptr_error( temp );
  196. if (ttErr != TT_OK) {
  197. fprintf( stderr, "libCoEd: tt_message_sender(): %s\n",
  198. tt_status_message( ttErr ));
  199. status = (CoEdStatus)ttErr;
  200. return;
  201. }
  202. _causer = new CoEdSiteID( temp );
  203. if (_causer == 0) {
  204. fprintf( stderr, "libCoEd: ran out of memory!\n" );
  205. status = CoEdErrNoMem;
  206. return;
  207. }
  208. tt_free( temp );
  209. status = CoEdOK;
  210. }
  211. CoEdTextChange::
  212. ~CoEdTextChange()
  213. {
  214. if (_text != 0) {
  215. free( (char *)_text );
  216. }
  217. if (_appliesTo != 0) {
  218. delete _appliesTo;
  219. }
  220. if (_causer != 0) {
  221. delete _causer;
  222. }
  223. }
  224. CoEdStatus CoEdTextChange::
  225. broadcast( const char *path ) const
  226. {
  227. //
  228. // Create the message.
  229. //
  230. Tt_message msg = tt_pnotice_create( TT_FILE, "Text_File_Changed" );
  231. Tt_status ttErr = tt_ptr_error( msg );
  232. if (ttErr != TT_OK) {
  233. fprintf( stderr, "libCoEd: tt_pnotice_create(): %s\n",
  234. tt_status_message( ttErr ));
  235. return (CoEdStatus)ttErr;
  236. }
  237. //
  238. // Set the file of the message.
  239. //
  240. ttErr = tt_message_file_set( msg, path );
  241. if (ttErr != TT_OK) {
  242. fprintf( stderr, "libCoEd: tt_message_file_set(): %s\n",
  243. tt_status_message( ttErr ));
  244. return (CoEdStatus)ttErr;
  245. }
  246. //
  247. // Add arg 0: CoEdTextVersion
  248. //
  249. unsigned long len;
  250. unsigned char *data;
  251. CoEdStatus err = _appliesTo->serialize( &data, &len );
  252. if (err != CoEdOK) {
  253. fprintf( stderr, "libCoEd: CoEdTextVersion::serialize(): %d\n",
  254. (int)err );
  255. return err;
  256. }
  257. ttErr = tt_message_barg_add( msg, TT_IN, "CoEdTextVersion", data,
  258. (int)len );
  259. if (ttErr != TT_OK) {
  260. fprintf( stderr, "libCoEd: tt_message_barg_add(): %s\n",
  261. tt_status_message( ttErr ));
  262. return (CoEdStatus)ttErr;
  263. }
  264. //
  265. // Add arg 1: changeNum
  266. //
  267. ttErr = tt_message_iarg_add( msg, TT_IN, "int", (int)_changeNum );
  268. if (ttErr != TT_OK) {
  269. fprintf( stderr, "libCoEd: tt_message_iarg_add(): %s\n",
  270. tt_status_message( ttErr ));
  271. return (CoEdStatus)ttErr;
  272. }
  273. //
  274. // Add arg 2: start
  275. //
  276. ttErr = tt_message_iarg_add( msg, TT_IN, "int", (int)_start );
  277. if (ttErr != TT_OK) {
  278. fprintf( stderr, "libCoEd: tt_message_iarg_add(): %s\n",
  279. tt_status_message( ttErr ));
  280. return (CoEdStatus)ttErr;
  281. }
  282. //
  283. // Add arg 3: end
  284. //
  285. ttErr = tt_message_iarg_add( msg, TT_IN, "int", (int)_end );
  286. if (ttErr != TT_OK) {
  287. fprintf( stderr, "libCoEd: tt_message_iarg_add(): %s\n",
  288. tt_status_message( ttErr ));
  289. return (CoEdStatus)ttErr;
  290. }
  291. //
  292. // Add arg 4: text
  293. //
  294. if (_text == 0) {
  295. ttErr = tt_message_arg_add( msg, TT_IN, "string", "" );
  296. } else {
  297. ttErr = tt_message_arg_add( msg, TT_IN, "string", _text );
  298. }
  299. if (ttErr != TT_OK) {
  300. fprintf( stderr, "libCoEd: tt_message_arg_add(): %s\n",
  301. tt_status_message( ttErr ));
  302. return (CoEdStatus)ttErr;
  303. }
  304. //
  305. // Send the message.
  306. //
  307. #ifdef DEBUG
  308. static Tt_message oldMsg = 0;
  309. if (oldMsg == 0) {
  310. oldMsg = msg;
  311. return CoEdOK;
  312. } else {
  313. ttErr = tt_message_send( oldMsg );
  314. if (ttErr != TT_OK) {
  315. fprintf( stderr, "libCoEd: tt_message_send(): %s\n",
  316. tt_status_message( ttErr ));
  317. return (CoEdStatus)ttErr;
  318. }
  319. ttErr = tt_message_destroy( oldMsg );
  320. if (ttErr != TT_OK) {
  321. fprintf( stderr, "libCoEd: tt_message_destroy(): %s\n",
  322. tt_status_message( ttErr ));
  323. return (CoEdStatus)ttErr;
  324. }
  325. oldMsg = 0;
  326. }
  327. #endif DEBUG
  328. ttErr = tt_message_send( msg );
  329. if (ttErr != TT_OK) {
  330. fprintf( stderr, "libCoEd: tt_message_send(): %s\n",
  331. tt_status_message( ttErr ));
  332. return (CoEdStatus)ttErr;
  333. }
  334. ttErr = tt_message_destroy( msg );
  335. if (ttErr != TT_OK) {
  336. fprintf( stderr, "libCoEd: tt_message_destroy(): %s\n",
  337. tt_status_message( ttErr ));
  338. return (CoEdStatus)ttErr;
  339. }
  340. return CoEdOK;
  341. }
  342. //
  343. // This method serves two purposes. First, it takes chng2Xlate,
  344. // and adjusts it forward in time over this change, so that we
  345. // will be able to apply the modified chng2Xlate to our local
  346. // text (to which this change has already been applied).
  347. // Second, since the _un_modified chng2Xlate is being put into the
  348. // history list _before_ this change, we need to modify this
  349. // change so that it makes sense when applied to a text that
  350. // has already had the unmodified chng2Xlate applied to it.
  351. //
  352. void CoEdTextChange::
  353. interTranslate( CoEdTextChange &chng2Xlate )
  354. {
  355. //
  356. // First, adjust chng2Xlate forward in time over this change.
  357. //
  358. int directionToLean = (chng2Xlate._end > _end) ? 1 : 0;
  359. long xlatdStart = adjustPt( chng2Xlate._start, *chng2Xlate._causer,
  360. directionToLean );
  361. directionToLean = (chng2Xlate._start < _start) ? -1 : 0;
  362. long xlatdEnd = adjustPt( chng2Xlate._end, *chng2Xlate._causer,
  363. directionToLean );
  364. //
  365. // Second, adjust this change into a form that makes
  366. // sense if chng2Xlate had already been applied when
  367. // it was time to apply this change.
  368. //
  369. directionToLean = (_end > chng2Xlate._end) ? 1 : 0;
  370. long _startNew = chng2Xlate.adjustPt( _start, *_causer,
  371. directionToLean );
  372. directionToLean = (_start < chng2Xlate._start) ? -1 : 0;
  373. long _endNew = chng2Xlate.adjustPt( _end, *_causer, directionToLean );
  374. //
  375. // Now, modify this change and chng2Xlate, as calculated.
  376. //
  377. _start = _startNew;
  378. _end = _endNew;
  379. //_appliesTo->update( *chng2Xlate._causer, chng2Xlate._changeNum );
  380. chng2Xlate._start = xlatdStart;
  381. chng2Xlate._end = xlatdEnd;
  382. //chng2Xlate._appliesTo->update( *_causer, _changeNum );
  383. }
  384. void CoEdTextChange::
  385. translateOver( const CoEdTextChange &chng2Hurdle )
  386. {
  387. int directionToLean = (_end > chng2Hurdle._end) ? 1 : 0;
  388. long newStart = chng2Hurdle.adjustPt( _start, *_causer,
  389. directionToLean );
  390. directionToLean = (_start < chng2Hurdle._start) ? -1 : 0;
  391. long newEnd = chng2Hurdle.adjustPt( _end, *_causer,
  392. directionToLean );
  393. //_appliesTo->update( *chng2Hurdle._causer, chng2Hurdle._changeNum );
  394. _start = newStart;
  395. _end = newEnd;
  396. }
  397. long CoEdTextChange::
  398. adjustPt( long pt, const CoEdSiteID &ptOwner, int directionToLean ) const
  399. {
  400. if (pt < _start) {
  401. return pt;
  402. }
  403. if (pt > _end) {
  404. return pt + charsAddedBy();
  405. }
  406. //
  407. // pt lies within our change, so it needs to be adjusted to
  408. // one end of our change. First, check if the caller has
  409. // a preference.
  410. //
  411. if (directionToLean < 0) {
  412. return _start;
  413. } else if (directionToLean > 0) {
  414. return _start + charsAddedBy();
  415. } else {
  416. //
  417. // The caller has no preference, so decide by order
  418. // of CoEdSiteID.
  419. //
  420. if (*_causer < ptOwner) {
  421. return _start + charsAddedBy();
  422. } else {
  423. return _start;
  424. }
  425. }
  426. }
  427. void CoEdTextChange::
  428. print( FILE *f )
  429. {
  430. fprintf( f, "[%3d, %3d] %2d: <", _start, _end, _charsAdded );
  431. if (_text == 0) {
  432. fprintf( f, "%6s", ">" );
  433. } else {
  434. fprintf( f, "%-5.5s>", _text );
  435. }
  436. fprintf( f, " #%2d ", _changeNum );
  437. _causer->print( f );
  438. fputs( " ", f );
  439. _appliesTo->print( f );
  440. fputs( "\n", f );
  441. }
  442. CoEdTextChangeList::
  443. CoEdTextChangeList()
  444. {
  445. _head = 0;
  446. _tail = 0;
  447. _count = 0;
  448. }
  449. CoEdTextChangeList::
  450. ~CoEdTextChangeList()
  451. {
  452. CoEdTextChange *curr = _head;
  453. CoEdTextChange *prev;
  454. while (curr != 0) {
  455. prev = curr;
  456. curr = curr->_next;
  457. delete prev;
  458. }
  459. }
  460. void CoEdTextChangeList::
  461. push( CoEdTextChange *change )
  462. {
  463. change->_next = _head;
  464. change->_prev = 0;
  465. if (_tail == 0) {
  466. _tail = change;
  467. } else {
  468. _head->_prev = change;
  469. }
  470. _head = change;
  471. _count++;
  472. }
  473. void CoEdTextChangeList::
  474. append( CoEdTextChange *change )
  475. {
  476. change->_next = 0;
  477. change->_prev = _tail;
  478. if (_head == 0) {
  479. _head = change;
  480. } else {
  481. _tail->_next = change;
  482. }
  483. _tail = change;
  484. _count++;
  485. }
  486. void CoEdTextChangeList::
  487. insertBefore( CoEdTextChange *change1, CoEdTextChange *change2 )
  488. {
  489. if (_head == change2) {
  490. push( change1 );
  491. return;
  492. }
  493. change1->_next = change2;
  494. change1->_prev = change2->_prev;
  495. change2->_prev->_next = change1;
  496. change2->_prev = change1;
  497. _count++;
  498. }
  499. void CoEdTextChangeList::
  500. insertAfter( CoEdTextChange *change2, CoEdTextChange *change1 )
  501. {
  502. if (_tail == change1) {
  503. append( change2 );
  504. return;
  505. }
  506. change2->_next = change1->_next;
  507. change2->_prev = change1;
  508. change1->_next->_prev = change2;
  509. change1->_next = change2;
  510. _count++;
  511. }
  512. CoEdTextChange *CoEdTextChangeList::
  513. remove( CoEdTextChange *change )
  514. {
  515. if (change == _head) {
  516. _head = change->_next;
  517. } else {
  518. change->_prev->_next = change->_next;
  519. }
  520. if (change == _tail) {
  521. _tail = change->_prev;
  522. } else {
  523. change->_next->_prev = change->_prev;
  524. }
  525. change->_next = 0;
  526. change->_prev = 0;
  527. _count--;
  528. return change;
  529. }
  530. void CoEdTextChangeList::
  531. print( FILE *f, char *indent )
  532. {
  533. CoEdTextChange *curr = _head;
  534. while (curr != 0) {
  535. fprintf( f, indent );
  536. curr->print( f );
  537. curr = curr->_next;
  538. }
  539. }