org_text.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * @file org_text.c
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include "print.h"
  9. #include "doc_elt.h"
  10. #include "doc_elt_ops.h"
  11. #include "doc_elt_util.h"
  12. #include "org_text.h"
  13. /* Forward Declarations */
  14. struct org_text_data;
  15. typedef struct org_text_data org_text_data;
  16. static doc_elt_ops_print org_text_print_op;
  17. static doc_elt_ops_isrelated org_text_isrelated_op;
  18. static doc_elt_ops_compare org_text_compare_op;
  19. static doc_elt_ops_merge org_text_merge_op;
  20. static doc_elt_ops_isupdated org_text_isupdated_op;
  21. static doc_elt_ops_note_delete org_text_note_delete;
  22. static doc_elt_ops_note_insert org_text_note_insert;
  23. static doc_elt_ops_get_key org_text_get_key;
  24. /* Declaration of org_element operations table */
  25. doc_elt_ops org_text_ops = {
  26. /* printing */
  27. .print = &org_text_print_op,
  28. /* comparing */
  29. .isrelated = &org_text_isrelated_op,
  30. .compare = &org_text_compare_op,
  31. /* merging */
  32. .merge = &org_text_merge_op,
  33. .isupdated = &org_text_isupdated_op,
  34. .note_delete = &org_text_note_delete,
  35. .note_insert = &org_text_note_insert,
  36. /* Global mapping */
  37. .get_key = &org_text_get_key
  38. };
  39. typedef struct org_text
  40. {
  41. doc_elt elt;
  42. org_text_data * data[3];
  43. } org_text;
  44. typedef struct org_text_data
  45. {
  46. substr text;
  47. } org_text_data;
  48. /*
  49. * Constructor, Destructor
  50. */
  51. org_text *
  52. org_text_create_empty (doc_elt_ops *ops)
  53. {
  54. org_text *txt = calloc (1, sizeof (org_text));
  55. doc_elt_set_ops (&(txt->elt), ops);
  56. doc_elt_set_type ((doc_elt *) txt, ORG_TEXT);
  57. return txt;
  58. }
  59. void
  60. org_text_initversion (org_text *text, doc_src src)
  61. {
  62. size_t index = srctoindex(src);
  63. if (text->data[index] == NULL)
  64. {
  65. text->data[index] = calloc (1, sizeof (org_text_data));
  66. }
  67. return;
  68. }
  69. void
  70. org_text_free (org_text *self)
  71. {
  72. int i = 0;
  73. for (i = 0; i < 3; i++)
  74. {
  75. free(self->data[i]);
  76. }
  77. free (self);
  78. return;
  79. }
  80. bool
  81. org_text_containsversion (org_text *text, doc_src src)
  82. {
  83. size_t index = srctoindex(src);
  84. return text->data[index] != NULL;
  85. }
  86. /*
  87. * Modifier functions
  88. */
  89. void
  90. org_text_set_text (org_text *text, char *string, size_t length, doc_src src)
  91. {
  92. assert (text != NULL);
  93. org_text_data *d = text->data[srctoindex(src)];
  94. assert (d != NULL);
  95. if (d != NULL)
  96. {
  97. d->text.length = length;
  98. d->text.string = string;
  99. }
  100. return;
  101. }
  102. char *
  103. org_text_get_text (org_text *text, doc_src src)
  104. {
  105. assert (text != NULL);
  106. char * str = NULL;
  107. org_text_data *d = text->data[srctoindex(src)];
  108. assert (d != NULL);
  109. if (d != NULL)
  110. {
  111. str = d->text.string;
  112. }
  113. return str;
  114. }
  115. size_t
  116. org_text_get_length (org_text *text, doc_src src)
  117. {
  118. assert (text != NULL);
  119. int length = 0;
  120. org_text_data *data = text->data[srctoindex(src)];
  121. assert (data != NULL);
  122. if (data != NULL)
  123. {
  124. length = data->text.length;
  125. }
  126. return length;
  127. }
  128. /*
  129. * Document Element Operations
  130. */
  131. static void
  132. org_text_print_op (doc_ref *ref, print_ctxt *ctxt, doc_stream *out)
  133. {
  134. /* text objects do not move. They are either deleted, added, or
  135. updated. */
  136. debug_msg (DOC, 5, "Begin Printing\n");
  137. doc_elt *elt = doc_ref_get_elt(ref);
  138. org_text *text = (org_text *)elt;
  139. org_text_data *anc_data = text->data[ANC_INDEX];
  140. org_text_data *loc_data = text->data[LOC_INDEX];
  141. org_text_data *rem_data = text->data[REM_INDEX];
  142. /* This is the merge logic */
  143. if (anc_data != NULL)
  144. {
  145. debug_msg (DOC, 4, "Ancestor exists\n");
  146. if (loc_data != NULL)
  147. {
  148. debug_msg (DOC, 4, "Local exists\n");
  149. if (rem_data != NULL)
  150. {
  151. debug_msg (DOC, 4, "Remote exists\n");
  152. if (substreql (loc_data->text, anc_data->text))
  153. {
  154. if (substreql (rem_data->text, anc_data->text))
  155. {
  156. /* Nothing has updated, print ancestor */
  157. substrprint (anc_data->text, out);
  158. }
  159. else
  160. {
  161. /* Remote has updated. Print remote */
  162. substrprint (rem_data->text, out);
  163. }
  164. }
  165. else
  166. {
  167. if (substreql (anc_data->text, rem_data->text))
  168. {
  169. /* Local has updated. Print local. */
  170. substrprint (loc_data->text, out);
  171. }
  172. else
  173. {
  174. debug_msg(DOC, 3, "Update Conflict\n");
  175. /* Both Have Updated, Conflict ! */
  176. line_diff3 (anc_data->text.string, anc_data->text.length,
  177. loc_data->text.string, loc_data->text.length,
  178. rem_data->text.string, rem_data->text.length,
  179. ctxt, out);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. if (substreql (loc_data->text, anc_data->text))
  186. {
  187. /* Deleted in remote. Print nothing. */
  188. }
  189. else
  190. {
  191. /* Updated in local, deleted in remote. Conflict ! */
  192. enter_structural_conflict (ctxt, local_side, "Updated\n", out);
  193. substrprint (loc_data->text, out);
  194. enter_structural_conflict (ctxt, remote_side, NULL, out);
  195. enter_structural_conflict (ctxt, no_conflict, "Deleted\n", out);
  196. }
  197. }
  198. }
  199. else
  200. {
  201. if (rem_data != NULL)
  202. {
  203. if (substreql (rem_data->text, anc_data->text))
  204. {
  205. /* Deleted in local. Print nothing. */
  206. }
  207. else
  208. {
  209. /* Updated in remote, deleted in local */
  210. enter_structural_conflict (ctxt, local_side, "Deleted\n", out);
  211. enter_structural_conflict (ctxt, remote_side, NULL, out);
  212. substrprint (rem_data->text, out);
  213. enter_structural_conflict (ctxt, no_conflict, "Updated\n", out);
  214. }
  215. }
  216. else
  217. {
  218. /* Deleted in both. Print nothing. */
  219. }
  220. }
  221. }
  222. else
  223. {
  224. if (loc_data != NULL)
  225. {
  226. if (rem_data != NULL)
  227. {
  228. if (substreql (loc_data->text, rem_data->text))
  229. {
  230. /* Both have inserted the same text. Print whichever, normally. */
  231. substrprint (loc_data->text, out);
  232. }
  233. else
  234. {
  235. debug_msg(DOC, 3, "Conflict: Local: Inserted, Remote: Inserted\n");
  236. /* Both have inserted different text. Conflict. */
  237. enter_content_conflict (ctxt, local_side, "Inserted\n", out);
  238. substrprint (loc_data->text, out);
  239. enter_content_conflict (ctxt, remote_side, NULL, out);
  240. substrprint (rem_data->text, out);
  241. enter_content_conflict (ctxt, no_conflict, "Inserted\n", out);
  242. }
  243. }
  244. else
  245. {
  246. /* Local insert */
  247. substrprint (loc_data->text, out);
  248. }
  249. }
  250. else
  251. {
  252. if (rem_data != NULL)
  253. {
  254. /* Remote insert */
  255. substrprint (rem_data->text, out);
  256. }
  257. else
  258. {
  259. /* Never existed. */
  260. }
  261. }
  262. }
  263. debug_msg (DOC, 5, "Done Printing\n");
  264. return;
  265. }
  266. static bool
  267. org_text_isrelated_op (doc_ref *a_ref, doc_ref *b_ref, merge_ctxt *ctxt)
  268. {
  269. /* Two text elements are related if they represent different
  270. * versions of the same element. Text objects are always related to
  271. * each other.
  272. */
  273. bool isrelated = false;
  274. doc_elt *elt_a = doc_ref_get_elt (a_ref);
  275. doc_elt *elt_b = doc_ref_get_elt (b_ref);
  276. if ((doc_elt_get_type (elt_a) == ORG_TEXT) &&
  277. (doc_elt_get_type (elt_b) == ORG_TEXT))
  278. {
  279. isrelated = true;
  280. }
  281. return isrelated;
  282. }
  283. static int
  284. org_text_compare_op (doc_elt *a, doc_src a_src, doc_elt *b, doc_src b_src)
  285. {
  286. /**
  287. * @todo Implement org_text_compare_op.
  288. */
  289. return 0;
  290. }
  291. static void
  292. org_text_merge_op (doc_ref *a_ref, doc_ref *b_ref, merge_ctxt *ctxt)
  293. {
  294. /**
  295. * @todo Ensure both elt's are org_text when merging.
  296. */
  297. debug_msg (DOC_ELT, 5, "Merging org_text\n");
  298. org_text *a_text = (org_text *) doc_ref_get_elt (a_ref);
  299. org_text *b_text = (org_text *) doc_ref_get_elt (b_ref);
  300. assert (a_text != NULL);
  301. assert (b_text != NULL);
  302. /* Merge data from b into a */
  303. int i = 0;
  304. for (i = 0; i < 3; i++)
  305. {
  306. if (a_text->data[i] == NULL)
  307. a_text->data[i] = b_text->data[i];
  308. }
  309. /* There is no other data to merge, so return; */
  310. return;
  311. }
  312. static bool
  313. org_text_isupdated_op (doc_ref *ref)
  314. {
  315. /* Return true if either local or remote texts have changed from the
  316. ancestor. Do not return true if the ancestor was deleted. */
  317. doc_elt *elt = doc_ref_get_elt(ref);
  318. org_text *text = (org_text *)elt;
  319. org_text_data *anc_data = text->data[ANC_INDEX];
  320. org_text_data *loc_data = text->data[LOC_INDEX];
  321. org_text_data *rem_data = text->data[REM_INDEX];
  322. bool isupdated = false;
  323. bool loc_isupdated = false;
  324. bool rem_isupdated = false;
  325. if (anc_data != NULL)
  326. {
  327. if (loc_data != NULL)
  328. {
  329. loc_isupdated = !substreql(anc_data->text, loc_data->text);
  330. }
  331. if (rem_data != NULL)
  332. {
  333. rem_isupdated = !substreql(anc_data->text, rem_data->text);
  334. }
  335. isupdated = (loc_isupdated || rem_isupdated);
  336. }
  337. else
  338. {
  339. /* if the node was inserted, ie. one new node */
  340. if ((loc_data != NULL) || (rem_data != NULL))
  341. {
  342. isupdated = true;
  343. }
  344. }
  345. return isupdated;
  346. }
  347. static void
  348. org_text_note_delete (doc_ref *ref, merge_ctxt *ctxt)
  349. {
  350. /* org_text does not perform any fallback matching technique.
  351. Do nothing. */
  352. return;
  353. }
  354. static void
  355. org_text_note_insert (doc_ref *ref, merge_ctxt *ctxt)
  356. {
  357. /* org_text does not have global matching, do nothing */
  358. return;
  359. }
  360. static doc_key *
  361. org_text_get_key (doc_elt * elt)
  362. {
  363. /* org_text does not have global matching, do nothing */
  364. /* this function should never be called? */
  365. abort ();
  366. return NULL;
  367. }