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@634: #define CX_PRINTF_BUFSIZE 256 universe@599: universe@600: int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...) { 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@600: int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) { universe@634: char buf[CX_PRINTF_BUFSIZE]; universe@599: va_list ap2; universe@599: va_copy(ap2, ap); universe@634: int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap); universe@599: if (ret < 0) { universe@599: return ret; universe@634: } else if (ret < CX_PRINTF_BUFSIZE) { 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@599: return -1; universe@599: } universe@599: universe@599: ret = vsnprintf(newbuf, len, fmt, 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@600: cxmutstr cx_asprintf_a(CxAllocator *allocator, char const *fmt, ...) { universe@599: va_list ap; universe@599: cxmutstr ret; universe@599: va_start(ap, fmt); universe@599: ret = cx_vasprintf_a(allocator, fmt, ap); universe@599: va_end(ap); universe@599: return ret; universe@599: } universe@599: universe@600: cxmutstr cx_vasprintf_a(CxAllocator *a, char const *fmt, va_list ap) { universe@599: cxmutstr s; universe@599: s.ptr = NULL; universe@599: s.length = 0; universe@634: char buf[CX_PRINTF_BUFSIZE]; universe@599: va_list ap2; universe@599: va_copy(ap2, ap); universe@634: int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap); universe@634: if (ret > 0 && ret < CX_PRINTF_BUFSIZE) { 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@599: return s; universe@599: } universe@599: