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.
 
 
 
 

137 lines
5.2 KiB

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