ndbaux.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-1;
  30. strncpy(t->attr, p, len);
  31. /* parse value */
  32. EATWHITE(cp);
  33. if(*cp == '='){
  34. cp++;
  35. if(*cp == '"'){
  36. p = ++cp;
  37. while(*cp != '\n' && *cp != '"')
  38. cp++;
  39. len = cp - p;
  40. if(*cp == '"')
  41. cp++;
  42. } else if(*cp == '#'){
  43. len = 0;
  44. } else {
  45. p = cp;
  46. while(!ISWHITE(*cp) && *cp != '\n')
  47. cp++;
  48. len = cp - p;
  49. }
  50. ndbsetval(t, p, len);
  51. }
  52. return cp;
  53. }
  54. /*
  55. * parse all tuples in a line. we assume that the
  56. * line ends in a '\n'.
  57. *
  58. * the tuples are linked as a list using ->entry and
  59. * as a ring using ->line.
  60. */
  61. Ndbtuple*
  62. _ndbparseline(char *cp)
  63. {
  64. Ndbtuple *t;
  65. Ndbtuple *first, *last;
  66. first = last = 0;
  67. while(*cp != '#' && *cp != '\n'){
  68. t = 0;
  69. cp = _ndbparsetuple(cp, &t);
  70. if(cp == 0)
  71. break;
  72. if(first){
  73. last->line = t;
  74. last->entry = t;
  75. } else
  76. first = t;
  77. last = t;
  78. t->line = 0;
  79. t->entry = 0;
  80. }
  81. if(first)
  82. last->line = first;
  83. setmalloctag(first, getcallerpc(&cp));
  84. return first;
  85. }