ndbaux.c 1.5 KB

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