|
|
|
@ -1,13 +1,18 @@
|
|
|
|
|
#ifndef BUFFER_H
|
|
|
|
|
#define BUFFER_H
|
|
|
|
|
|
|
|
|
|
/* for size_t: */
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
/* for ssize_t: */
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
typedef struct buffer {
|
|
|
|
|
char *x; /* actual buffer space */
|
|
|
|
|
unsigned long int p; /* current position */
|
|
|
|
|
unsigned long int n; /* current size of string in buffer */
|
|
|
|
|
unsigned long int a; /* allocated buffer size */
|
|
|
|
|
size_t p; /* current position */
|
|
|
|
|
size_t n; /* current size of string in buffer */
|
|
|
|
|
size_t a; /* allocated buffer size */
|
|
|
|
|
int fd; /* passed as first argument to op */
|
|
|
|
|
int (*op)(); /* use read(2) or write(2) */
|
|
|
|
|
ssize_t (*op)(); /* use read(2) or write(2) */
|
|
|
|
|
enum { NOTHING, FREE, MUNMAP } todo;
|
|
|
|
|
} buffer;
|
|
|
|
|
|
|
|
|
@ -17,15 +22,15 @@ typedef struct buffer {
|
|
|
|
|
#define BUFFER_INSIZE 8192
|
|
|
|
|
#define BUFFER_OUTSIZE 8192
|
|
|
|
|
|
|
|
|
|
void buffer_init(buffer* b,int (*op)(),int fd,char* y,unsigned long int ylen);
|
|
|
|
|
void buffer_init_free(buffer* b,int (*op)(),int fd,char* y,unsigned long int ylen);
|
|
|
|
|
void buffer_init(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
|
|
|
|
|
void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen);
|
|
|
|
|
int buffer_mmapread(buffer* b,const char* filename);
|
|
|
|
|
void buffer_close(buffer* b);
|
|
|
|
|
|
|
|
|
|
int buffer_flush(buffer* b);
|
|
|
|
|
int buffer_put(buffer* b,const char* x,unsigned long int len);
|
|
|
|
|
int buffer_putalign(buffer* b,const char* x,unsigned long int len);
|
|
|
|
|
int buffer_putflush(buffer* b,const char* x,unsigned long int len);
|
|
|
|
|
int buffer_put(buffer* b,const char* x,size_t len);
|
|
|
|
|
int buffer_putalign(buffer* b,const char* x,size_t len);
|
|
|
|
|
int buffer_putflush(buffer* b,const char* x,size_t len);
|
|
|
|
|
int buffer_puts(buffer* b,const char* x);
|
|
|
|
|
int buffer_putsalign(buffer* b,const char* x);
|
|
|
|
|
int buffer_putsflush(buffer* b,const char* x);
|
|
|
|
@ -44,27 +49,27 @@ int buffer_putnlflush(buffer* b); /* put \n and flush */
|
|
|
|
|
: buffer_put((s),&(c),1) \
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
int buffer_get(buffer* b,char* x,unsigned long int len);
|
|
|
|
|
ssize_t buffer_get(buffer* b,char* x,size_t len);
|
|
|
|
|
int buffer_feed(buffer* b);
|
|
|
|
|
int buffer_getc(buffer* b,char* x);
|
|
|
|
|
int buffer_getn(buffer* b,char* x,unsigned long int len);
|
|
|
|
|
ssize_t buffer_getn(buffer* b,char* x,size_t len);
|
|
|
|
|
|
|
|
|
|
/* read bytes until the destination buffer is full (len bytes), end of
|
|
|
|
|
* file is reached or the read char is in charset (setlen bytes). An
|
|
|
|
|
* empty line when looking for \n will write '\n' to x and return 0. If
|
|
|
|
|
* EOF is reached, \0 is written to the buffer */
|
|
|
|
|
int buffer_get_token(buffer* b,char* x,unsigned long int len,const char* charset,unsigned long int setlen);
|
|
|
|
|
int buffer_getline(buffer* b,char* x,unsigned long int len);
|
|
|
|
|
ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen);
|
|
|
|
|
ssize_t buffer_getline(buffer* b,char* x,size_t len);
|
|
|
|
|
|
|
|
|
|
/* this predicate is given the string as currently read from the buffer
|
|
|
|
|
* and is supposed to return 1 if the token is complete, 0 if not. */
|
|
|
|
|
typedef int (*string_predicate)(const char* x,unsigned long int len);
|
|
|
|
|
typedef int (*string_predicate)(const char* x,size_t len);
|
|
|
|
|
|
|
|
|
|
/* like buffer_get_token but the token ends when your predicate says so */
|
|
|
|
|
int buffer_get_token_pred(buffer* b,char* x,unsigned long int len,string_predicate p);
|
|
|
|
|
ssize_t buffer_get_token_pred(buffer* b,char* x,size_t len,string_predicate p);
|
|
|
|
|
|
|
|
|
|
char *buffer_peek(buffer* b);
|
|
|
|
|
void buffer_seek(buffer* b,unsigned long int len);
|
|
|
|
|
void buffer_seek(buffer* b,size_t len);
|
|
|
|
|
|
|
|
|
|
#define buffer_PEEK(s) ( (s)->x + (s)->p )
|
|
|
|
|
#define buffer_SEEK(s,len) ( (s)->p += (len) )
|
|
|
|
@ -112,12 +117,12 @@ int buffer_putsaflush(buffer* b,stralloc* sa);
|
|
|
|
|
* data is available. */
|
|
|
|
|
|
|
|
|
|
/* read token from buffer to stralloc */
|
|
|
|
|
int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned long int setlen);
|
|
|
|
|
int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
|
|
|
|
|
/* read line from buffer to stralloc */
|
|
|
|
|
int buffer_getline_sa(buffer* b,stralloc* sa);
|
|
|
|
|
|
|
|
|
|
/* same as buffer_get_token_sa but empty sa first */
|
|
|
|
|
int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned long int setlen);
|
|
|
|
|
int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,size_t setlen);
|
|
|
|
|
/* same as buffer_getline_sa but empty sa first */
|
|
|
|
|
int buffer_getnewline_sa(buffer* b,stralloc* sa);
|
|
|
|
|
|
|
|
|
|