Blame printbuf.h

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