Blame printbuf.h

Packit ea8578
/*
Packit ea8578
 * $Id: printbuf.h,v 1.4 2006/01/26 02:16:28 mclark Exp $
Packit ea8578
 *
Packit ea8578
 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Packit ea8578
 * Michael Clark <michael@metaparadigm.com>
Packit ea8578
 *
Packit ea8578
 * This library is free software; you can redistribute it and/or modify
Packit ea8578
 * it under the terms of the MIT license. See COPYING for details.
Packit ea8578
 *
Packit ea8578
 *
Packit ea8578
 * Copyright (c) 2008-2009 Yahoo! Inc.  All rights reserved.
Packit ea8578
 * The copyrights to the contents of this file are licensed under the MIT License
Packit ea8578
 * (http://www.opensource.org/licenses/mit-license.php)
Packit ea8578
 */
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * @file
Packit ea8578
 * @brief Internal string buffer handing.  Unless you're writing a 
Packit ea8578
 *        json_object_to_json_string_fn implementation for use with
Packit ea8578
 *        json_object_set_serializer() direct use of this is not
Packit ea8578
 *        recommended.
Packit ea8578
 */
Packit ea8578
#ifndef _printbuf_h_
Packit ea8578
#define _printbuf_h_
Packit ea8578
Packit ea8578
#ifdef __cplusplus
Packit ea8578
extern "C" {
Packit ea8578
#endif
Packit ea8578
Packit ea8578
struct printbuf {
Packit ea8578
  char *buf;
Packit ea8578
  int bpos;
Packit ea8578
  int size;
Packit ea8578
};
Packit ea8578
typedef struct printbuf printbuf;
Packit ea8578
Packit ea8578
extern struct printbuf*
Packit ea8578
printbuf_new(void);
Packit ea8578
Packit ea8578
/* As an optimization, printbuf_memappend_fast() is defined as a macro
Packit ea8578
 * that handles copying data if the buffer is large enough; otherwise
Packit ea8578
 * it invokes printbuf_memappend() which performs the heavy
Packit ea8578
 * lifting of realloc()ing the buffer and copying data.
Packit ea8578
 *
Packit ea8578
 * Your code should not use printbuf_memappend() directly unless it
Packit ea8578
 * checks the return code. Use printbuf_memappend_fast() instead.
Packit ea8578
 */
Packit ea8578
extern int
Packit ea8578
printbuf_memappend(struct printbuf *p, const char *buf, int size);
Packit ea8578
Packit ea8578
#define printbuf_memappend_fast(p, bufptr, bufsize)          \
Packit ea8578
do {                                                         \
Packit ea8578
  if ((p->size - p->bpos) > bufsize) {                       \
Packit ea8578
    memcpy(p->buf + p->bpos, (bufptr), bufsize);             \
Packit ea8578
    p->bpos += bufsize;                                      \
Packit ea8578
    p->buf[p->bpos]= '\0';                                   \
Packit ea8578
  } else {  printbuf_memappend(p, (bufptr), bufsize); }      \
Packit ea8578
} while (0)
Packit ea8578
Packit ea8578
#define printbuf_length(p) ((p)->bpos)
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * Results in a compile error if the argument is not a string literal.
Packit ea8578
 */
Packit ea8578
#define _printbuf_check_literal(mystr) ("" mystr)
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * This is an optimization wrapper around printbuf_memappend() that is useful
Packit ea8578
 * for appending string literals. Since the size of string constants is known
Packit ea8578
 * at compile time, using this macro can avoid a costly strlen() call. This is
Packit ea8578
 * especially helpful when a constant string must be appended many times. If
Packit ea8578
 * you got here because of a compilation error caused by passing something
Packit ea8578
 * other than a string literal, use printbuf_memappend_fast() in conjunction
Packit ea8578
 * with strlen().
Packit ea8578
 *
Packit ea8578
 * See also:
Packit ea8578
 *   printbuf_memappend_fast()
Packit ea8578
 *   printbuf_memappend()
Packit ea8578
 *   sprintbuf()
Packit ea8578
 */
Packit ea8578
#define printbuf_strappend(pb, str) \
Packit ea8578
   printbuf_memappend ((pb), _printbuf_check_literal(str), sizeof(str) - 1)
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * Set len bytes of the buffer to charvalue, starting at offset offset.
Packit ea8578
 * Similar to calling memset(x, charvalue, len);
Packit ea8578
 *
Packit ea8578
 * The memory allocated for the buffer is extended as necessary.
Packit ea8578
 *
Packit ea8578
 * If offset is -1, this starts at the end of the current data in the buffer.
Packit ea8578
 */
Packit ea8578
extern int
Packit ea8578
printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * Formatted print to printbuf.
Packit ea8578
 *
Packit ea8578
 * This function is the most expensive of the available functions for appending
Packit ea8578
 * string data to a printbuf and should be used only where convenience is more
Packit ea8578
 * important than speed. Avoid using this function in high performance code or
Packit ea8578
 * tight loops; in these scenarios, consider using snprintf() with a static
Packit ea8578
 * buffer in conjunction with one of the printbuf_*append() functions.
Packit ea8578
 *
Packit ea8578
 * See also:
Packit ea8578
 *   printbuf_memappend_fast()
Packit ea8578
 *   printbuf_memappend()
Packit ea8578
 *   printbuf_strappend()
Packit ea8578
 */
Packit ea8578
extern int
Packit ea8578
sprintbuf(struct printbuf *p, const char *msg, ...);
Packit ea8578
Packit ea8578
extern void
Packit ea8578
printbuf_reset(struct printbuf *p);
Packit ea8578
Packit ea8578
extern void
Packit ea8578
printbuf_free(struct printbuf *p);
Packit ea8578
Packit ea8578
#ifdef __cplusplus
Packit ea8578
}
Packit ea8578
#endif
Packit ea8578
Packit ea8578
#endif