src/printf.c

Sun, 14 Jan 2024 13:13:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Jan 2024 13:13:12 +0100
changeset 805
26500fc24058
parent 693
494d9b20b99e
child 810
85859399a0cc
permissions
-rw-r--r--

add constant for reading out printf sbo size - relates to #343

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@805 37 unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE;
universe@599 38
universe@643 39 int cx_fprintf(
universe@643 40 void *stream,
universe@643 41 cx_write_func wfc,
universe@643 42 char const *fmt,
universe@643 43 ...
universe@643 44 ) {
universe@599 45 int ret;
universe@599 46 va_list ap;
universe@599 47 va_start(ap, fmt);
universe@599 48 ret = cx_vfprintf(stream, wfc, fmt, ap);
universe@599 49 va_end(ap);
universe@599 50 return ret;
universe@599 51 }
universe@599 52
universe@644 53 int cx_vfprintf(
universe@644 54 void *stream,
universe@644 55 cx_write_func wfc,
universe@644 56 char const *fmt,
universe@644 57 va_list ap
universe@644 58 ) {
universe@643 59 char buf[CX_PRINTF_SBO_SIZE];
universe@599 60 va_list ap2;
universe@599 61 va_copy(ap2, ap);
universe@643 62 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
universe@599 63 if (ret < 0) {
universe@599 64 return ret;
universe@643 65 } else if (ret < CX_PRINTF_SBO_SIZE) {
universe@599 66 return (int) wfc(buf, 1, ret, stream);
universe@599 67 } else {
universe@599 68 int len = ret + 1;
universe@599 69 char *newbuf = malloc(len);
universe@599 70 if (!newbuf) {
universe@599 71 return -1;
universe@599 72 }
universe@599 73
universe@599 74 ret = vsnprintf(newbuf, len, fmt, ap2);
universe@599 75 if (ret > 0) {
universe@599 76 ret = (int) wfc(newbuf, 1, ret, stream);
universe@599 77 }
universe@599 78 free(newbuf);
universe@599 79 }
universe@599 80 return ret;
universe@599 81 }
universe@599 82
universe@644 83 cxmutstr cx_asprintf_a(
universe@693 84 CxAllocator const *allocator,
universe@644 85 char const *fmt,
universe@644 86 ...
universe@644 87 ) {
universe@599 88 va_list ap;
universe@599 89 va_start(ap, fmt);
universe@805 90 cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap);
universe@599 91 va_end(ap);
universe@599 92 return ret;
universe@599 93 }
universe@599 94
universe@644 95 cxmutstr cx_vasprintf_a(
universe@693 96 CxAllocator const *a,
universe@644 97 char const *fmt,
universe@644 98 va_list ap
universe@644 99 ) {
universe@599 100 cxmutstr s;
universe@599 101 s.ptr = NULL;
universe@599 102 s.length = 0;
universe@643 103 char buf[CX_PRINTF_SBO_SIZE];
universe@599 104 va_list ap2;
universe@599 105 va_copy(ap2, ap);
universe@643 106 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
universe@643 107 if (ret > 0 && ret < CX_PRINTF_SBO_SIZE) {
universe@599 108 s.ptr = cxMalloc(a, ret + 1);
universe@599 109 if (s.ptr) {
universe@599 110 s.length = (size_t) ret;
universe@599 111 memcpy(s.ptr, buf, ret);
universe@599 112 s.ptr[s.length] = '\0';
universe@599 113 }
universe@599 114 } else {
universe@599 115 int len = ret + 1;
universe@599 116 s.ptr = cxMalloc(a, len);
universe@599 117 if (s.ptr) {
universe@599 118 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@599 119 if (ret < 0) {
universe@599 120 free(s.ptr);
universe@599 121 s.ptr = NULL;
universe@599 122 } else {
universe@599 123 s.length = (size_t) ret;
universe@599 124 }
universe@599 125 }
universe@599 126 }
universe@599 127 return s;
universe@599 128 }
universe@599 129

mercurial