ndbaux.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * parse a single tuple
  9. */
  10. char*
  11. _ndbparsetuple(char *cp, Ndbtuple **tp)
  12. {
  13. char *p;
  14. int len;
  15. Ndbtuple *t;
  16. /* a '#' starts a comment lasting till new line */
  17. EATWHITE(cp);
  18. if(*cp == '#' || *cp == '\n')
  19. return 0;
  20. t = ndbnew(nil, nil);
  21. setmalloctag(t, getcallerpc(&cp));
  22. *tp = t;
  23. /* parse attribute */
  24. p = cp;
  25. while(*cp != '=' && !ISWHITE(*cp) && *cp != '\n')
  26. cp++;
  27. len = cp - p;
  28. if(len >= Ndbalen)
  29. len = Ndbalen;
  30. strncpy(t->attr, p, len);
  31. /* parse value */
  32. EATWHITE(cp);
  33. if(*cp == '='){
  34. cp++;
  35. EATWHITE(cp);
  36. if(*cp == '"'){
  37. p = ++cp;
  38. while(*cp != '\n' && *cp != '"')
  39. cp++;
  40. len = cp - p;
  41. if(*cp == '"')
  42. cp++;
  43. } else if(*cp == '#'){
  44. len = 0;
  45. } else {
  46. p = cp;
  47. while(!ISWHITE(*cp) && *cp != '\n')
  48. cp++;
  49. len = cp - p;
  50. }
  51. ndbsetval(t, p, len);
  52. }
  53. return cp;
  54. }
  55. /*
  56. * parse all tuples in a line. we assume that the
  57. * line ends in a '\n'.
  58. *
  59. * the tuples are linked as a list using ->entry and
  60. * as a ring using ->line.
  61. */
  62. Ndbtuple*
  63. _ndbparseline(char *cp)
  64. {
  65. Ndbtuple *t;
  66. Ndbtuple *first, *last;
  67. first = last = 0;
  68. while(*cp != '#' && *cp != '\n'){
  69. t = 0;
  70. cp = _ndbparsetuple(cp, &t);
  71. if(cp == 0)
  72. break;
  73. if(first){
  74. last->line = t;
  75. last->entry = t;
  76. } else
  77. first = t;
  78. last = t;
  79. t->line = 0;
  80. t->entry = 0;
  81. }
  82. if(first)
  83. last->line = first;
  84. setmalloctag(first, getcallerpc(&cp));
  85. return first;
  86. }