Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.8 KiB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3. typedef struct buffer {
  4. char *x; /* actual buffer space */
  5. unsigned int p; /* current position */
  6. unsigned int n; /* current size of string in buffer */
  7. unsigned int a; /* allocated buffer size */
  8. int fd; /* passed as first argument to op */
  9. int (*op)(); /* use read(2) or write(2) */
  10. } buffer;
  11. #define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op) }
  12. #define BUFFER_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/
  13. #define BUFFER_INSIZE 8192
  14. #define BUFFER_OUTSIZE 8192
  15. void buffer_init(buffer* b,int (*op)(),int fd,char* y,unsigned int ylen);
  16. int buffer_flush(buffer* b);
  17. int buffer_put(buffer* b,const char* x,unsigned int len);
  18. int buffer_putalign(buffer* b,const char* x,unsigned int len);
  19. int buffer_putflush(buffer* b,const char* x,unsigned int len);
  20. int buffer_puts(buffer* b,const char* x);
  21. int buffer_putsalign(buffer* b,const char* x);
  22. int buffer_putsflush(buffer* b,const char* x);
  23. int buffer_putm_internal(buffer*b,...);
  24. int buffer_putm_internal_flush(buffer*b,...);
  25. #define buffer_putm(b,...) buffer_putm_internal(b,__VA_ARGS__,0)
  26. #define buffer_putmflush(b,...) buffer_putm_internal_flush(b,__VA_ARGS__,0)
  27. int buffer_putspace(buffer* b);
  28. int buffer_putnlflush(buffer* b); /* put \n and flush */
  29. #define buffer_PUTC(s,c) \
  30. ( ((s)->a != (s)->p) \
  31. ? ( (s)->x[(s)->p++] = (c), 0 ) \
  32. : buffer_put((s),&(c),1) \
  33. )
  34. int buffer_get(buffer* b,char* x,unsigned int len);
  35. int buffer_feed(buffer* b);
  36. int buffer_getc(buffer* b,char* x);
  37. int buffer_getn(buffer* b,char* x,unsigned int len);
  38. /* read bytes until the destination buffer is full (len bytes), end of
  39. * file is reached or the read char is in charset (setlen bytes). An
  40. * empty line when looking for \n will write '\n' to x and return 0. If
  41. * EOF is reached, \0 is written to the buffer */
  42. int buffer_get_token(buffer* b,char* x,unsigned int len,const char* charset,unsigned int setlen);
  43. #define buffer_getline(b,x,len) buffer_get_token((b),(x),(len),"\n",1)
  44. /* this predicate is given the string as currently read from the buffer
  45. * and is supposed to return 1 if the token is complete, 0 if not. */
  46. typedef int (*string_predicate)(const char* x,unsigned int len);
  47. /* like buffer_get_token but the token ends when your predicate says so */
  48. int buffer_get_token_pred(buffer* b,char* x,unsigned int len,string_predicate p);
  49. char *buffer_peek(buffer* b);
  50. void buffer_seek(buffer* b,unsigned int len);
  51. #define buffer_PEEK(s) ( (s)->x + (s)->p )
  52. #define buffer_SEEK(s,len) ( (s)->p += (len) )
  53. #define buffer_GETC(s,c) \
  54. ( ((s)->p < (s)->n) \
  55. ? ( *(c) = *buffer_PEEK(s), buffer_SEEK((s),1), 1 ) \
  56. : buffer_get((s),(c),1) \
  57. )
  58. int buffer_copy(buffer* out,buffer* in);
  59. int buffer_putulong(buffer *b,unsigned long l);
  60. int buffer_put8long(buffer *b,unsigned long l);
  61. int buffer_putxlong(buffer *b,unsigned long l);
  62. int buffer_putlong(buffer *b,signed long l);
  63. int buffer_putlonglong(buffer* b,signed long long l);
  64. int buffer_putulonglong(buffer* b,unsigned long long l);
  65. int buffer_puterror(buffer* b);
  66. int buffer_puterror2(buffer* b, int errnum);
  67. extern buffer *buffer_0;
  68. extern buffer *buffer_0small;
  69. extern buffer *buffer_1;
  70. extern buffer *buffer_1small;
  71. extern buffer *buffer_2;
  72. #ifdef STRALLOC_H
  73. /* write stralloc to buffer */
  74. int buffer_putsa(buffer* b,stralloc* sa);
  75. /* write stralloc to buffer and flush */
  76. int buffer_putsaflush(buffer* b,stralloc* sa);
  77. /* these "read token" functions return 0 if the token was complete or
  78. * EOF was hit or -1 on error. In contrast to the non-stralloc token
  79. * functions, the separator is also put in the stralloc; use
  80. * stralloc_chop or stralloc_chomp to get rid of it. */
  81. /* WARNING! These token reading functions will not clear the stralloc!
  82. * They _append_ the token to the contents of the stralloc. The idea is
  83. * that this way these functions can be used on non-blocking sockets;
  84. * when you get signalled EAGAIN, just call the functions again when new
  85. * data is available. */
  86. /* read token from buffer to stralloc */
  87. int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen);
  88. /* read line from buffer to stralloc */
  89. int buffer_getline_sa(buffer* b,stralloc* sa);
  90. /* same as buffer_get_token_sa but empty sa first */
  91. int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen);
  92. /* same as buffer_getline_sa but empty sa first */
  93. int buffer_getnewline_sa(buffer* b,stralloc* sa);
  94. typedef int (*sa_predicate)(stralloc* sa);
  95. /* like buffer_get_token_sa but the token ends when your predicate says so */
  96. int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  97. /* same, but clear sa first */
  98. int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  99. /* make a buffer from a stralloc.
  100. * Do not change the stralloc after this! */
  101. void buffer_fromsa(buffer* b,stralloc* sa);
  102. #endif
  103. #endif