summaryrefslogtreecommitdiff
path: root/sys/src/cmd/wikifs/parsehist.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/wikifs/parsehist.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/wikifs/parsehist.c')
-rwxr-xr-xsys/src/cmd/wikifs/parsehist.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/sys/src/cmd/wikifs/parsehist.c b/sys/src/cmd/wikifs/parsehist.c
new file mode 100755
index 000000000..88b49dd7e
--- /dev/null
+++ b/sys/src/cmd/wikifs/parsehist.c
@@ -0,0 +1,130 @@
+/*
+ * Read a Wiki history file.
+ * It's a title line then a sequence of Wiki files separated by headers.
+ *
+ * Ddate/time
+ * #body
+ * #...
+ * #...
+ * #...
+ * etc.
+ */
+
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <String.h>
+#include <thread.h>
+#include "wiki.h"
+
+static char*
+Brdwline(void *vb, int sep)
+{
+ Biobufhdr *b;
+ char *p;
+
+ b = vb;
+ if(Bgetc(b) == '#'){
+ if(p = Brdline(b, sep))
+ p[Blinelen(b)-1] = '\0';
+ return p;
+ }else{
+ Bungetc(b);
+ return nil;
+ }
+}
+
+Whist*
+Brdwhist(Biobuf *b)
+{
+ int i, current, conflict, c, n;
+ char *author, *comment, *p, *title;
+ ulong t;
+ Wdoc *w;
+ Whist *h;
+
+ if((p = Brdline(b, '\n')) == nil){
+ werrstr("short read: %r");
+ return nil;
+ }
+
+ p[Blinelen(b)-1] = '\0';
+ p = strcondense(p, 1);
+ title = estrdup(p);
+
+ w = nil;
+ n = 0;
+ t = -1;
+ author = nil;
+ comment = nil;
+ conflict = 0;
+ current = 0;
+ while((c = Bgetc(b)) != Beof){
+ if(c != '#'){
+ p = Brdline(b, '\n');
+ if(p == nil)
+ break;
+ p[Blinelen(b)-1] = '\0';
+
+ switch(c){
+ case 'D':
+ t = strtoul(p, 0, 10);
+ break;
+ case 'A':
+ free(author);
+ author = estrdup(p);
+ break;
+ case 'C':
+ free(comment);
+ comment = estrdup(p);
+ break;
+ case 'X':
+ conflict = 1;
+ }
+ } else { /* c=='#' */
+ Bungetc(b);
+ if(n%8 == 0)
+ w = erealloc(w, (n+8)*sizeof(w[0]));
+ w[n].time = t;
+ w[n].author = author;
+ w[n].comment = comment;
+ comment = nil;
+ author = nil;
+ w[n].wtxt = Brdpage(Brdwline, b);
+ w[n].conflict = conflict;
+ if(w[n].wtxt == nil)
+ goto Error;
+ if(!conflict)
+ current = n;
+ n++;
+ conflict = 0;
+ t = -1;
+ }
+ }
+ if(w==nil)
+ goto Error;
+
+ free(comment);
+ free(author);
+ h = emalloc(sizeof *h);
+ h->title = title;
+ h->doc = w;
+ h->ndoc = n;
+ h->current = current;
+ incref(h);
+ setmalloctag(h, getcallerpc(&b));
+ return h;
+
+Error:
+ free(title);
+ free(author);
+ free(comment);
+ for(i=0; i<n; i++){
+ free(w[i].author);
+ free(w[i].comment);
+ freepage(w[i].wtxt);
+ }
+ free(w);
+ return nil;
+}
+