parsehist.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Read a Wiki history file.
  3. * It's a title line then a sequence of Wiki files separated by headers.
  4. *
  5. * Ddate/time
  6. * #body
  7. * #...
  8. * #...
  9. * #...
  10. * etc.
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <String.h>
  16. #include <thread.h>
  17. #include "wiki.h"
  18. static char*
  19. Brdwline(void *vb, int sep)
  20. {
  21. Biobufhdr *b;
  22. char *p;
  23. b = vb;
  24. if(Bgetc(b) == '#'){
  25. if(p = Brdline(b, sep))
  26. p[Blinelen(b)-1] = '\0';
  27. return p;
  28. }else{
  29. Bungetc(b);
  30. return nil;
  31. }
  32. }
  33. Whist*
  34. Brdwhist(Biobuf *b)
  35. {
  36. int i, current, conflict, c, n;
  37. char *author, *comment, *p, *title;
  38. ulong t;
  39. Wdoc *w;
  40. Whist *h;
  41. if((p = Brdline(b, '\n')) == nil){
  42. werrstr("short read: %r");
  43. return nil;
  44. }
  45. p[Blinelen(b)-1] = '\0';
  46. p = strcondense(p, 1);
  47. title = estrdup(p);
  48. w = nil;
  49. n = 0;
  50. t = -1;
  51. author = nil;
  52. comment = nil;
  53. conflict = 0;
  54. current = 0;
  55. while((c = Bgetc(b)) != Beof){
  56. if(c != '#'){
  57. p = Brdline(b, '\n');
  58. if(p == nil)
  59. break;
  60. p[Blinelen(b)-1] = '\0';
  61. switch(c){
  62. case 'D':
  63. t = strtoul(p, 0, 10);
  64. break;
  65. case 'A':
  66. free(author);
  67. author = estrdup(p);
  68. break;
  69. case 'C':
  70. free(comment);
  71. comment = estrdup(p);
  72. break;
  73. case 'X':
  74. conflict = 1;
  75. }
  76. } else { /* c=='#' */
  77. Bungetc(b);
  78. if(n%8 == 0)
  79. w = erealloc(w, (n+8)*sizeof(w[0]));
  80. w[n].time = t;
  81. w[n].author = author;
  82. w[n].comment = comment;
  83. comment = nil;
  84. author = nil;
  85. w[n].wtxt = Brdpage(Brdwline, b);
  86. w[n].conflict = conflict;
  87. if(w[n].wtxt == nil)
  88. goto Error;
  89. if(!conflict)
  90. current = n;
  91. n++;
  92. conflict = 0;
  93. t = -1;
  94. }
  95. }
  96. if(w==nil)
  97. goto Error;
  98. free(comment);
  99. free(author);
  100. h = emalloc(sizeof *h);
  101. h->title = title;
  102. h->doc = w;
  103. h->ndoc = n;
  104. h->current = current;
  105. incref(h);
  106. setmalloctag(h, getcallerpc(&b));
  107. return h;
  108. Error:
  109. free(title);
  110. free(author);
  111. free(comment);
  112. for(i=0; i<n; i++){
  113. free(w[i].author);
  114. free(w[i].comment);
  115. freepage(w[i].wtxt);
  116. }
  117. free(w);
  118. return nil;
  119. }