walk.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Generic traversal routines.
  11. */
  12. #include "stdinc.h"
  13. #include "dat.h"
  14. #include "fns.h"
  15. static uint
  16. etype(Entry *e)
  17. {
  18. uint t;
  19. if(e->flags&VtEntryDir)
  20. t = BtDir;
  21. else
  22. t = BtData;
  23. return t+e->depth;
  24. }
  25. void
  26. initWalk(WalkPtr *w, Block *b, uint size)
  27. {
  28. memset(w, 0, sizeof *w);
  29. switch(b->l.type){
  30. case BtData:
  31. return;
  32. case BtDir:
  33. w->data = b->data;
  34. w->m = size / VtEntrySize;
  35. w->isEntry = 1;
  36. return;
  37. default:
  38. w->data = b->data;
  39. w->m = size / VtScoreSize;
  40. w->type = b->l.type;
  41. w->tag = b->l.tag;
  42. return;
  43. }
  44. }
  45. int
  46. nextWalk(WalkPtr *w, uint8_t score[VtScoreSize], uint8_t *type, uint32_t *tag,
  47. Entry **e)
  48. {
  49. if(w->n >= w->m)
  50. return 0;
  51. if(w->isEntry){
  52. *e = &w->e;
  53. entryUnpack(&w->e, w->data, w->n);
  54. memmove(score, w->e.score, VtScoreSize);
  55. *type = etype(&w->e);
  56. *tag = w->e.tag;
  57. }else{
  58. *e = nil;
  59. memmove(score, w->data+w->n*VtScoreSize, VtScoreSize);
  60. *type = w->type-1;
  61. *tag = w->tag;
  62. }
  63. w->n++;
  64. return 1;
  65. }