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.
 
 
 
 

197 lines
6.8 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef BUFFER_H
  3. #define BUFFER_H
  4. /* for size_t: */
  5. #include <stddef.h>
  6. /* for ssize_t: */
  7. #include <sys/types.h>
  8. /* for strlen */
  9. #include <string.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /* dietlibc defines these in sys/cdefs.h, which is included from stddef.h */
  14. #ifndef __writememsz__
  15. #define __writememsz__(a,b)
  16. #endif
  17. #ifndef __readmemsz__
  18. #define __readmemsz__(a,b)
  19. #endif
  20. #ifndef __readmem__
  21. #define __readmem__(a)
  22. #endif
  23. typedef struct buffer {
  24. char *x; /* actual buffer space */
  25. size_t p; /* current position */
  26. size_t n; /* current size of string in buffer */
  27. size_t a; /* allocated buffer size */
  28. ssize_t (*op)(); /* use read(2) or write(2) */
  29. void* cookie; /* used internally by the to-stralloc buffers, and for buffer chaining */
  30. void (*deinit)(void*); /* called to munmap/free cleanup, with a pointer to the buffer as argument */
  31. int fd; /* passed as first argument to op */
  32. } buffer;
  33. #define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (op), NULL, NULL, (fd) }
  34. #define BUFFER_INIT_FREE(op,fd,buf,len) { (buf), 0, 0, (len), (op), NULL, buffer_free, (fd) }
  35. #define BUFFER_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/
  36. #define BUFFER_INSIZE 8192
  37. #define BUFFER_OUTSIZE 8192
  38. __writememsz__(4,5)
  39. void buffer_init(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
  40. __writememsz__(4,5)
  41. void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
  42. void buffer_free(void* buf);
  43. void buffer_munmap(void* buf);
  44. int buffer_mmapread(buffer* b,const char* filename);
  45. void buffer_close(buffer* b);
  46. int buffer_flush(buffer* b);
  47. __readmemsz__(2,3)
  48. int buffer_put(buffer* b,const char* x,size_t len);
  49. __readmemsz__(2,3)
  50. int buffer_putalign(buffer* b,const char* x,size_t len);
  51. __readmemsz__(2,3)
  52. int buffer_putflush(buffer* b,const char* x,size_t len);
  53. __readmem__(2)
  54. int buffer_puts(buffer* b,const char* x);
  55. __readmem__(2)
  56. int buffer_putsalign(buffer* b,const char* x);
  57. __readmem__(2)
  58. int buffer_putsflush(buffer* b,const char* x);
  59. #if defined(__GNUC__) && !defined(__LIBOWFAT_INTERNAL)
  60. /* as a little gcc-specific hack, if somebody calls buffer_puts with a
  61. * constant string, where we know its length at compile-time, call
  62. * buffer_put with the known length instead */
  63. #define buffer_puts(b,s) (__builtin_constant_p(s) ? buffer_put(b,s,strlen(s)) : buffer_puts(b,s))
  64. #define buffer_putsflush(b,s) (__builtin_constant_p(s) ? buffer_putflush(b,s,strlen(s)) : buffer_putsflush(b,s))
  65. #endif
  66. int buffer_putm_internal(buffer* b,...);
  67. int buffer_putm_internal_flush(buffer* b,...);
  68. #define buffer_putm(b,...) buffer_putm_internal(b,__VA_ARGS__,(char*)0)
  69. #define buffer_putmflush(b,...) buffer_putm_internal_flush(b,__VA_ARGS__,(char*)0)
  70. int buffer_putspace(buffer* b);
  71. int buffer_putnlflush(buffer* b); /* put \n and flush */
  72. #define buffer_PUTC(s,c) \
  73. ( ((s)->a != (s)->p) \
  74. ? ( (s)->x[(s)->p++] = (c), 0 ) \
  75. : buffer_put((s),&(c),1) \
  76. )
  77. __writememsz__(2,3)
  78. ssize_t buffer_get(buffer* b,char* x,size_t len);
  79. ssize_t buffer_feed(buffer* b);
  80. ssize_t buffer_getc(buffer* b,char* x);
  81. __writememsz__(2,3)
  82. ssize_t buffer_getn(buffer* b,char* x,size_t len);
  83. /* read bytes until the destination buffer is full (len bytes), end of
  84. * file is reached or the read char is in charset (setlen bytes). An
  85. * empty line when looking for \n will write '\n' to x and return 0. If
  86. * EOF is reached, \0 is written to the buffer */
  87. __writememsz__(2,3)
  88. __readmemsz__(4,5)
  89. ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen);
  90. __writememsz__(2,3)
  91. ssize_t buffer_getline(buffer* b,char* x,size_t len);
  92. /* this predicate is given the string as currently read from the buffer
  93. * and is supposed to return 1 if the token is complete, 0 if not. */
  94. typedef int (*string_predicate)(const char* x,size_t len);
  95. /* like buffer_get_token but the token ends when your predicate says so */
  96. __writememsz__(2,3)
  97. ssize_t buffer_get_token_pred(buffer* b,char* x,size_t len,string_predicate p);
  98. char *buffer_peek(buffer* b);
  99. void buffer_seek(buffer* b,size_t len);
  100. #define buffer_PEEK(s) ( (s)->x + (s)->p )
  101. #define buffer_SEEK(s,len) ( (s)->p += (len) )
  102. #define buffer_GETC(s,c) \
  103. ( ((s)->p < (s)->n) \
  104. ? ( *(c) = *buffer_PEEK(s), buffer_SEEK((s),1), 1 ) \
  105. : buffer_get((s),(c),1) \
  106. )
  107. int buffer_putulong(buffer *b,unsigned long int l);
  108. int buffer_put8long(buffer *b,unsigned long int l);
  109. int buffer_putxlong(buffer *b,unsigned long int l);
  110. int buffer_putlong(buffer *b,signed long int l);
  111. int buffer_putlonglong(buffer* b,signed long long int l);
  112. int buffer_putulonglong(buffer* b,unsigned long long int l);
  113. int buffer_puterror(buffer* b);
  114. int buffer_puterror2(buffer* b, int errnum);
  115. extern buffer *buffer_0;
  116. extern buffer *buffer_0small;
  117. extern buffer *buffer_1;
  118. extern buffer *buffer_1small;
  119. extern buffer *buffer_2;
  120. #ifdef STRALLOC_H
  121. /* write stralloc to buffer */
  122. int buffer_putsa(buffer* b,const stralloc* sa);
  123. /* write stralloc to buffer and flush */
  124. int buffer_putsaflush(buffer* b,const stralloc* sa);
  125. /* these "read token" functions return 0 if the token was complete or
  126. * EOF was hit or -1 on error. In contrast to the non-stralloc token
  127. * functions, the separator is also put in the stralloc; use
  128. * stralloc_chop or stralloc_chomp to get rid of it. */
  129. /* WARNING! These token reading functions will not clear the stralloc!
  130. * They _append_ the token to the contents of the stralloc. The idea is
  131. * that this way these functions can be used on non-blocking sockets;
  132. * when you get signalled EAGAIN, just call the functions again when new
  133. * data is available. */
  134. /* read token from buffer to stralloc */
  135. __readmemsz__(3,4)
  136. int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
  137. /* read line from buffer to stralloc */
  138. int buffer_getline_sa(buffer* b,stralloc* sa);
  139. /* same as buffer_get_token_sa but empty sa first */
  140. __readmemsz__(3,4)
  141. int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
  142. /* same as buffer_getline_sa but empty sa first */
  143. int buffer_getnewline_sa(buffer* b,stralloc* sa);
  144. typedef int (*sa_predicate)(stralloc* sa);
  145. /* like buffer_get_token_sa but the token ends when your predicate says so */
  146. int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  147. /* same, but clear sa first */
  148. int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  149. /* make a buffer from a stralloc.
  150. * Do not change the stralloc after this! */
  151. void buffer_fromsa(buffer* b,const stralloc* sa); /* read from sa */
  152. int buffer_tosa(buffer*b,stralloc* sa); /* write to sa, auto-growing it */
  153. #endif
  154. __readmemsz__(2,3)
  155. void buffer_frombuf(buffer* b,const char* x,size_t l); /* buffer reads from static buffer */
  156. #ifdef ARRAY_H
  157. void buffer_fromarray(buffer* b,array* a); /* buffer reads from array */
  158. #endif
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif