ndbgetval.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, char *buf, int len)
  13. {
  14. Ndbtuple *t, *nt;
  15. werrstr("");
  16. t = ndbsearch(db, s, attr, val);
  17. while(t){
  18. /* first look on same line (closer binding) */
  19. nt = s->t;
  20. for(;;){
  21. if(strcmp(rattr, nt->attr) == 0){
  22. strncpy(buf, nt->val, len);
  23. buf[len-1] = 0;
  24. if(strlen(nt->val) >= len)
  25. werrstr("return value truncated");
  26. return t;
  27. }
  28. nt = nt->line;
  29. if(nt == s->t)
  30. break;
  31. }
  32. /* search whole tuple */
  33. for(nt = t; nt; nt = nt->entry)
  34. if(strcmp(rattr, nt->attr) == 0){
  35. strncpy(buf, nt->val, len);
  36. buf[len-1] = 0;
  37. if(strlen(nt->val) >= len)
  38. werrstr("return value truncated");
  39. return t;
  40. }
  41. ndbfree(t);
  42. t = ndbsnext(s, attr, val);
  43. }
  44. return 0;
  45. }
  46. Ndbtuple*
  47. ndbgetval(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, char *buf)
  48. {
  49. return ndbgetvalue(db, s, attr, val, rattr, buf, Ndbvlen);
  50. }