universe@599: /* universe@599: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@599: * universe@599: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@599: * universe@599: * Redistribution and use in source and binary forms, with or without universe@599: * modification, are permitted provided that the following conditions are met: universe@599: * universe@599: * 1. Redistributions of source code must retain the above copyright universe@599: * notice, this list of conditions and the following disclaimer. universe@599: * universe@599: * 2. Redistributions in binary form must reproduce the above copyright universe@599: * notice, this list of conditions and the following disclaimer in the universe@599: * documentation and/or other materials provided with the distribution. universe@599: * universe@599: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@599: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@599: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@599: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@599: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@599: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@599: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@599: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@599: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@599: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@599: * POSSIBILITY OF SUCH DAMAGE. universe@599: */ universe@599: universe@599: #include "cx/printf.h" universe@599: universe@599: #include universe@599: #include universe@599: universe@643: #ifndef CX_PRINTF_SBO_SIZE universe@643: #define CX_PRINTF_SBO_SIZE 512 universe@643: #endif universe@805: unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE; universe@599: universe@643: int cx_fprintf( universe@643: void *stream, universe@643: cx_write_func wfc, universe@643: char const *fmt, universe@643: ... universe@643: ) { universe@599: int ret; universe@599: va_list ap; universe@599: va_start(ap, fmt); universe@599: ret = cx_vfprintf(stream, wfc, fmt, ap); universe@599: va_end(ap); universe@599: return ret; universe@599: } universe@599: universe@644: int cx_vfprintf( universe@644: void *stream, universe@644: cx_write_func wfc, universe@644: char const *fmt, universe@644: va_list ap universe@644: ) { universe@643: char buf[CX_PRINTF_SBO_SIZE]; universe@599: va_list ap2; universe@599: va_copy(ap2, ap); universe@643: int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); universe@599: if (ret < 0) { universe@811: va_end(ap2); universe@599: return ret; universe@643: } else if (ret < CX_PRINTF_SBO_SIZE) { universe@811: va_end(ap2); universe@599: return (int) wfc(buf, 1, ret, stream); universe@599: } else { universe@599: int len = ret + 1; universe@599: char *newbuf = malloc(len); universe@599: if (!newbuf) { universe@811: va_end(ap2); universe@599: return -1; universe@599: } universe@599: universe@599: ret = vsnprintf(newbuf, len, fmt, ap2); universe@811: va_end(ap2); universe@599: if (ret > 0) { universe@599: ret = (int) wfc(newbuf, 1, ret, stream); universe@599: } universe@599: free(newbuf); universe@599: } universe@599: return ret; universe@599: } universe@599: universe@644: cxmutstr cx_asprintf_a( universe@693: CxAllocator const *allocator, universe@644: char const *fmt, universe@644: ... universe@644: ) { universe@599: va_list ap; universe@599: va_start(ap, fmt); universe@805: cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap); universe@599: va_end(ap); universe@599: return ret; universe@599: } universe@599: universe@644: cxmutstr cx_vasprintf_a( universe@693: CxAllocator const *a, universe@644: char const *fmt, universe@644: va_list ap universe@644: ) { universe@599: cxmutstr s; universe@599: s.ptr = NULL; universe@599: s.length = 0; universe@643: char buf[CX_PRINTF_SBO_SIZE]; universe@599: va_list ap2; universe@599: va_copy(ap2, ap); universe@643: int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); universe@812: if (ret >= 0 && ret < CX_PRINTF_SBO_SIZE) { universe@599: s.ptr = cxMalloc(a, ret + 1); universe@599: if (s.ptr) { universe@599: s.length = (size_t) ret; universe@599: memcpy(s.ptr, buf, ret); universe@599: s.ptr[s.length] = '\0'; universe@599: } universe@599: } else { universe@599: int len = ret + 1; universe@599: s.ptr = cxMalloc(a, len); universe@599: if (s.ptr) { universe@599: ret = vsnprintf(s.ptr, len, fmt, ap2); universe@599: if (ret < 0) { universe@599: free(s.ptr); universe@599: s.ptr = NULL; universe@599: } else { universe@599: s.length = (size_t) ret; universe@599: } universe@599: } universe@599: } universe@811: va_end(ap2); universe@599: return s; universe@599: } universe@599: universe@849: int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... ) { universe@810: va_list ap; universe@810: va_start(ap, fmt); universe@810: int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); universe@810: va_end(ap); universe@810: return ret; universe@810: } universe@810: universe@849: int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap) { universe@810: va_list ap2; universe@810: va_copy(ap2, ap); universe@849: int ret = vsnprintf(*str, *len, fmt, ap); universe@849: if ((unsigned) ret >= *len) { universe@810: unsigned newlen = ret + 1; universe@810: char *ptr = cxRealloc(alloc, *str, newlen); universe@810: if (ptr) { universe@810: int newret = vsnprintf(ptr, newlen, fmt, ap2); universe@810: if (newret < 0) { universe@810: cxFree(alloc, ptr); universe@810: } else { universe@849: *len = newlen; universe@810: *str = ptr; universe@813: ret = newret; universe@810: } universe@810: } universe@810: } universe@813: va_end(ap2); universe@813: return ret; universe@810: } universe@810: universe@849: int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ) { universe@810: va_list ap; universe@810: va_start(ap, fmt); universe@810: int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); universe@810: va_end(ap); universe@810: return ret; universe@810: } universe@810: universe@849: int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap) { universe@810: va_list ap2; universe@810: va_copy(ap2, ap); universe@849: int ret = vsnprintf(buf, *len, fmt, ap); universe@810: *str = buf; universe@849: if ((unsigned) ret >= *len) { universe@810: unsigned newlen = ret + 1; universe@810: char *ptr = cxMalloc(alloc, newlen); universe@810: if (ptr) { universe@810: int newret = vsnprintf(ptr, newlen, fmt, ap2); universe@810: if (newret < 0) { universe@810: cxFree(alloc, ptr); universe@810: } else { universe@849: *len = newlen; universe@810: *str = ptr; universe@813: ret = newret; universe@810: } universe@810: } universe@810: } universe@813: va_end(ap2); universe@813: return ret; universe@810: }