/*- * Copyright (c) 1999, 2000, 2001, 2002, 2003 Lev Walkin . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: sf_sbuf.h,v 1.6 2006/12/11 07:11:52 vlm Exp $ */ #ifndef __SF_SBUF_H__ #define __SF_SBUF_H__ #include #include #include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /**************************/ /*** Smart data buffers ***/ /**************************/ typedef struct { char *buf; size_t len; /* Meaningful length */ size_t size; /* Allocated size of the *buf. */ size_t off; /* Offset from the start (not related with len) */ } sbuf; #define sbuf2ptr(sb) ((sb)->buf + (sb)->off) /* Pointer to the first char */ #define sbuf_len(sb) ((sb)->len - (sb)->off) /* Meaningful length */ sbuf *sbuf_init(void); void sbuf_free(sbuf *sb); /* Destroy buffer */ /* Detach the sb->buf pointer and reinitialize buffer */ char *sbuf_detach(sbuf *sb, size_t *optLength, size_t *optSize); int sbuf_clear(sbuf *); /* Clear buffer contents & truncate memory */ int sbuf_zero(sbuf *); /* Clear buffer contents */ ssize_t sbuf_add(sbuf *, const char *string); /* Add a copy of the string */ ssize_t sbuf_add2(sbuf *, const void *data, size_t nbytes); /* ... */ #define SBUF_ADDCHAR(s,c) do { \ if(s->len >= s->size) \ sbuf_extend(s, s->size); \ s->buf[s->len++] = c; \ } while(0) ssize_t sbuf_trim(sbuf *, int _trim_left, size_t nbytes); /* Trim */ ssize_t sbuf_extend(sbuf *, size_t upto); /* Extend buffer up to upto. */ /* * Add a formatted string to the buffer, extending it when necessary. * Returns -1 if memory allocation fails. * Otherwise, returns the number of characters added. */ ssize_t sbuf_sprintf(sbuf *, const char *fmt, ...) #if __GNUC__ >= 3 __attribute__ ((format (printf, 2, 3))) #endif ; ssize_t sbuf_vsprintf(sbuf *, const char *fmt, va_list ap); char *sbuf_fgets(sbuf *, FILE *stream); /* Read till the end of line. */ char *sbuf_fetch(sbuf *sb, size_t len, char *optDelimiter, size_t *optLength, int flags); /* Fork a substring from the buffer */ #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* __SF_SBUF_H__ */