Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2008, Atmel Corporation |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame^] | 5 | * Copyright (c) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 6 | * |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
| 12 | * - Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the disclaimer below. |
| 14 | * |
| 15 | * Atmel's name may not be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 24 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 27 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * ---------------------------------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | //------------------------------------------------------------------------------ |
| 32 | /// \unit |
| 33 | /// |
| 34 | /// !Purpose |
| 35 | /// |
| 36 | /// Implementation of several stdio.h methods, such as printf(), sprintf() and |
| 37 | /// so on. This reduces the memory footprint of the binary when using those |
| 38 | /// methods, compared to the libc implementation. |
| 39 | /// |
| 40 | /// !Usage |
| 41 | /// |
| 42 | /// Adds stdio.c to the list of file to compile for the project. This will |
| 43 | /// automatically replace libc methods by the custom ones. |
| 44 | //------------------------------------------------------------------------------ |
| 45 | |
| 46 | //------------------------------------------------------------------------------ |
| 47 | // Headers |
| 48 | //------------------------------------------------------------------------------ |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdarg.h> |
| 52 | |
| 53 | //------------------------------------------------------------------------------ |
| 54 | // Local Definitions |
| 55 | //------------------------------------------------------------------------------ |
| 56 | |
| 57 | // Maximum string size allowed (in bytes). |
Harald Welte | ee9ebb3 | 2017-03-02 16:52:08 +0100 | [diff] [blame] | 58 | #define MAX_STRING_SIZE 512 |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 59 | |
| 60 | //------------------------------------------------------------------------------ |
| 61 | // Global Variables |
| 62 | //------------------------------------------------------------------------------ |
Harald Welte | c430ac1 | 2017-02-28 01:25:12 +0100 | [diff] [blame] | 63 | // |
| 64 | FILE* const stdin = NULL; |
| 65 | FILE* const stdout = NULL; |
| 66 | FILE* const stderr = NULL; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 67 | |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 68 | |
| 69 | //------------------------------------------------------------------------------ |
| 70 | // Local Functions |
| 71 | //------------------------------------------------------------------------------ |
| 72 | |
| 73 | //------------------------------------------------------------------------------ |
| 74 | // Writes a character inside the given string. Returns 1. |
| 75 | // \param pStr Storage string. |
| 76 | // \param c Character to write. |
| 77 | //------------------------------------------------------------------------------ |
| 78 | signed int PutChar(char *pStr, char c) |
| 79 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 80 | *pStr = c; |
| 81 | return 1; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //------------------------------------------------------------------------------ |
| 85 | // Writes a string inside the given string. |
| 86 | // Returns the size of the written |
| 87 | // string. |
| 88 | // \param pStr Storage string. |
| 89 | // \param pSource Source string. |
| 90 | //------------------------------------------------------------------------------ |
| 91 | signed int PutString(char *pStr, const char *pSource) |
| 92 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 93 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 94 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 95 | while (*pSource != 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 96 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 97 | *pStr++ = *pSource++; |
| 98 | num++; |
| 99 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 100 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 101 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | //------------------------------------------------------------------------------ |
| 105 | // Writes an unsigned int inside the given string, using the provided fill & |
| 106 | // width parameters. |
| 107 | // Returns the size in characters of the written integer. |
| 108 | // \param pStr Storage string. |
| 109 | // \param fill Fill character. |
| 110 | // \param width Minimum integer width. |
| 111 | // \param value Integer value. |
| 112 | //------------------------------------------------------------------------------ |
| 113 | signed int PutUnsignedInt( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 114 | char *pStr, |
| 115 | char fill, |
| 116 | signed int width, |
| 117 | unsigned int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 118 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 119 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 120 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 121 | // Take current digit into account when calculating width |
| 122 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 123 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 124 | // Recursively write upper digits |
| 125 | if ((value / 10) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 126 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 127 | num = PutUnsignedInt(pStr, fill, width, value / 10); |
| 128 | pStr += num; |
| 129 | } |
| 130 | // Write filler characters |
| 131 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 132 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 133 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 134 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 135 | PutChar(pStr, fill); |
| 136 | pStr++; |
| 137 | num++; |
| 138 | width--; |
| 139 | } |
| 140 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 141 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 142 | // Write lower digit |
| 143 | num += PutChar(pStr, (value % 10) + '0'); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 144 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 145 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | //------------------------------------------------------------------------------ |
| 149 | // Writes a signed int inside the given string, using the provided fill & width |
| 150 | // parameters. |
| 151 | // Returns the size of the written integer. |
| 152 | // \param pStr Storage string. |
| 153 | // \param fill Fill character. |
| 154 | // \param width Minimum integer width. |
| 155 | // \param value Signed integer value. |
| 156 | //------------------------------------------------------------------------------ |
| 157 | signed int PutSignedInt( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 158 | char *pStr, |
| 159 | char fill, |
| 160 | signed int width, |
| 161 | signed int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 162 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 163 | signed int num = 0; |
| 164 | unsigned int absolute; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 165 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 166 | // Compute absolute value |
| 167 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 168 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 169 | absolute = -value; |
| 170 | } |
| 171 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 172 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 173 | absolute = value; |
| 174 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 175 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 176 | // Take current digit into account when calculating width |
| 177 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 178 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 179 | // Recursively write upper digits |
| 180 | if ((absolute / 10) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 181 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 182 | if (value < 0) { |
| 183 | |
| 184 | num = PutSignedInt(pStr, fill, width, -(absolute / 10)); |
| 185 | } |
| 186 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 187 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 188 | num = PutSignedInt(pStr, fill, width, absolute / 10); |
| 189 | } |
| 190 | pStr += num; |
| 191 | } |
| 192 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 193 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 194 | // Reserve space for sign |
| 195 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 196 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 197 | width--; |
| 198 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 199 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 200 | // Write filler characters |
| 201 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 202 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 203 | PutChar(pStr, fill); |
| 204 | pStr++; |
| 205 | num++; |
| 206 | width--; |
| 207 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 208 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 209 | // Write sign |
| 210 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 211 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 212 | num += PutChar(pStr, '-'); |
| 213 | pStr++; |
| 214 | } |
| 215 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 216 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 217 | // Write lower digit |
| 218 | num += PutChar(pStr, (absolute % 10) + '0'); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 219 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 220 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | //------------------------------------------------------------------------------ |
| 224 | // Writes an hexadecimal value into a string, using the given fill, width & |
| 225 | // capital parameters. |
| 226 | // Returns the number of char written. |
| 227 | // \param pStr Storage string. |
| 228 | // \param fill Fill character. |
| 229 | // \param width Minimum integer width. |
| 230 | // \param maj Indicates if the letters must be printed in lower- or upper-case. |
| 231 | // \param value Hexadecimal value. |
| 232 | //------------------------------------------------------------------------------ |
| 233 | signed int PutHexa( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 234 | char *pStr, |
| 235 | char fill, |
| 236 | signed int width, |
| 237 | unsigned char maj, |
| 238 | unsigned int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 239 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 240 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 241 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 242 | // Decrement width |
| 243 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 244 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 245 | // Recursively output upper digits |
| 246 | if ((value >> 4) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 247 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 248 | num += PutHexa(pStr, fill, width, maj, value >> 4); |
| 249 | pStr += num; |
| 250 | } |
| 251 | // Write filler chars |
| 252 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 253 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 254 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 255 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 256 | PutChar(pStr, fill); |
| 257 | pStr++; |
| 258 | num++; |
| 259 | width--; |
| 260 | } |
| 261 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 262 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 263 | // Write current digit |
| 264 | if ((value & 0xF) < 10) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 265 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 266 | PutChar(pStr, (value & 0xF) + '0'); |
| 267 | } |
| 268 | else if (maj) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 269 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 270 | PutChar(pStr, (value & 0xF) - 10 + 'A'); |
| 271 | } |
| 272 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 273 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 274 | PutChar(pStr, (value & 0xF) - 10 + 'a'); |
| 275 | } |
| 276 | num++; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 277 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 278 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | //------------------------------------------------------------------------------ |
| 282 | // Global Functions |
| 283 | //------------------------------------------------------------------------------ |
| 284 | |
| 285 | //------------------------------------------------------------------------------ |
| 286 | /// Stores the result of a formatted string into another string. Format |
| 287 | /// arguments are given in a va_list instance. |
| 288 | /// Return the number of characters written. |
| 289 | /// \param pStr Destination string. |
| 290 | /// \param length Length of Destination string. |
| 291 | /// \param pFormat Format string. |
| 292 | /// \param ap Argument list. |
| 293 | //------------------------------------------------------------------------------ |
| 294 | signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap) |
| 295 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 296 | char fill; |
| 297 | unsigned char width; |
| 298 | signed int num = 0; |
| 299 | size_t size = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 300 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 301 | // Clear the string |
| 302 | if (pStr) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 303 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 304 | *pStr = 0; |
| 305 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 306 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 307 | // Phase string |
| 308 | while (*pFormat != 0 && size < length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 309 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 310 | // Normal character |
| 311 | if (*pFormat != '%') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 312 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 313 | *pStr++ = *pFormat++; |
| 314 | size++; |
| 315 | } |
| 316 | // Escaped '%' |
| 317 | else if (*(pFormat+1) == '%') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 318 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 319 | *pStr++ = '%'; |
| 320 | pFormat += 2; |
| 321 | size++; |
| 322 | } |
| 323 | // Token delimiter |
| 324 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 325 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 326 | fill = ' '; |
| 327 | width = 0; |
| 328 | pFormat++; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 329 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 330 | // Parse filler |
| 331 | if (*pFormat == '0') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 332 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 333 | fill = '0'; |
| 334 | pFormat++; |
| 335 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 336 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 337 | // Parse width |
| 338 | while ((*pFormat >= '0') && (*pFormat <= '9')) { |
| 339 | |
| 340 | width = (width*10) + *pFormat-'0'; |
| 341 | pFormat++; |
| 342 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 343 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 344 | // Check if there is enough space |
| 345 | if (size + width > length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 346 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 347 | width = length - size; |
| 348 | } |
| 349 | |
| 350 | // Parse type |
| 351 | switch (*pFormat) { |
| 352 | case 'd': |
| 353 | case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break; |
| 354 | case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break; |
| 355 | case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break; |
| 356 | case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break; |
| 357 | case 's': num = PutString(pStr, va_arg(ap, char *)); break; |
| 358 | case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break; |
| 359 | default: |
| 360 | return EOF; |
| 361 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 362 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 363 | pFormat++; |
| 364 | pStr += num; |
| 365 | size += num; |
| 366 | } |
| 367 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 368 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 369 | // NULL-terminated (final \0 is not counted) |
| 370 | if (size < length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 371 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 372 | *pStr = 0; |
| 373 | } |
| 374 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 375 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 376 | *(--pStr) = 0; |
| 377 | size--; |
| 378 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 379 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 380 | return size; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | //------------------------------------------------------------------------------ |
| 384 | /// Stores the result of a formatted string into another string. Format |
| 385 | /// arguments are given in a va_list instance. |
| 386 | /// Return the number of characters written. |
| 387 | /// \param pString Destination string. |
| 388 | /// \param length Length of Destination string. |
| 389 | /// \param pFormat Format string. |
| 390 | /// \param ... Other arguments |
| 391 | //------------------------------------------------------------------------------ |
| 392 | signed int snprintf(char *pString, size_t length, const char *pFormat, ...) |
| 393 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 394 | va_list ap; |
| 395 | signed int rc; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 396 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 397 | va_start(ap, pFormat); |
| 398 | rc = vsnprintf(pString, length, pFormat, ap); |
| 399 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 400 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 401 | return rc; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | //------------------------------------------------------------------------------ |
| 405 | /// Stores the result of a formatted string into another string. Format |
| 406 | /// arguments are given in a va_list instance. |
| 407 | /// Return the number of characters written. |
| 408 | /// \param pString Destination string. |
| 409 | /// \param pFormat Format string. |
| 410 | /// \param ap Argument list. |
| 411 | //------------------------------------------------------------------------------ |
| 412 | signed int vsprintf(char *pString, const char *pFormat, va_list ap) |
| 413 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 414 | return vsnprintf(pString, MAX_STRING_SIZE, pFormat, ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | //------------------------------------------------------------------------------ |
| 418 | /// Outputs a formatted string on the given stream. Format arguments are given |
| 419 | /// in a va_list instance. |
| 420 | /// \param pStream Output stream. |
| 421 | /// \param pFormat Format string |
| 422 | /// \param ap Argument list. |
| 423 | //------------------------------------------------------------------------------ |
| 424 | signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap) |
| 425 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 426 | char pStr[MAX_STRING_SIZE]; |
| 427 | char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r"; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 428 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 429 | // Write formatted string in buffer |
| 430 | if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 431 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 432 | fputs(pError, stderr); |
| 433 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 434 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 435 | // Display string |
| 436 | return fputs(pStr, pStream); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame^] | 440 | /// Outputs a formatted string on the given stream. Format arguments are given |
| 441 | /// in a va_list instance. |
| 442 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 443 | /// \param pStream Output stream. |
| 444 | /// \param pFormat Format string |
| 445 | /// \param ap Argument list. |
| 446 | //------------------------------------------------------------------------------ |
| 447 | #pragma GCC diagnostic push |
| 448 | #pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| 449 | signed int vfprintf_sync(FILE *pStream, const char *pFormat, va_list ap) |
| 450 | { |
| 451 | char pStr[MAX_STRING_SIZE]; |
| 452 | char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r"; |
| 453 | |
| 454 | // Write formatted string in buffer |
| 455 | if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) { |
| 456 | |
| 457 | fputs_sync(pError, stderr); |
| 458 | } |
| 459 | |
| 460 | // Display string |
| 461 | return fputs_sync(pStr, pStream); |
| 462 | } |
| 463 | #pragma GCC diagnostic pop |
| 464 | |
| 465 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 466 | /// Outputs a formatted string on the DBGU stream. Format arguments are given |
| 467 | /// in a va_list instance. |
| 468 | /// \param pFormat Format string |
| 469 | /// \param ap Argument list. |
| 470 | //------------------------------------------------------------------------------ |
| 471 | signed int vprintf(const char *pFormat, va_list ap) |
| 472 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 473 | return vfprintf(stdout, pFormat, ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame^] | 477 | /// Outputs a formatted string on the DBGU stream. Format arguments are given |
| 478 | /// in a va_list instance. |
| 479 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 480 | /// \param pFormat Format string |
| 481 | /// \param ap Argument list. |
| 482 | //------------------------------------------------------------------------------ |
| 483 | signed int vprintf_sync(const char *pFormat, va_list ap) |
| 484 | { |
| 485 | return vfprintf_sync(stdout, pFormat, ap); |
| 486 | } |
| 487 | |
| 488 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 489 | /// Outputs a formatted string on the given stream, using a variable number of |
| 490 | /// arguments. |
| 491 | /// \param pStream Output stream. |
| 492 | /// \param pFormat Format string. |
| 493 | //------------------------------------------------------------------------------ |
| 494 | signed int fprintf(FILE *pStream, const char *pFormat, ...) |
| 495 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 496 | va_list ap; |
| 497 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 498 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 499 | // Forward call to vfprintf |
| 500 | va_start(ap, pFormat); |
| 501 | result = vfprintf(pStream, pFormat, ap); |
| 502 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 503 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 504 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | //------------------------------------------------------------------------------ |
| 508 | /// Outputs a formatted string on the DBGU stream, using a variable number of |
| 509 | /// arguments. |
| 510 | /// \param pFormat Format string. |
| 511 | //------------------------------------------------------------------------------ |
| 512 | signed int printf(const char *pFormat, ...) |
| 513 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 514 | va_list ap; |
| 515 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 516 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 517 | // Forward call to vprintf |
| 518 | va_start(ap, pFormat); |
| 519 | result = vprintf(pFormat, ap); |
| 520 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 521 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 522 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame^] | 526 | /// Outputs a formatted string on the DBGU stream, using a variable number of |
| 527 | /// arguments. |
| 528 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 529 | /// \param pFormat Format string. |
| 530 | //------------------------------------------------------------------------------ |
| 531 | signed int printf_sync(const char *pFormat, ...) |
| 532 | { |
| 533 | va_list ap; |
| 534 | signed int result; |
| 535 | |
| 536 | // Forward call to vprintf |
| 537 | va_start(ap, pFormat); |
| 538 | result = vprintf_sync(pFormat, ap); |
| 539 | va_end(ap); |
| 540 | |
| 541 | return result; |
| 542 | } |
| 543 | |
| 544 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 545 | /// Writes a formatted string inside another string. |
| 546 | /// \param pStr Storage string. |
| 547 | /// \param pFormat Format string. |
| 548 | //------------------------------------------------------------------------------ |
| 549 | signed int sprintf(char *pStr, const char *pFormat, ...) |
| 550 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 551 | va_list ap; |
| 552 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 553 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 554 | // Forward call to vsprintf |
| 555 | va_start(ap, pFormat); |
| 556 | result = vsprintf(pStr, pFormat, ap); |
| 557 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 558 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 559 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | //------------------------------------------------------------------------------ |
| 563 | /// Outputs a string on stdout. |
| 564 | /// \param pStr String to output. |
| 565 | //------------------------------------------------------------------------------ |
| 566 | signed int puts(const char *pStr) |
| 567 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 568 | return fputs(pStr, stdout); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 569 | } |
| 570 | |