ndbparse.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 data base entry. Entries may span multiple
  9. * lines. An entry starts on a left margin. All subsequent
  10. * lines must be indented by white space. An entry consists
  11. * of tuples of the forms:
  12. * attribute-name
  13. * attribute-name=value
  14. * attribute-name="value with white space"
  15. *
  16. * The parsing returns a 2-dimensional structure. The first
  17. * dimension joins all tuples. All tuples on the same line
  18. * form a ring along the second dimension.
  19. */
  20. /*
  21. * parse the next entry in the file
  22. */
  23. Ndbtuple*
  24. ndbparse(Ndb *db)
  25. {
  26. char *line;
  27. Ndbtuple *t;
  28. Ndbtuple *first, *last;
  29. int len;
  30. first = last = 0;
  31. for(;;){
  32. if((line = Brdline(&db->b, '\n')) == 0)
  33. break;
  34. len = Blinelen(&db->b);
  35. if(line[len-1] != '\n')
  36. break;
  37. if(first && !ISWHITE(*line) && *line != '#'){
  38. Bseek(&db->b, -len, 1);
  39. return first;
  40. }
  41. t = _ndbparseline(line);
  42. if(t == 0)
  43. continue;
  44. if(first)
  45. last->entry = t;
  46. else
  47. first = t;
  48. last = t;
  49. while(last->entry)
  50. last = last->entry;
  51. }
  52. return first;
  53. }