Mercurial > code
view port/asprintf.c @ 136:c6d9eb5702e8
array:
o add ARRAY_FASTREMOVE, use last object when removing,
o add ARRAY_CLEARBITS, set to 0 the bytes when removing
parray:
o add PARRAY_FASTREMOVE, use last object when removing,
o add PARRAY_NULLEND, always has a last NULL entry
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 26 Mar 2012 10:24:49 +0200 |
parents | add14be36dad |
children | cedd831d02ff |
line wrap: on
line source
/* * asprintf.c -- basic port of asprintf / vsprintf functions * * Copyright (c) 2011, 2012 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #define BASE 80 int vasprintf(char **res, const char *format, va_list ap) { int rvalue, ok; size_t base = BASE; if ((*res = malloc(base)) == NULL) return -1; ok = 0; do { rvalue = vsnprintf(*res, base, format, ap); if ((signed int)base <= rvalue || rvalue <= 0) { *res = realloc(*res, base + BASE); base += BASE; } else ok = 1; } while (!ok && *res != NULL); return rvalue; } int asprintf(char **res, const char *fmt, ...) { va_list ap; int rvalue; va_start(ap, fmt); rvalue = vasprintf(res, fmt, ap); va_end(ap); return rvalue; }