parsehist.c

来自「这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易」· C语言 代码 · 共 131 行

C
131
字号
/* * 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;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?