ec.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # ifndef EC_H
  2. # define EC_H
  3. # ifndef CORD_H
  4. # include "cord.h"
  5. # endif
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* Extensible cords are strings that may be destructively appended to. */
  10. /* They allow fast construction of cords from characters that are */
  11. /* being read from a stream. */
  12. /*
  13. * A client might look like:
  14. *
  15. * {
  16. * CORD_ec x;
  17. * CORD result;
  18. * char c;
  19. * FILE *f;
  20. *
  21. * ...
  22. * CORD_ec_init(x);
  23. * while(...) {
  24. * c = getc(f);
  25. * ...
  26. * CORD_ec_append(x, c);
  27. * }
  28. * result = CORD_balance(CORD_ec_to_cord(x));
  29. *
  30. * If a C string is desired as the final result, the call to CORD_balance
  31. * may be replaced by a call to CORD_to_char_star.
  32. */
  33. # ifndef CORD_BUFSZ
  34. # define CORD_BUFSZ 128
  35. # endif
  36. typedef struct CORD_ec_struct {
  37. CORD ec_cord;
  38. char * ec_bufptr;
  39. char ec_buf[CORD_BUFSZ+1];
  40. } CORD_ec[1];
  41. /* This structure represents the concatenation of ec_cord with */
  42. /* ec_buf[0 ... (ec_bufptr-ec_buf-1)] */
  43. /* Flush the buffer part of the extended chord into ec_cord. */
  44. /* Note that this is almost the only real function, and it is */
  45. /* implemented in 6 lines in cordxtra.c */
  46. void CORD_ec_flush_buf(CORD_ec x);
  47. /* Convert an extensible cord to a cord. */
  48. # define CORD_ec_to_cord(x) (CORD_ec_flush_buf(x), (x)[0].ec_cord)
  49. /* Initialize an extensible cord. */
  50. #define CORD_ec_init(x) \
  51. ((x)[0].ec_cord = 0, (void)((x)[0].ec_bufptr = (x)[0].ec_buf))
  52. /* Append a character to an extensible cord. */
  53. #define CORD_ec_append(x, c) \
  54. (((x)[0].ec_bufptr == (x)[0].ec_buf + CORD_BUFSZ ? \
  55. (CORD_ec_flush_buf(x), 0) : 0), \
  56. (void)(*(x)[0].ec_bufptr++ = (c)))
  57. /* Append a cord to an extensible cord. Structure remains shared with */
  58. /* original. */
  59. void CORD_ec_append_cord(CORD_ec x, CORD s);
  60. #ifdef __cplusplus
  61. } /* extern "C" */
  62. #endif
  63. # endif /* EC_H */