ndbfree.c 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <ndb.h>
  6. #include "ndbhf.h"
  7. /*
  8. * free a parsed entry
  9. */
  10. void
  11. ndbfree(Ndbtuple *t)
  12. {
  13. Ndbtuple *tn;
  14. for(; t; t = tn){
  15. tn = t->entry;
  16. if(t->val != t->valbuf){
  17. free(t->val);
  18. }
  19. free(t);
  20. }
  21. }
  22. /*
  23. * set a value in a tuple
  24. */
  25. void
  26. ndbsetval(Ndbtuple *t, char *val, int n)
  27. {
  28. if(n < Ndbvlen){
  29. if(t->val != t->valbuf){
  30. free(t->val);
  31. t->val = t->valbuf;
  32. }
  33. } else {
  34. if(t->val != t->valbuf)
  35. t->val = realloc(t->val, n+1);
  36. else
  37. t->val = malloc(n+1);
  38. if(t->val == nil)
  39. sysfatal("ndbsetval %r");
  40. }
  41. strncpy(t->val, val, n);
  42. t->val[n] = 0;
  43. }
  44. /*
  45. * allocate a tuple
  46. */
  47. Ndbtuple*
  48. ndbnew(char *attr, char *val)
  49. {
  50. Ndbtuple *t;
  51. t = mallocz(sizeof(*t), 1);
  52. if(t == nil)
  53. sysfatal("ndbnew %r");
  54. if(attr != nil)
  55. strncpy(t->attr, attr, sizeof(t->attr)-1);
  56. t->val = t->valbuf;
  57. if(val != nil)
  58. ndbsetval(t, val, strlen(val));
  59. return t;
  60. }