xref: /plan9/sys/src/cmd/fossil/walk.c (revision 5e96a66c77eb9140492ca53f857cbbf108e128ed)
1 /*
2  * Generic traversal routines.
3  */
4 
5 #include "stdinc.h"
6 #include "dat.h"
7 #include "fns.h"
8 
9 static uint
etype(Entry * e)10 etype(Entry *e)
11 {
12 	uint t;
13 
14 	if(e->flags&VtEntryDir)
15 		t = BtDir;
16 	else
17 		t = BtData;
18 	return t+e->depth;
19 }
20 
21 void
initWalk(WalkPtr * w,Block * b,uint size)22 initWalk(WalkPtr *w, Block *b, uint size)
23 {
24 	memset(w, 0, sizeof *w);
25 	switch(b->l.type){
26 	case BtData:
27 		return;
28 
29 	case BtDir:
30 		w->data = b->data;
31 		w->m = size / VtEntrySize;
32 		w->isEntry = 1;
33 		return;
34 
35 	default:
36 		w->data = b->data;
37 		w->m = size / VtScoreSize;
38 		w->type = b->l.type;
39 		w->tag = b->l.tag;
40 		return;
41 	}
42 }
43 
44 int
nextWalk(WalkPtr * w,uchar score[VtScoreSize],uchar * type,u32int * tag,Entry ** e)45 nextWalk(WalkPtr *w, uchar score[VtScoreSize], uchar *type, u32int *tag, Entry **e)
46 {
47 	if(w->n >= w->m)
48 		return 0;
49 
50 	if(w->isEntry){
51 		*e = &w->e;
52 		entryUnpack(&w->e, w->data, w->n);
53 		memmove(score, w->e.score, VtScoreSize);
54 		*type = etype(&w->e);
55 		*tag = w->e.tag;
56 	}else{
57 		*e = nil;
58 		memmove(score, w->data+w->n*VtScoreSize, VtScoreSize);
59 		*type = w->type-1;
60 		*tag = w->tag;
61 	}
62 	w->n++;
63 	return 1;
64 }
65 
66