Fri, 29 Dec 2023 17:17:43 +0100
revert the insanely stupid idea to automatically run the test program on default build
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@644 | 52 | int cx_vfprintf( |
universe@644 | 53 | void *stream, |
universe@644 | 54 | cx_write_func wfc, |
universe@644 | 55 | char const *fmt, |
universe@644 | 56 | va_list ap |
universe@644 | 57 | ) { |
universe@643 | 58 | char buf[CX_PRINTF_SBO_SIZE]; |
universe@599 | 59 | va_list ap2; |
universe@599 | 60 | va_copy(ap2, ap); |
universe@643 | 61 | int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); |
universe@599 | 62 | if (ret < 0) { |
universe@599 | 63 | return ret; |
universe@643 | 64 | } else if (ret < CX_PRINTF_SBO_SIZE) { |
universe@599 | 65 | return (int) wfc(buf, 1, ret, stream); |
universe@599 | 66 | } else { |
universe@599 | 67 | int len = ret + 1; |
universe@599 | 68 | char *newbuf = malloc(len); |
universe@599 | 69 | if (!newbuf) { |
universe@599 | 70 | return -1; |
universe@599 | 71 | } |
universe@599 | 72 | |
universe@599 | 73 | ret = vsnprintf(newbuf, len, fmt, ap2); |
universe@599 | 74 | if (ret > 0) { |
universe@599 | 75 | ret = (int) wfc(newbuf, 1, ret, stream); |
universe@599 | 76 | } |
universe@599 | 77 | free(newbuf); |
universe@599 | 78 | } |
universe@599 | 79 | return ret; |
universe@599 | 80 | } |
universe@599 | 81 | |
universe@644 | 82 | cxmutstr cx_asprintf_a( |
universe@693 | 83 | CxAllocator const *allocator, |
universe@644 | 84 | char const *fmt, |
universe@644 | 85 | ... |
universe@644 | 86 | ) { |
universe@599 | 87 | va_list ap; |
universe@599 | 88 | cxmutstr ret; |
universe@599 | 89 | va_start(ap, fmt); |
universe@599 | 90 | 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 |