X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=buffer.c;h=a0cf7b809f87aaa48698ad5fac257f660d197950;hb=e826fd379421938f920e7e51d2a34bf4eb3483a0;hp=f6091fbaec855a3994b5f9e3e665b2a654819e05;hpb=9a4efa4f0dc893f243ee69d1b20f024666ca943d;p=apps%2Fpfixtools.git diff --git a/buffer.c b/buffer.c index f6091fb..a0cf7b8 100644 --- a/buffer.c +++ b/buffer.c @@ -30,9 +30,12 @@ /******************************************************************************/ /* - * Copyright © 2006 Pierre Habouzit + * Copyright © 2006-2007 Pierre Habouzit */ +#include +#include + #include "buffer.h" #define BUFSIZ_INCREMENT 256 @@ -46,4 +49,34 @@ void buffer_resize(buffer_t *buf, ssize_t newsize) } } +void buffer_consume(buffer_t *buf, ssize_t len) { + if (len <= 0) + return; + + if (len >= buf->len) { + buffer_reset(buf); + return; + } + + memmove(buf->data, buf->data + len, buf->len + 1 - len); + buf->len -= len; +} + +ssize_t buffer_read(buffer_t *buf, int fd, ssize_t count) +{ + ssize_t res; + + if (count < 0) + count = BUFSIZ; + + buffer_ensure(buf, count); + res = read(fd, buf->data + buf->len, count); + if (res < 0) { + buf->data[buf->len] = '\0'; + return res; + } + + buffer_extend(buf, res); + return res; +}