added initial buffer implementation.
parent
762298de12
commit
a50b334f58
@ -0,0 +1,12 @@
|
||||
#include <unistd.h>
|
||||
#include "buffer.h"
|
||||
|
||||
static int b0read(int fd,const char* buf, unsigned int len) {
|
||||
if (buffer_flush(buffer_1)<0) return -1;
|
||||
return read(fd,buf,len);
|
||||
}
|
||||
|
||||
char buffer_0_space[BUFFER_INSIZE];
|
||||
static buffer it = BUFFER_INIT(b0read,0,buffer_0_space,sizeof buffer_0_space);
|
||||
buffer *buffer_0 = ⁢
|
||||
|
@ -0,0 +1,12 @@
|
||||
#include <unistd.h>
|
||||
#include "buffer.h"
|
||||
|
||||
static int b0read(int fd,const char* buf, unsigned int len) {
|
||||
if (buffer_flush(buffer_1small)<0) return -1;
|
||||
return read(fd,buf,len);
|
||||
}
|
||||
|
||||
char buffer_0_space[128];
|
||||
static buffer it = BUFFER_INIT(b0read,0,buffer_0_space,sizeof buffer_0_space);
|
||||
buffer *buffer_0small = ⁢
|
||||
|
@ -0,0 +1,7 @@
|
||||
#include <unistd.h>
|
||||
#include "buffer.h"
|
||||
|
||||
char buffer_1_space[BUFFER_INSIZE];
|
||||
static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space);
|
||||
buffer *buffer_1 = ⁢
|
||||
|
@ -0,0 +1,7 @@
|
||||
#include <unistd.h>
|
||||
#include "buffer.h"
|
||||
|
||||
char buffer_1_space[128];
|
||||
static buffer it = BUFFER_INIT(write,1,buffer_1_space,sizeof buffer_1_space);
|
||||
buffer *buffer_1small = ⁢
|
||||
|
@ -0,0 +1,14 @@
|
||||
#include "buffer.h"
|
||||
|
||||
extern int buffer_stubborn_read(int (*op)(),int fd,const char* buf, unsigned int len);
|
||||
|
||||
int buffer_feed(buffer* b) {
|
||||
if (b->p==b->n) {
|
||||
int w;
|
||||
if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->n))<0)
|
||||
return -1;
|
||||
b->n=w;
|
||||
b->p=0;
|
||||
}
|
||||
return b->n;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#include "buffer.h"
|
||||
|
||||
extern int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len);
|
||||
|
||||
extern int buffer_flush(buffer* b) {
|
||||
register int p;
|
||||
if (!(p=b->p)) return 0; /* buffer already empty */
|
||||
b->p=0;
|
||||
return buffer_stubborn(b->op,b->fd,b->x,p);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#include "byte.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_get(buffer* b,char* x,unsigned int len) {
|
||||
int blen;
|
||||
if ((blen=buffer_feed(b))>=len)
|
||||
blen=len;
|
||||
if (blen<=0) return blen;
|
||||
byte_copy(x,blen,b->x+b->p);
|
||||
b->p+=blen;
|
||||
return blen;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#include "buffer.h"
|
||||
|
||||
void buffer_init(buffer* b,int (*op)(),int fd,char* y,unsigned int ylen) {
|
||||
b->op=op;
|
||||
b->fd=fd;
|
||||
b->x=y;
|
||||
b->n=ylen;
|
||||
b->p=0;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#include "buffer.h"
|
||||
|
||||
char *buffer_peek(buffer* b) {
|
||||
return b->x+b->p;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#include "byte.h"
|
||||
#include "buffer.h"
|
||||
|
||||
extern int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len);
|
||||
|
||||
int buffer_put(buffer* b,const char* buf,unsigned int len) {
|
||||
if (len>b->n-b->p) { /* doesn't fit */
|
||||
if (buffer_flush(b)==-1) return -1;
|
||||
if (len>b->n) {
|
||||
if (buffer_stubborn(b->op,b->fd,buf,len)<0) return -1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
byte_copy(b->x+b->p, len, buf);
|
||||
b->p+=len;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
#include "byte.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_putalign(buffer* b,const char* buf,unsigned int len) {
|
||||
int tmp;
|
||||
while (len>(tmp=b->n-b->p)) {
|
||||
byte_copy(b->x+b->p, tmp, buf);
|
||||
b->p+=tmp;
|
||||
buf+=tmp;
|
||||
len-=tmp;
|
||||
if (buffer_flush(b)<0) return -1;
|
||||
}
|
||||
byte_copy(b->x+b->p, len, buf);
|
||||
b->p+=len;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_putflush(buffer* b,const char* x,unsigned int len) {
|
||||
if (buffer_put(b,x,len)<0) return -1;
|
||||
if (buffer_flush(b)<0) return -1;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#include "str.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_puts(buffer* b,const char* x) {
|
||||
buffer_put(b,x,str_len(x));
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#include "str.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_puts(buffer* b,const char* x) {
|
||||
buffer_putalign(b,x,str_len(x));
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#include "str.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_puts(buffer* b,const char* x) {
|
||||
buffer_putflush(b,x,str_len(x));
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
#include <errno.h>
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_stubborn(int (*op)(),int fd,const char* buf, unsigned int len) {
|
||||
int w;
|
||||
while (len) {
|
||||
if ((w=op(fd,buf,len))<0) {
|
||||
if (errno == EINTR) continue;
|
||||
return -1;
|
||||
};
|
||||
buf+=w;
|
||||
len-=w;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#include <errno.h>
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_stubborn_read(int (*op)(),int fd,const char* buf, unsigned int len) {
|
||||
int w;
|
||||
for (;;) {
|
||||
if ((w=op(fd,buf,len))<0)
|
||||
if (errno == EINTR) continue;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
Loading…
Reference in New Issue