| 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 (2→3) not taken.
✓ Branch 1 (2→4) taken 16 times.
|
16 | if (ret < 0) { |
| 64 | // LCOV_EXCL_START | ||
| 65 | − | va_end(ap2); | |
| 66 | − | return ret; | |
| 67 | // LCOV_EXCL_STOP | ||
| 68 |
2/2✓ Branch 0 (4→5) taken 15 times.
✓ Branch 1 (4→7) taken 1 times.
|
16 | } else if (ret < CX_PRINTF_SBO_SIZE) { |
| 69 | 15 | va_end(ap2); | |
| 70 | 15 | return (int) wfc(buf, 1, ret, stream); | |
| 71 | } else { | ||
| 72 | 1 | int len = ret + 1; | |
| 73 | 1 | char *newbuf = cxMallocDefault(len); | |
| 74 | − | if (!newbuf) { // LCOV_EXCL_START | |
| 75 | − | va_end(ap2); | |
| 76 | − | return -1; | |
| 77 | } // LCOV_EXCL_STOP | ||
| 78 | |||
| 79 | 1 | ret = vsnprintf(newbuf, len, fmt, ap2); | |
| 80 | 1 | va_end(ap2); | |
| 81 |
1/2✓ Branch 0 (10→11) taken 1 times.
✗ Branch 1 (10→13) not taken.
|
1 | if (ret > 0) { |
| 82 | 1 | ret = (int) wfc(newbuf, 1, ret, stream); | |
| 83 | } | ||
| 84 | 1 | cxFreeDefault(newbuf); | |
| 85 | } | ||
| 86 | 1 | return ret; | |
| 87 | } | ||
| 88 | |||
| 89 | 14 | cxmutstr cx_asprintf_a( | |
| 90 | const CxAllocator *allocator, | ||
| 91 | const char *fmt, | ||
| 92 | ... | ||
| 93 | ) { | ||
| 94 | va_list ap; | ||
| 95 | 14 | va_start(ap, fmt); | |
| 96 | 14 | cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap); | |
| 97 | 14 | va_end(ap); | |
| 98 | 14 | return ret; | |
| 99 | } | ||
| 100 | |||
| 101 | 14 | cxmutstr cx_vasprintf_a( | |
| 102 | const CxAllocator *a, | ||
| 103 | const char *fmt, | ||
| 104 | va_list ap | ||
| 105 | ) { | ||
| 106 | cxmutstr s; | ||
| 107 | 14 | s.ptr = NULL; | |
| 108 | 14 | s.length = 0; | |
| 109 | char buf[CX_PRINTF_SBO_SIZE]; | ||
| 110 | va_list ap2; | ||
| 111 | 14 | va_copy(ap2, ap); | |
| 112 | 14 | int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); | |
| 113 |
3/4✓ Branch 0 (2→3) taken 14 times.
✗ Branch 1 (2→8) not taken.
✓ Branch 2 (3→4) taken 13 times.
✓ Branch 3 (3→8) taken 1 times.
|
14 | if (ret >= 0 && ret < CX_PRINTF_SBO_SIZE) { |
| 114 | 13 | s.ptr = cxMalloc(a, ret + 1); | |
| 115 |
1/2✓ Branch 0 (5→6) taken 13 times.
✗ Branch 1 (5→7) not taken.
|
13 | if (s.ptr) { |
| 116 | 13 | s.length = (size_t) ret; | |
| 117 | 13 | memcpy(s.ptr, buf, ret); | |
| 118 | 13 | s.ptr[s.length] = '\0'; | |
| 119 | } | ||
| 120 | } else { | ||
| 121 | 1 | int len = ret + 1; | |
| 122 | 1 | s.ptr = cxMalloc(a, len); | |
| 123 |
1/2✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→14) not taken.
|
1 | if (s.ptr) { |
| 124 | 1 | ret = vsnprintf(s.ptr, len, fmt, ap2); | |
| 125 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 1 times.
|
1 | if (ret < 0) { |
| 126 | // LCOV_EXCL_START | ||
| 127 | − | cxFree(a, s.ptr); | |
| 128 | − | s.ptr = NULL; | |
| 129 | // LCOV_EXCL_STOP | ||
| 130 | } else { | ||
| 131 | 1 | s.length = (size_t) ret; | |
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | 14 | va_end(ap2); | |
| 136 | 14 | return s; | |
| 137 | } | ||
| 138 | |||
| 139 | 3 | int cx_sprintf_a( | |
| 140 | const CxAllocator *alloc, | ||
| 141 | char **str, | ||
| 142 | size_t *len, | ||
| 143 | const char *fmt, | ||
| 144 | ... | ||
| 145 | ) { | ||
| 146 | va_list ap; | ||
| 147 | 3 | va_start(ap, fmt); | |
| 148 | 3 | int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); | |
| 149 | 3 | va_end(ap); | |
| 150 | 3 | return ret; | |
| 151 | } | ||
| 152 | |||
| 153 | 3 | int cx_vsprintf_a( | |
| 154 | const CxAllocator *alloc, | ||
| 155 | char **str, | ||
| 156 | size_t *len, | ||
| 157 | const char *fmt, | ||
| 158 | va_list ap | ||
| 159 | ) { | ||
| 160 | va_list ap2; | ||
| 161 | 3 | va_copy(ap2, ap); | |
| 162 | 3 | int ret = vsnprintf(*str, *len, fmt, ap); | |
| 163 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→8) taken 1 times.
|
3 | if ((unsigned) ret >= *len) { |
| 164 | 2 | unsigned newlen = ret + 1; | |
| 165 | 2 | char *ptr = cxRealloc(alloc, *str, newlen); | |
| 166 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→8) not taken.
|
2 | if (ptr) { |
| 167 | 2 | int newret = vsnprintf(ptr, newlen, fmt, ap2); | |
| 168 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 2 times.
|
2 | if (newret < 0) { |
| 169 | − | cxFree(alloc, ptr); // LCOV_EXCL_LINE | |
| 170 | } else { | ||
| 171 | 2 | *len = newlen; | |
| 172 | 2 | *str = ptr; | |
| 173 | 2 | ret = newret; | |
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | 3 | va_end(ap2); | |
| 178 | 3 | return ret; | |
| 179 | } | ||
| 180 | |||
| 181 | 3 | int cx_sprintf_sa( | |
| 182 | const CxAllocator *alloc, | ||
| 183 | char *buf, | ||
| 184 | size_t *len, | ||
| 185 | char **str, | ||
| 186 | const char *fmt, | ||
| 187 | ... | ||
| 188 | ) { | ||
| 189 | va_list ap; | ||
| 190 | 3 | va_start(ap, fmt); | |
| 191 | 3 | int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); | |
| 192 | 3 | va_end(ap); | |
| 193 | 3 | return ret; | |
| 194 | } | ||
| 195 | |||
| 196 | 3 | int cx_vsprintf_sa( | |
| 197 | const CxAllocator *alloc, | ||
| 198 | char *buf, | ||
| 199 | size_t *len, | ||
| 200 | char **str, | ||
| 201 | const char *fmt, | ||
| 202 | va_list ap | ||
| 203 | ) { | ||
| 204 | va_list ap2; | ||
| 205 | 3 | va_copy(ap2, ap); | |
| 206 | 3 | int ret = vsnprintf(buf, *len, fmt, ap); | |
| 207 | 3 | *str = buf; | |
| 208 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→8) taken 1 times.
|
3 | if ((unsigned) ret >= *len) { |
| 209 | 2 | unsigned newlen = ret + 1; | |
| 210 | 2 | char *ptr = cxMalloc(alloc, newlen); | |
| 211 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→8) not taken.
|
2 | if (ptr) { |
| 212 | 2 | int newret = vsnprintf(ptr, newlen, fmt, ap2); | |
| 213 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 2 times.
|
2 | if (newret < 0) { |
| 214 | − | cxFree(alloc, ptr); // LCOV_EXCL_LINE | |
| 215 | } else { | ||
| 216 | 2 | *len = newlen; | |
| 217 | 2 | *str = ptr; | |
| 218 | 2 | ret = newret; | |
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | 3 | va_end(ap2); | |
| 223 | 3 | return ret; | |
| 224 | } | ||
| 225 |