| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/printf.h" | ||
| 30 | |||
| 31 | #include <stdio.h> | ||
| 32 | #include <string.h> | ||
| 33 | |||
| 34 | #ifndef CX_PRINTF_SBO_SIZE | ||
| 35 | #define CX_PRINTF_SBO_SIZE 512 | ||
| 36 | #endif | ||
| 37 | const unsigned cx_printf_sbo_size = CX_PRINTF_SBO_SIZE; | ||
| 38 | |||
| 39 | 16 | int cx_fprintf( | |
| 40 | void *stream, | ||
| 41 | cx_write_func wfc, | ||
| 42 | const char *fmt, | ||
| 43 | ... | ||
| 44 | ) { | ||
| 45 | int ret; | ||
| 46 | va_list ap; | ||
| 47 | 16 | va_start(ap, fmt); | |
| 48 | 16 | ret = cx_vfprintf(stream, wfc, fmt, ap); | |
| 49 | 16 | va_end(ap); | |
| 50 | 16 | return ret; | |
| 51 | } | ||
| 52 | |||
| 53 | 16 | int cx_vfprintf( | |
| 54 | void *stream, | ||
| 55 | cx_write_func wfc, | ||
| 56 | const char *fmt, | ||
| 57 | va_list ap | ||
| 58 | ) { | ||
| 59 | char buf[CX_PRINTF_SBO_SIZE]; | ||
| 60 | va_list ap2; | ||
| 61 | 16 | va_copy(ap2, ap); | |
| 62 | 16 | int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); | |
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (ret < 0) { |
| 64 | ✗ | va_end(ap2); | |
| 65 | ✗ | return ret; | |
| 66 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
|
16 | } else if (ret < CX_PRINTF_SBO_SIZE) { |
| 67 | 15 | va_end(ap2); | |
| 68 | 15 | return (int) wfc(buf, 1, ret, stream); | |
| 69 | } else { | ||
| 70 | 1 | int len = ret + 1; | |
| 71 | 1 | char *newbuf = malloc(len); | |
| 72 | − | if (!newbuf) { // LCOV_EXCL_START | |
| 73 | − | va_end(ap2); | |
| 74 | − | return -1; | |
| 75 | } // LCOV_EXCL_STOP | ||
| 76 | |||
| 77 | 1 | ret = vsnprintf(newbuf, len, fmt, ap2); | |
| 78 | 1 | va_end(ap2); | |
| 79 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ret > 0) { |
| 80 | 1 | ret = (int) wfc(newbuf, 1, ret, stream); | |
| 81 | } | ||
| 82 | 1 | free(newbuf); | |
| 83 | } | ||
| 84 | 1 | return ret; | |
| 85 | } | ||
| 86 | |||
| 87 | 14 | cxmutstr cx_asprintf_a( | |
| 88 | const CxAllocator *allocator, | ||
| 89 | const char *fmt, | ||
| 90 | ... | ||
| 91 | ) { | ||
| 92 | va_list ap; | ||
| 93 | 14 | va_start(ap, fmt); | |
| 94 | 14 | cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap); | |
| 95 | 14 | va_end(ap); | |
| 96 | 14 | return ret; | |
| 97 | } | ||
| 98 | |||
| 99 | 14 | cxmutstr cx_vasprintf_a( | |
| 100 | const CxAllocator *a, | ||
| 101 | const char *fmt, | ||
| 102 | va_list ap | ||
| 103 | ) { | ||
| 104 | cxmutstr s; | ||
| 105 | 14 | s.ptr = NULL; | |
| 106 | 14 | s.length = 0; | |
| 107 | char buf[CX_PRINTF_SBO_SIZE]; | ||
| 108 | va_list ap2; | ||
| 109 | 14 | va_copy(ap2, ap); | |
| 110 | 14 | int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); | |
| 111 |
3/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
|
14 | if (ret >= 0 && ret < CX_PRINTF_SBO_SIZE) { |
| 112 | 13 | s.ptr = cxMalloc(a, ret + 1); | |
| 113 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if (s.ptr) { |
| 114 | 13 | s.length = (size_t) ret; | |
| 115 | 13 | memcpy(s.ptr, buf, ret); | |
| 116 | 13 | s.ptr[s.length] = '\0'; | |
| 117 | } | ||
| 118 | } else { | ||
| 119 | 1 | int len = ret + 1; | |
| 120 | 1 | s.ptr = cxMalloc(a, len); | |
| 121 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s.ptr) { |
| 122 | 1 | ret = vsnprintf(s.ptr, len, fmt, ap2); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 124 | ✗ | free(s.ptr); | |
| 125 | ✗ | s.ptr = NULL; | |
| 126 | } else { | ||
| 127 | 1 | s.length = (size_t) ret; | |
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | 14 | va_end(ap2); | |
| 132 | 14 | return s; | |
| 133 | } | ||
| 134 | |||
| 135 | 3 | int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... ) { | |
| 136 | va_list ap; | ||
| 137 | 3 | va_start(ap, fmt); | |
| 138 | 3 | int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); | |
| 139 | 3 | va_end(ap); | |
| 140 | 3 | return ret; | |
| 141 | } | ||
| 142 | |||
| 143 | 3 | int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap) { | |
| 144 | va_list ap2; | ||
| 145 | 3 | va_copy(ap2, ap); | |
| 146 | 3 | int ret = vsnprintf(*str, *len, fmt, ap); | |
| 147 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if ((unsigned) ret >= *len) { |
| 148 | 2 | unsigned newlen = ret + 1; | |
| 149 | 2 | char *ptr = cxRealloc(alloc, *str, newlen); | |
| 150 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ptr) { |
| 151 | 2 | int newret = vsnprintf(ptr, newlen, fmt, ap2); | |
| 152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (newret < 0) { |
| 153 | ✗ | cxFree(alloc, ptr); | |
| 154 | } else { | ||
| 155 | 2 | *len = newlen; | |
| 156 | 2 | *str = ptr; | |
| 157 | 2 | ret = newret; | |
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | 3 | va_end(ap2); | |
| 162 | 3 | return ret; | |
| 163 | } | ||
| 164 | |||
| 165 | 3 | int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ) { | |
| 166 | va_list ap; | ||
| 167 | 3 | va_start(ap, fmt); | |
| 168 | 3 | int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); | |
| 169 | 3 | va_end(ap); | |
| 170 | 3 | return ret; | |
| 171 | } | ||
| 172 | |||
| 173 | 3 | int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap) { | |
| 174 | va_list ap2; | ||
| 175 | 3 | va_copy(ap2, ap); | |
| 176 | 3 | int ret = vsnprintf(buf, *len, fmt, ap); | |
| 177 | 3 | *str = buf; | |
| 178 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if ((unsigned) ret >= *len) { |
| 179 | 2 | unsigned newlen = ret + 1; | |
| 180 | 2 | char *ptr = cxMalloc(alloc, newlen); | |
| 181 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ptr) { |
| 182 | 2 | int newret = vsnprintf(ptr, newlen, fmt, ap2); | |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (newret < 0) { |
| 184 | ✗ | cxFree(alloc, ptr); | |
| 185 | } else { | ||
| 186 | 2 | *len = newlen; | |
| 187 | 2 | *str = ptr; | |
| 188 | 2 | ret = newret; | |
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | 3 | va_end(ap2); | |
| 193 | 3 | return ret; | |
| 194 | } | ||
| 195 |