walk.c 963 B

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