src/printf.c

Wed, 01 Feb 2023 18:06:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Feb 2023 18:06:50 +0100
changeset 643
5700ba9154ab
parent 634
f78d3b77d456
child 644
fcaa0891ef28
permissions
-rw-r--r--

#228 make buffer sizes adjustable at compile time

universe@599 1 /*
universe@599 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@599 3 *
universe@599 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@599 5 *
universe@599 6 * Redistribution and use in source and binary forms, with or without
universe@599 7 * modification, are permitted provided that the following conditions are met:
universe@599 8 *
universe@599 9 * 1. Redistributions of source code must retain the above copyright
universe@599 10 * notice, this list of conditions and the following disclaimer.
universe@599 11 *
universe@599 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@599 13 * notice, this list of conditions and the following disclaimer in the
universe@599 14 * documentation and/or other materials provided with the distribution.
universe@599 15 *
universe@599 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@599 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@599 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@599 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@599 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@599 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@599 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@599 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@599 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@599 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@599 26 * POSSIBILITY OF SUCH DAMAGE.
universe@599 27 */
universe@599 28
universe@599 29 #include "cx/printf.h"
universe@599 30
universe@599 31 #include <stdio.h>
universe@599 32 #include <string.h>
universe@599 33
universe@643 34 #ifndef CX_PRINTF_SBO_SIZE
universe@643 35 #define CX_PRINTF_SBO_SIZE 512
universe@643 36 #endif
universe@599 37
universe@643 38 int cx_fprintf(
universe@643 39 void *stream,
universe@643 40 cx_write_func wfc,
universe@643 41 char const *fmt,
universe@643 42 ...
universe@643 43 ) {
universe@599 44 int ret;
universe@599 45 va_list ap;
universe@599 46 va_start(ap, fmt);
universe@599 47 ret = cx_vfprintf(stream, wfc, fmt, ap);
universe@599 48 va_end(ap);
universe@599 49 return ret;
universe@599 50 }
universe@599 51
universe@600 52 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) {
universe@643 53 char buf[CX_PRINTF_SBO_SIZE];
universe@599 54 va_list ap2;
universe@599 55 va_copy(ap2, ap);
universe@643 56 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
universe@599 57 if (ret < 0) {
universe@599 58 return ret;
universe@643 59 } else if (ret < CX_PRINTF_SBO_SIZE) {
universe@599 60 return (int) wfc(buf, 1, ret, stream);
universe@599 61 } else {
universe@599 62 int len = ret + 1;
universe@599 63 char *newbuf = malloc(len);
universe@599 64 if (!newbuf) {
universe@599 65 return -1;
universe@599 66 }
universe@599 67
universe@599 68 ret = vsnprintf(newbuf, len, fmt, ap2);
universe@599 69 if (ret > 0) {
universe@599 70 ret = (int) wfc(newbuf, 1, ret, stream);
universe@599 71 }
universe@599 72 free(newbuf);
universe@599 73 }
universe@599 74 return ret;
universe@599 75 }
universe@599 76
universe@600 77 cxmutstr cx_asprintf_a(CxAllocator *allocator, char const *fmt, ...) {
universe@599 78 va_list ap;
universe@599 79 cxmutstr ret;
universe@599 80 va_start(ap, fmt);
universe@599 81 ret = cx_vasprintf_a(allocator, fmt, ap);
universe@599 82 va_end(ap);
universe@599 83 return ret;
universe@599 84 }
universe@599 85
universe@600 86 cxmutstr cx_vasprintf_a(CxAllocator *a, char const *fmt, va_list ap) {
universe@599 87 cxmutstr s;
universe@599 88 s.ptr = NULL;
universe@599 89 s.length = 0;
universe@643 90 char buf[CX_PRINTF_SBO_SIZE];
universe@599 91 va_list ap2;
universe@599 92 va_copy(ap2, ap);
universe@643 93 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
universe@643 94 if (ret > 0 && ret < CX_PRINTF_SBO_SIZE) {
universe@599 95 s.ptr = cxMalloc(a, ret + 1);
universe@599 96 if (s.ptr) {
universe@599 97 s.length = (size_t) ret;
universe@599 98 memcpy(s.ptr, buf, ret);
universe@599 99 s.ptr[s.length] = '\0';
universe@599 100 }
universe@599 101 } else {
universe@599 102 int len = ret + 1;
universe@599 103 s.ptr = cxMalloc(a, len);
universe@599 104 if (s.ptr) {
universe@599 105 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@599 106 if (ret < 0) {
universe@599 107 free(s.ptr);
universe@599 108 s.ptr = NULL;
universe@599 109 } else {
universe@599 110 s.length = (size_t) ret;
universe@599 111 }
universe@599 112 }
universe@599 113 }
universe@599 114 return s;
universe@599 115 }
universe@599 116

mercurial