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.

266 lines
9.2 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
9 months ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  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. #include <libowfat/compiler.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /* dietlibc defines these in sys/cdefs.h, which is included from stddef.h */
  15. #ifndef __writememsz__
  16. #define __writememsz__(a,b)
  17. #endif
  18. #ifndef __readmemsz__
  19. #define __readmemsz__(a,b)
  20. #endif
  21. #ifndef __readmem__
  22. #define __readmem__(a)
  23. #endif
  24. typedef struct buffer {
  25. char *x; /* actual buffer space */
  26. size_t p; /* current position */
  27. size_t n; /* current size of string in buffer */
  28. size_t a; /* allocated buffer size */
  29. ssize_t (*op)(); /* use read(2) or write(2) */
  30. void* cookie; /* used internally by the to-stralloc buffers, and for buffer chaining */
  31. void (*deinit)(void*); /* called to munmap/free cleanup, with a pointer to the buffer as argument */
  32. int fd; /* passed as first argument to op */
  33. } buffer;
  34. #define BUFFER_INIT(op,fd,buf,len) { (char*)(buf), 0, 0, (len), (op), NULL, NULL, (fd) }
  35. #define BUFFER_INIT_FREE(op,fd,buf,len) { (buf), 0, 0, (len), (op), NULL, buffer_free, (fd) }
  36. #define BUFFER_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/
  37. #define BUFFER_INSIZE 8192
  38. #define BUFFER_OUTSIZE 8192
  39. /* Initialize a buffer with an existing memory area, which the buffer
  40. * will NOT take ownership of (i.e. won't free the memory when it's done) */
  41. att_writen(4,5)
  42. void buffer_init(buffer* b, ssize_t (*op)(), int fd, char* y, size_t ylen);
  43. /* Initialize a buffer with an existing memory area, which the buffer
  44. * WILL take ownership of (it will call free() on it when it's done) */
  45. att_writen(4,5)
  46. void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
  47. /* Call buffer_init with op=read(), return 0.
  48. * If fd==-1, return -1 instead, leaving b untouched. */
  49. att_writen(3,4)
  50. int buffer_init_read(buffer* b, int fd, char* y, size_t ylen);
  51. /* Call buffer_init with op=write(), return 0.
  52. * If fd==-1, return -1 instead, leaving b untouched. */
  53. att_writen(3,4)
  54. int buffer_init_write(buffer* b, int fd, char* y, size_t ylen);
  55. /* Will allocate a buffer of size ylen and then call
  56. * buffer_init_free(b, op, fd, thebuffer, ylen) on it.
  57. * Returns 0 on success, -1 on error (setting errno).
  58. * Passing fd==-1 is treated as error (so you can pass open*() for fd). */
  59. int buffer_init_allocbuf(buffer* b, ssize_t (*op)(), int fd, size_t ylen);
  60. /* Call buffer_init_allocbuf with op=read */
  61. int buffer_init_read_allocbuf(buffer* b, int fd, size_t ylen);
  62. /* Call buffer_init_allocbuf with op=read */
  63. int buffer_init_write_allocbuf(buffer* b, int fd, size_t ylen);
  64. /* Initialize a buffer without actual I/O.
  65. * You give it a pre-existing memory area.
  66. * When reading from this buffer, it will simply return the data from
  67. * that memory area. If it reaches the end, it will signal EOF and never
  68. * actually attempt to read from any actual file.
  69. * Does not take ownership. Useful for testing. */
  70. att_readn(2,3)
  71. void buffer_init_staticcontents(buffer* b,char* y,size_t ylen);
  72. /* Same but the buffer takes ownership of the static buffer and frees it
  73. * in buffer_close. */
  74. att_readn(2,3)
  75. void buffer_init_staticcontents_free(buffer* b,char* y,size_t ylen);
  76. /* Set buffer->deinit to this if you want buffer_close() to call free() on
  77. * the associated memory buffer */
  78. void buffer_free(void* buf);
  79. /* Set buffer->deinit to this if you want buffer_close() to call munmap() on
  80. * the associated memory buffer */
  81. void buffer_munmap(void* buf);
  82. /* Initialize a buffer so it will read from this file by memory mapping
  83. * the whole thing. */
  84. att_read(2)
  85. int buffer_mmapread(buffer* b,const char* filename);
  86. /* Indicate you are done with a buffer.
  87. * If the buffer has an associated memory map or buffer it owns, it will
  88. * free that memory. This will NOT call free() on b itself! */
  89. void buffer_close(buffer* b);
  90. /* Flush the buffer. This is only meaningful for write buffers and will
  91. * cause a write() syscall (or whatever you set as buffer->op) for all
  92. * the data in the buffer. */
  93. int buffer_flush(buffer* b);
  94. att_readn(2,3)
  95. int buffer_put(buffer* b,const char* x,size_t len);
  96. att_readn(2,3)
  97. int buffer_putalign(buffer* b,const char* x,size_t len);
  98. att_readn(2,3)
  99. int buffer_putflush(buffer* b,const char* x,size_t len);
  100. att_read(2)
  101. int buffer_puts(buffer* b,const char* x);
  102. att_read(2)
  103. int buffer_putsalign(buffer* b,const char* x);
  104. att_read(2)
  105. int buffer_putsflush(buffer* b,const char* x);
  106. #if defined(__GNUC__) && !defined(__LIBOWFAT_INTERNAL)
  107. /* as a little gcc-specific hack, if somebody calls buffer_puts with a
  108. * constant string, where we know its length at compile-time, call
  109. * buffer_put with the known length instead */
  110. #define buffer_puts(b,s) (__builtin_constant_p(s) ? buffer_put(b,s,strlen(s)) : buffer_puts(b,s))
  111. #define buffer_putsflush(b,s) (__builtin_constant_p(s) ? buffer_putflush(b,s,strlen(s)) : buffer_putsflush(b,s))
  112. #endif
  113. int buffer_putm_internal(buffer* b,...);
  114. int buffer_putm_internal_flush(buffer* b,...);
  115. #define buffer_putm(b,...) buffer_putm_internal(b,__VA_ARGS__,(char*)0)
  116. #define buffer_putmflush(b,...) buffer_putm_internal_flush(b,__VA_ARGS__,(char*)0)
  117. int buffer_putspace(buffer* b);
  118. int buffer_putnlflush(buffer* b); /* put \n and flush */
  119. #define buffer_PUTC(s,c) \
  120. ( ((s)->a != (s)->p) \
  121. ? ( (s)->x[(s)->p++] = (c), 0 ) \
  122. : buffer_put((s),&(c),1) \
  123. )
  124. att_writen(2,3)
  125. ssize_t buffer_get(buffer* b,char* x,size_t len);
  126. ssize_t buffer_feed(buffer* b);
  127. ssize_t buffer_getc(buffer* b,char* x);
  128. ssize_t buffer_peekc(buffer* b,char* x);
  129. att_writen(2,3)
  130. ssize_t buffer_getn(buffer* b,char* x,size_t len);
  131. /* read bytes until the destination buffer is full (len bytes), end of
  132. * file is reached or the read char is in charset (setlen bytes). An
  133. * empty line when looking for \n will write '\n' to x and return 0. If
  134. * EOF is reached, \0 is written to the buffer */
  135. att_writen(2,3)
  136. att_readn(4,5)
  137. ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen);
  138. att_writen(2,3)
  139. ssize_t buffer_getline(buffer* b,char* x,size_t len);
  140. /* this predicate is given the string as currently read from the buffer
  141. * and is supposed to return 1 if the token is complete, 0 if not. */
  142. typedef int (*string_predicate)(const char* x,size_t len);
  143. /* like buffer_get_token but the token ends when your predicate says so */
  144. att_writen(2,3)
  145. ssize_t buffer_get_token_pred(buffer* b,char* x,size_t len,string_predicate p);
  146. char *buffer_peek(buffer* b);
  147. ssize_t buffer_seek(buffer* b,size_t len);
  148. #define buffer_PEEK(s) ( (s)->x + (s)->p )
  149. #define buffer_SEEK(s,len) ( (s)->p += (len) )
  150. #define buffer_GETC(s,c) \
  151. ( ((s)->p < (s)->n) \
  152. ? ( *(c) = *buffer_PEEK(s), buffer_SEEK((s),1), 1 ) \
  153. : buffer_get((s),(c),1) \
  154. )
  155. int buffer_putulong(buffer *b,unsigned long int l);
  156. int buffer_put8long(buffer *b,unsigned long int l);
  157. int buffer_putxlong(buffer *b,unsigned long int l);
  158. int buffer_putlong(buffer *b,signed long int l);
  159. int buffer_putlonglong(buffer* b,signed long long int l);
  160. int buffer_putulonglong(buffer* b,unsigned long long int l);
  161. int buffer_puterror(buffer* b);
  162. int buffer_puterror2(buffer* b, int errnum);
  163. extern buffer *buffer_0;
  164. extern buffer *buffer_0small;
  165. extern buffer *buffer_1;
  166. extern buffer *buffer_1small;
  167. extern buffer *buffer_2;
  168. #ifdef STRALLOC_H
  169. /* write stralloc to buffer */
  170. int buffer_putsa(buffer* b,const stralloc* sa);
  171. /* write stralloc to buffer and flush */
  172. int buffer_putsaflush(buffer* b,const stralloc* sa);
  173. /* these "read token" functions return 0 if the token was complete or
  174. * EOF was hit or -1 on error. In contrast to the non-stralloc token
  175. * functions, the separator is also put in the stralloc; use
  176. * stralloc_chop or stralloc_chomp to get rid of it. */
  177. /* WARNING! These token reading functions will not clear the stralloc!
  178. * They _append_ the token to the contents of the stralloc. The idea is
  179. * that this way these functions can be used on non-blocking sockets;
  180. * when you get signalled EAGAIN, just call the functions again when new
  181. * data is available. */
  182. /* read token from buffer to stralloc */
  183. att_readn(3,4)
  184. int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
  185. /* read line from buffer to stralloc */
  186. int buffer_getline_sa(buffer* b,stralloc* sa);
  187. /* same as buffer_get_token_sa but empty sa first */
  188. att_readn(3,4)
  189. int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
  190. /* same as buffer_getline_sa but empty sa first */
  191. int buffer_getnewline_sa(buffer* b,stralloc* sa);
  192. typedef int (*sa_predicate)(stralloc* sa);
  193. /* like buffer_get_token_sa but the token ends when your predicate says so */
  194. int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  195. /* same, but clear sa first */
  196. int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  197. /* make a buffer from a stralloc.
  198. * Do not change the stralloc after this! */
  199. void buffer_fromsa(buffer* b,const stralloc* sa); /* read from sa */
  200. int buffer_tosa(buffer*b,stralloc* sa); /* write to sa, auto-growing it */
  201. #endif
  202. att_readn(2,3)
  203. void buffer_frombuf(buffer* b,const char* x,size_t l); /* buffer reads from static buffer */
  204. #ifdef ARRAY_H
  205. void buffer_fromarray(buffer* b,array* a); /* buffer reads from array */
  206. #endif
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif