# HG changeset patch # User David Demelier # Date 1335982822 -7200 # Node ID 5ed54050ae3106112a30d9848cf88b61ed99a03a # Parent 09fb5267c906cb0b0614053b72ad70805f694057 Added buf_cut, cut the buffer diff -r 09fb5267c906 -r 5ed54050ae31 buf.c --- a/buf.c Wed Apr 18 21:30:29 2012 +0200 +++ b/buf.c Wed May 02 20:20:22 2012 +0200 @@ -130,7 +130,9 @@ int buf_putc(struct buf *buf, int c) { - return buf_printf(buf, "%c", c); + char str[2] = { c, '\0' }; + + return buf_ncat(buf, str, 1); } /* @@ -204,6 +206,28 @@ } /* + * Remove `n' characters from the buffer, a positive value will cut the string + * from beginning, negative value will cut from end. + */ + +void +buf_cut(struct buf *buf, int n) +{ + int pos; + + if (n < 0 || (unsigned int)n >= buf->length) + return; + + if (n < 0 && buf->length - (-n) > 0) + pos = buf->length - (-n); + else if ((unsigned int)n < buf->length) + pos = n; + + buf->text[pos] = '\0'; + buf->length -= buf->length - pos; +} + +/* * Clear the string buffer. */ diff -r 09fb5267c906 -r 5ed54050ae31 buf.h --- a/buf.h Wed Apr 18 21:30:29 2012 +0200 +++ b/buf.h Wed May 02 20:20:22 2012 +0200 @@ -31,10 +31,6 @@ #ifdef __GNUC__ # define _buf_at_printf(i1, i2) __attribute__ ((format (printf, i1, i2))) -# if __GNUC_MINOR__ > 2 -# pragma GCC diagnostic push -# pragma GCC diagnostic warning "-Wformat" -# endif #else # define _buf_at_printf(i1, i2) #endif @@ -82,15 +78,14 @@ buf_trim(struct buf *); void +buf_cut(struct buf *, int); + +void buf_clear(struct buf *); void buf_free(struct buf *); -#if defined(__GNUC__) && __GNUC_MINOR__ > 2 -# pragma GCC diagnostic pop -#endif - #ifdef __cplusplus } #endif