ndbgetval.c 811 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "ndb.h"
  5. /*
  6. * search for a tuple that has the given 'attr=val' and also 'rattr=x'.
  7. * copy 'x' into 'buf' and return the whole tuple.
  8. *
  9. * return 0 if not found.
  10. */
  11. Ndbtuple*
  12. ndbgetval(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, char *buf)
  13. {
  14. Ndbtuple *t, *nt;
  15. t = ndbsearch(db, s, attr, val);
  16. while(t){
  17. /* first look on same line (closer binding) */
  18. nt = s->t;
  19. for(;;){
  20. if(strcmp(rattr, nt->attr) == 0){
  21. strncpy(buf, nt->val, Ndbvlen);
  22. return t;
  23. }
  24. nt = nt->line;
  25. if(nt == s->t)
  26. break;
  27. }
  28. /* search whole tuple */
  29. for(nt = t; nt; nt = nt->entry)
  30. if(strcmp(rattr, nt->attr) == 0){
  31. strncpy(buf, nt->val, Ndbvlen);
  32. return t;
  33. }
  34. ndbfree(t);
  35. t = ndbsnext(s, attr, val);
  36. }
  37. return 0;
  38. }