Rev 2131 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 974 | cejka | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2001-2004 Jakub Jermar |
| 3 | * Copyright (c) 2006 Josef Cejka |
||
| 974 | cejka | 4 | * 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 |
||
| 8 | * are met: |
||
| 9 | * |
||
| 10 | * - Redistributions of source code must retain the above copyright |
||
| 11 | * notice, this list of conditions and the following disclaimer. |
||
| 12 | * - 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 | * - The name of the author may not be used to endorse or promote products |
||
| 16 | * derived from this software without specific prior written permission. |
||
| 17 | * |
||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 28 | */ |
||
| 29 | |||
| 1866 | jermar | 30 | /** @addtogroup libc |
| 1653 | cejka | 31 | * @{ |
| 32 | */ |
||
| 1272 | cejka | 33 | /** |
| 1653 | cejka | 34 | * @file |
| 1272 | cejka | 35 | * @brief Printing functions. |
| 36 | */ |
||
| 37 | |||
| 1234 | cejka | 38 | #include <unistd.h> |
| 974 | cejka | 39 | #include <stdio.h> |
| 1234 | cejka | 40 | #include <io/printf_core.h> |
| 1173 | cejka | 41 | #include <ctype.h> |
| 42 | #include <string.h> |
||
| 974 | cejka | 43 | |
| 1272 | cejka | 44 | #define __PRINTF_FLAG_PREFIX 0x00000001 /**< show prefixes 0x or 0*/ |
| 45 | #define __PRINTF_FLAG_SIGNED 0x00000002 /**< signed / unsigned number */ |
||
| 46 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 /**< print leading zeroes */ |
||
| 47 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /**< align to left */ |
||
| 48 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 /**< always show + sign */ |
||
| 49 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 /**< print space instead of plus */ |
||
| 50 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 /**< show big characters */ |
||
| 51 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 /**< number has - sign */ |
||
| 995 | cejka | 52 | |
| 1272 | cejka | 53 | #define PRINT_NUMBER_BUFFER_SIZE (64+5) /**< Buffer big enought for 64 bit number |
| 1073 | cejka | 54 | * printed in base 2, sign, prefix and |
| 55 | * 0 to terminate string.. (last one is only for better testing |
||
| 56 | * end of buffer by zero-filling subroutine) |
||
| 57 | */ |
||
| 1272 | cejka | 58 | /** Enumeration of possible arguments types. |
| 59 | */ |
||
| 1073 | cejka | 60 | typedef enum { |
| 61 | PrintfQualifierByte = 0, |
||
| 62 | PrintfQualifierShort, |
||
| 63 | PrintfQualifierInt, |
||
| 64 | PrintfQualifierLong, |
||
| 65 | PrintfQualifierLongLong, |
||
| 66 | PrintfQualifierSizeT, |
||
| 67 | PrintfQualifierPointer |
||
| 68 | } qualifier_t; |
||
| 974 | cejka | 69 | |
| 1272 | cejka | 70 | static char digits_small[] = "0123456789abcdef"; /**< Small hexadecimal characters */ |
| 71 | static char digits_big[] = "0123456789ABCDEF"; /**< Big hexadecimal characters */ |
||
| 995 | cejka | 72 | |
| 1234 | cejka | 73 | /** Print count chars from buffer without adding newline |
| 74 | * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
||
| 75 | * @param count |
||
| 76 | * @param ps output method and its data |
||
| 1617 | cejka | 77 | * @return number of printed characters |
| 1234 | cejka | 78 | */ |
| 79 | static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) |
||
| 80 | { |
||
| 1617 | cejka | 81 | return ps->write((void *)buf, count, ps->data); |
| 1234 | cejka | 82 | } |
| 1173 | cejka | 83 | |
| 1234 | cejka | 84 | /** Print string without added newline |
| 85 | * @param str string to print |
||
| 86 | * @param ps write function specification and support data |
||
| 1617 | cejka | 87 | * @return number of printed characters |
| 1234 | cejka | 88 | */ |
| 89 | static int printf_putstr(const char * str, struct printf_spec *ps) |
||
| 90 | { |
||
| 91 | size_t count; |
||
| 92 | |||
| 2307 | hudecek | 93 | if (str == NULL) |
| 1234 | cejka | 94 | return printf_putnchars("(NULL)", 6, ps); |
| 95 | |||
| 96 | for (count = 0; str[count] != 0; count++); |
||
| 97 | |||
| 2307 | hudecek | 98 | if (ps->write((void *) str, count, ps->data) == count) |
| 1234 | cejka | 99 | return 0; |
| 100 | |||
| 101 | return EOF; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** Print one character to output |
||
| 105 | * @param c one character |
||
| 106 | * @param ps output method |
||
| 1617 | cejka | 107 | * @return number of printed characters |
| 1234 | cejka | 108 | */ |
| 109 | static int printf_putchar(int c, struct printf_spec *ps) |
||
| 110 | { |
||
| 111 | unsigned char ch = c; |
||
| 112 | |||
| 1617 | cejka | 113 | return ps->write((void *) &ch, 1, ps->data); |
| 1234 | cejka | 114 | } |
| 115 | |||
| 1173 | cejka | 116 | /** Print one formatted character |
| 117 | * @param c character to print |
||
| 118 | * @param width |
||
| 119 | * @param flags |
||
| 1617 | cejka | 120 | * @return number of printed characters |
| 1173 | cejka | 121 | */ |
| 1234 | cejka | 122 | static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) |
| 1173 | cejka | 123 | { |
| 124 | int counter = 0; |
||
| 125 | |||
| 126 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
||
| 127 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
||
| 1617 | cejka | 128 | if (printf_putchar(' ', ps) > 0) |
| 129 | ++counter; |
||
| 1173 | cejka | 130 | } |
| 131 | } |
||
| 1617 | cejka | 132 | |
| 133 | if (printf_putchar(c, ps) > 0) |
||
| 134 | counter++; |
||
| 1173 | cejka | 135 | |
| 136 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
||
| 1617 | cejka | 137 | if (printf_putchar(' ', ps) > 0) |
| 138 | ++counter; |
||
| 1173 | cejka | 139 | } |
| 140 | |||
| 141 | return ++counter; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** Print one string |
||
| 145 | * @param s string |
||
| 146 | * @param width |
||
| 147 | * @param precision |
||
| 148 | * @param flags |
||
| 1617 | cejka | 149 | * @return number of printed characters |
| 1173 | cejka | 150 | */ |
| 151 | |||
| 1234 | cejka | 152 | static int print_string(char *s, int width, int precision, uint64_t flags, struct printf_spec *ps) |
| 1173 | cejka | 153 | { |
| 154 | int counter = 0; |
||
| 155 | size_t size; |
||
| 1617 | cejka | 156 | int retval; |
| 1173 | cejka | 157 | |
| 158 | if (s == NULL) { |
||
| 1234 | cejka | 159 | return printf_putstr("(NULL)", ps); |
| 1173 | cejka | 160 | } |
| 161 | |||
| 162 | size = strlen(s); |
||
| 163 | |||
| 164 | /* print leading spaces */ |
||
| 165 | |||
| 166 | if (precision == 0) |
||
| 167 | precision = size; |
||
| 168 | |||
| 169 | width -= precision; |
||
| 170 | |||
| 171 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
||
| 172 | while (width-- > 0) { |
||
| 1617 | cejka | 173 | if (printf_putchar(' ', ps) == 1) |
| 174 | counter++; |
||
| 1173 | cejka | 175 | } |
| 176 | } |
||
| 177 | |||
| 178 | while (precision > size) { |
||
| 179 | precision--; |
||
| 1617 | cejka | 180 | if (printf_putchar(' ', ps) == 1) |
| 181 | ++counter; |
||
| 1173 | cejka | 182 | } |
| 183 | |||
| 1617 | cejka | 184 | if ((retval = printf_putnchars(s, precision, ps)) < 0) { |
| 185 | return -counter; |
||
| 1173 | cejka | 186 | } |
| 187 | |||
| 1617 | cejka | 188 | counter += retval; |
| 1173 | cejka | 189 | |
| 190 | while (width-- > 0) { |
||
| 1617 | cejka | 191 | if (printf_putchar(' ', ps) == 1) |
| 192 | ++counter; |
||
| 1173 | cejka | 193 | } |
| 194 | |||
| 1617 | cejka | 195 | return counter; |
| 1173 | cejka | 196 | } |
| 197 | |||
| 198 | |||
| 974 | cejka | 199 | /** Print number in given base |
| 200 | * |
||
| 201 | * Print significant digits of a number in given |
||
| 202 | * base. |
||
| 203 | * |
||
| 204 | * @param num Number to print. |
||
| 1173 | cejka | 205 | * @param width |
| 206 | * @param precision |
||
| 974 | cejka | 207 | * @param base Base to print the number in (should |
| 208 | * be in range 2 .. 16). |
||
| 1073 | cejka | 209 | * @param flags output modifiers |
| 1617 | cejka | 210 | * @return number of printed characters |
| 974 | cejka | 211 | * |
| 212 | */ |
||
| 1234 | cejka | 213 | static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags, struct printf_spec *ps) |
| 974 | cejka | 214 | { |
| 1073 | cejka | 215 | char *digits = digits_small; |
| 216 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
||
| 217 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
||
| 1234 | cejka | 218 | int size = 0; /* size of number with all prefixes and signs */ |
| 219 | int number_size; /* size of plain number */ |
||
| 1173 | cejka | 220 | char sgn; |
| 1617 | cejka | 221 | int retval; |
| 222 | int counter = 0; |
||
| 974 | cejka | 223 | |
| 1073 | cejka | 224 | if (flags & __PRINTF_FLAG_BIGCHARS) |
| 225 | digits = digits_big; |
||
| 995 | cejka | 226 | |
| 1073 | cejka | 227 | *ptr-- = 0; /* Put zero at end of string */ |
| 228 | |||
| 229 | if (num == 0) { |
||
| 230 | *ptr-- = '0'; |
||
| 1173 | cejka | 231 | size++; |
| 1073 | cejka | 232 | } else { |
| 233 | do { |
||
| 234 | *ptr-- = digits[num % base]; |
||
| 1173 | cejka | 235 | size++; |
| 1073 | cejka | 236 | } while (num /= base); |
| 237 | } |
||
| 1234 | cejka | 238 | |
| 239 | number_size = size; |
||
| 1173 | cejka | 240 | |
| 241 | /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */ |
||
| 242 | if (flags & __PRINTF_FLAG_PREFIX) { |
||
| 1073 | cejka | 243 | switch(base) { |
| 2070 | jermar | 244 | case 2: /* Binary formating is not standard, but usefull */ |
| 245 | size += 2; |
||
| 246 | break; |
||
| 247 | case 8: |
||
| 248 | size++; |
||
| 249 | break; |
||
| 250 | case 16: |
||
| 251 | size += 2; |
||
| 252 | break; |
||
| 1073 | cejka | 253 | } |
| 254 | } |
||
| 1173 | cejka | 255 | |
| 256 | sgn = 0; |
||
| 1073 | cejka | 257 | if (flags & __PRINTF_FLAG_SIGNED) { |
| 258 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
||
| 1173 | cejka | 259 | sgn = '-'; |
| 260 | size++; |
||
| 1073 | cejka | 261 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
| 1173 | cejka | 262 | sgn = '+'; |
| 263 | size++; |
||
| 1073 | cejka | 264 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
| 1173 | cejka | 265 | sgn = ' '; |
| 266 | size++; |
||
| 1073 | cejka | 267 | } |
| 268 | } |
||
| 974 | cejka | 269 | |
| 1073 | cejka | 270 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
| 271 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
||
| 272 | } |
||
| 1173 | cejka | 273 | |
| 274 | /* if number is leftaligned or precision is specified then zeropadding is ignored */ |
||
| 1073 | cejka | 275 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
| 1173 | cejka | 276 | if ((precision == 0) && (width > size)) { |
| 1234 | cejka | 277 | precision = width - size + number_size; |
| 1073 | cejka | 278 | } |
| 279 | } |
||
| 1173 | cejka | 280 | |
| 281 | /* print leading spaces */ |
||
| 1234 | cejka | 282 | if (number_size > precision) /* We must print whole number not only a part */ |
| 283 | precision = number_size; |
||
| 1173 | cejka | 284 | |
| 1234 | cejka | 285 | width -= precision + size - number_size; |
| 1073 | cejka | 286 | |
| 1173 | cejka | 287 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 288 | while (width-- > 0) { |
||
| 1617 | cejka | 289 | if (printf_putchar(' ', ps) == 1) |
| 290 | counter++; |
||
| 1173 | cejka | 291 | } |
| 292 | } |
||
| 293 | |||
| 294 | /* print sign */ |
||
| 295 | if (sgn) { |
||
| 1617 | cejka | 296 | if (printf_putchar(sgn, ps) == 1) |
| 297 | counter++; |
||
| 1173 | cejka | 298 | } |
| 299 | |||
| 300 | /* print prefix */ |
||
| 301 | |||
| 302 | if (flags & __PRINTF_FLAG_PREFIX) { |
||
| 303 | switch(base) { |
||
| 2070 | jermar | 304 | case 2: /* Binary formating is not standard, but usefull */ |
| 305 | if (printf_putchar('0', ps) == 1) |
||
| 306 | counter++; |
||
| 307 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
||
| 308 | if (printf_putchar('B', ps) == 1) |
||
| 1617 | cejka | 309 | counter++; |
| 2070 | jermar | 310 | } else { |
| 311 | if (printf_putchar('b', ps) == 1) |
||
| 1617 | cejka | 312 | counter++; |
| 2070 | jermar | 313 | } |
| 314 | break; |
||
| 315 | case 8: |
||
| 316 | if (printf_putchar('o', ps) == 1) |
||
| 317 | counter++; |
||
| 318 | break; |
||
| 319 | case 16: |
||
| 320 | if (printf_putchar('0', ps) == 1) |
||
| 321 | counter++; |
||
| 322 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
||
| 323 | if (printf_putchar('X', ps) == 1) |
||
| 1617 | cejka | 324 | counter++; |
| 2070 | jermar | 325 | } else { |
| 326 | if (printf_putchar('x', ps) == 1) |
||
| 327 | counter++; |
||
| 328 | } |
||
| 329 | break; |
||
| 1173 | cejka | 330 | } |
| 331 | } |
||
| 332 | |||
| 333 | /* print leading zeroes */ |
||
| 1234 | cejka | 334 | precision -= number_size; |
| 1173 | cejka | 335 | while (precision-- > 0) { |
| 1617 | cejka | 336 | if (printf_putchar('0', ps) == 1) |
| 337 | counter++; |
||
| 1173 | cejka | 338 | } |
| 339 | |||
| 340 | |||
| 341 | /* print number itself */ |
||
| 342 | |||
| 1617 | cejka | 343 | if ((retval = printf_putstr(++ptr, ps)) > 0) { |
| 344 | counter += retval; |
||
| 345 | } |
||
| 1173 | cejka | 346 | |
| 347 | /* print ending spaces */ |
||
| 348 | |||
| 349 | while (width-- > 0) { |
||
| 1617 | cejka | 350 | if (printf_putchar(' ', ps) == 1) |
| 351 | counter++; |
||
| 1173 | cejka | 352 | } |
| 353 | |||
| 1617 | cejka | 354 | return counter; |
| 974 | cejka | 355 | } |
| 356 | |||
| 357 | |||
| 1272 | cejka | 358 | /** Print formatted string. |
| 974 | cejka | 359 | * |
| 1272 | cejka | 360 | * Print string formatted according to the fmt parameter |
| 361 | * and variadic arguments. Each formatting directive |
||
| 1199 | cejka | 362 | * must have the following form: |
| 1272 | cejka | 363 | * |
| 364 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
||
| 974 | cejka | 365 | * |
| 1272 | cejka | 366 | * FLAGS:@n |
| 367 | * - "#" Force to print prefix. |
||
| 368 | * For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and 0X |
||
| 369 | * and for conversion \%b the prefix is 0b. |
||
| 370 | * |
||
| 371 | * - "-" Align to left. |
||
| 372 | * |
||
| 373 | * - "+" Print positive sign just as negative. |
||
| 374 | * |
||
| 375 | * - " " If the printed number is positive and "+" flag is not set, print space in |
||
| 376 | * place of sign. |
||
| 377 | * |
||
| 378 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between sign and the |
||
| 379 | * rest of the number. This flag is ignored if "-" flag is specified. |
||
| 1199 | cejka | 380 | * |
| 1272 | cejka | 381 | * WIDTH:@n |
| 382 | * - Specify minimal width of printed argument. If it is bigger, width is ignored. |
||
| 383 | * If width is specified with a "*" character instead of number, width is taken from |
||
| 384 | * parameter list. And integer parameter is expected before parameter for processed |
||
| 385 | * conversion specification. If this value is negative its absolute value is taken |
||
| 386 | * and the "-" flag is set. |
||
| 974 | cejka | 387 | * |
| 1272 | cejka | 388 | * PRECISION:@n |
| 389 | * - Value precision. For numbers it specifies minimum valid numbers. |
||
| 1199 | cejka | 390 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not affected. |
| 1272 | cejka | 391 | * Strings with more than precision characters are cut off. |
| 392 | * Just as with width, an "*" can be used used instead of a number. |
||
| 393 | * An integer value is then expected in parameters. When both width and precision |
||
| 394 | * are specified using "*", the first parameter is used for width and the second one |
||
| 395 | * for precision. |
||
| 1199 | cejka | 396 | * |
| 1272 | cejka | 397 | * TYPE:@n |
| 398 | * - "hh" Signed or unsigned char.@n |
||
| 399 | * - "h" Signed or usigned short.@n |
||
| 400 | * - "" Signed or usigned int (default value).@n |
||
| 401 | * - "l" Signed or usigned long int.@n |
||
| 402 | * - "ll" Signed or usigned long long int.@n |
||
| 403 | * - "z" Type size_t.@n |
||
| 1199 | cejka | 404 | * |
| 405 | * |
||
| 1272 | cejka | 406 | * CONVERSION:@n |
| 407 | * - % Print percentile character itself. |
||
| 974 | cejka | 408 | * |
| 1272 | cejka | 409 | * - c Print single character. |
| 974 | cejka | 410 | * |
| 1272 | cejka | 411 | * - s Print zero terminated string. If a NULL value is passed as value, "(NULL)" is printed instead. |
| 1199 | cejka | 412 | * |
| 1272 | cejka | 413 | * - P, p Print value of a pointer. Void * value is expected and it is printed in hexadecimal notation with prefix |
| 1653 | cejka | 414 | * (as with '\%#X' or '\%#x' for 32bit or '\%#X' or '\%#x' for 64bit long pointers). |
| 974 | cejka | 415 | * |
| 1272 | cejka | 416 | * - b Print value as unsigned binary number. Prefix is not printed by default. (Nonstandard extension.) |
| 1199 | cejka | 417 | * |
| 1272 | cejka | 418 | * - o Print value as unsigned octal number. Prefix is not printed by default. |
| 974 | cejka | 419 | * |
| 1272 | cejka | 420 | * - d,i Print signed decimal number. There is no difference between d and i conversion. |
| 974 | cejka | 421 | * |
| 1272 | cejka | 422 | * - u Print unsigned decimal number. |
| 974 | cejka | 423 | * |
| 1272 | cejka | 424 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is not printed by default. |
| 1199 | cejka | 425 | * |
| 974 | cejka | 426 | * All other characters from fmt except the formatting directives |
| 427 | * are printed in verbatim. |
||
| 428 | * |
||
| 429 | * @param fmt Formatting NULL terminated string. |
||
| 1272 | cejka | 430 | * @return Number of printed characters or negative value on failure. |
| 974 | cejka | 431 | */ |
| 1234 | cejka | 432 | int printf_core(const char *fmt, struct printf_spec *ps, va_list ap) |
| 974 | cejka | 433 | { |
| 1444 | cejka | 434 | int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */ |
| 1073 | cejka | 435 | int end; |
| 1444 | cejka | 436 | int counter; /* counter of printed characters */ |
| 437 | int retval; /* used to store return values from called functions */ |
||
| 1073 | cejka | 438 | char c; |
| 1444 | cejka | 439 | qualifier_t qualifier; /* type of argument */ |
| 440 | int base; /* base in which will be parameter (numbers only) printed */ |
||
| 441 | uint64_t number; /* argument value */ |
||
| 442 | size_t size; /* byte size of integer parameter */ |
||
| 1173 | cejka | 443 | int width, precision; |
| 995 | cejka | 444 | uint64_t flags; |
| 974 | cejka | 445 | |
| 446 | counter = 0; |
||
| 1173 | cejka | 447 | |
| 974 | cejka | 448 | while ((c = fmt[i])) { |
| 992 | jermar | 449 | /* control character */ |
| 974 | cejka | 450 | if (c == '%' ) { |
| 992 | jermar | 451 | /* print common characters if any processed */ |
| 974 | cejka | 452 | if (i > j) { |
| 1617 | cejka | 453 | if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */ |
| 1616 | palkovsky | 454 | goto minus_out; |
| 974 | cejka | 455 | } |
| 456 | counter += retval; |
||
| 457 | } |
||
| 1073 | cejka | 458 | |
| 459 | j = i; |
||
| 995 | cejka | 460 | /* parse modifiers */ |
| 461 | flags = 0; |
||
| 1073 | cejka | 462 | end = 0; |
| 463 | |||
| 464 | do { |
||
| 465 | ++i; |
||
| 466 | switch (c = fmt[i]) { |
||
| 2070 | jermar | 467 | case '#': flags |= __PRINTF_FLAG_PREFIX; break; |
| 468 | case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break; |
||
| 469 | case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break; |
||
| 470 | case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break; |
||
| 471 | case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break; |
||
| 472 | default: end = 1; |
||
| 1073 | cejka | 473 | }; |
| 474 | |||
| 475 | } while (end == 0); |
||
| 1173 | cejka | 476 | |
| 477 | /* width & '*' operator */ |
||
| 478 | width = 0; |
||
| 479 | if (isdigit(fmt[i])) { |
||
| 480 | while (isdigit(fmt[i])) { |
||
| 481 | width *= 10; |
||
| 482 | width += fmt[i++] - '0'; |
||
| 483 | } |
||
| 484 | } else if (fmt[i] == '*') { |
||
| 485 | /* get width value from argument list*/ |
||
| 486 | i++; |
||
| 487 | width = (int)va_arg(ap, int); |
||
| 488 | if (width < 0) { |
||
| 489 | /* negative width means to set '-' flag */ |
||
| 490 | width *= -1; |
||
| 491 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /* precision and '*' operator */ |
||
| 496 | precision = 0; |
||
| 497 | if (fmt[i] == '.') { |
||
| 498 | ++i; |
||
| 499 | if (isdigit(fmt[i])) { |
||
| 500 | while (isdigit(fmt[i])) { |
||
| 501 | precision *= 10; |
||
| 502 | precision += fmt[i++] - '0'; |
||
| 503 | } |
||
| 504 | } else if (fmt[i] == '*') { |
||
| 505 | /* get precision value from argument list*/ |
||
| 506 | i++; |
||
| 507 | precision = (int)va_arg(ap, int); |
||
| 508 | if (precision < 0) { |
||
| 509 | /* negative precision means to ignore it */ |
||
| 510 | precision = 0; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | } |
||
| 1073 | cejka | 514 | |
| 515 | switch (fmt[i++]) { |
||
| 2070 | jermar | 516 | /** TODO: unimplemented qualifiers: |
| 517 | * t ptrdiff_t - ISO C 99 |
||
| 518 | */ |
||
| 519 | case 'h': /* char or short */ |
||
| 520 | qualifier = PrintfQualifierShort; |
||
| 521 | if (fmt[i] == 'h') { |
||
| 522 | i++; |
||
| 523 | qualifier = PrintfQualifierByte; |
||
| 524 | } |
||
| 525 | break; |
||
| 526 | case 'l': /* long or long long*/ |
||
| 527 | qualifier = PrintfQualifierLong; |
||
| 528 | if (fmt[i] == 'l') { |
||
| 529 | i++; |
||
| 530 | qualifier = PrintfQualifierLongLong; |
||
| 531 | } |
||
| 532 | break; |
||
| 533 | case 'z': /* size_t */ |
||
| 534 | qualifier = PrintfQualifierSizeT; |
||
| 535 | break; |
||
| 536 | default: |
||
| 537 | qualifier = PrintfQualifierInt; /* default type */ |
||
| 538 | --i; |
||
| 995 | cejka | 539 | } |
| 1073 | cejka | 540 | |
| 541 | base = 10; |
||
| 542 | |||
| 974 | cejka | 543 | switch (c = fmt[i]) { |
| 544 | |||
| 545 | /* |
||
| 546 | * String and character conversions. |
||
| 547 | */ |
||
| 2070 | jermar | 548 | case 's': |
| 549 | if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) { |
||
| 550 | goto minus_out; |
||
| 551 | } |
||
| 974 | cejka | 552 | |
| 2070 | jermar | 553 | counter += retval; |
| 554 | j = i + 1; |
||
| 555 | goto next_char; |
||
| 556 | case 'c': |
||
| 557 | c = va_arg(ap, unsigned int); |
||
| 558 | if ((retval = print_char(c, width, flags, ps)) < 0) { |
||
| 559 | goto minus_out; |
||
| 560 | } |
||
| 974 | cejka | 561 | |
| 2070 | jermar | 562 | counter += retval; |
| 563 | j = i + 1; |
||
| 564 | goto next_char; |
||
| 565 | |||
| 566 | /* |
||
| 567 | * Integer values |
||
| 568 | */ |
||
| 569 | case 'P': /* pointer */ |
||
| 570 | flags |= __PRINTF_FLAG_BIGCHARS; |
||
| 571 | case 'p': |
||
| 572 | flags |= __PRINTF_FLAG_PREFIX; |
||
| 573 | base = 16; |
||
| 574 | qualifier = PrintfQualifierPointer; |
||
| 575 | break; |
||
| 576 | case 'b': |
||
| 577 | base = 2; |
||
| 578 | break; |
||
| 579 | case 'o': |
||
| 580 | base = 8; |
||
| 581 | break; |
||
| 582 | case 'd': |
||
| 583 | case 'i': |
||
| 584 | flags |= __PRINTF_FLAG_SIGNED; |
||
| 585 | case 'u': |
||
| 586 | break; |
||
| 587 | case 'X': |
||
| 588 | flags |= __PRINTF_FLAG_BIGCHARS; |
||
| 589 | case 'x': |
||
| 590 | base = 16; |
||
| 591 | break; |
||
| 592 | /* percentile itself */ |
||
| 593 | case '%': |
||
| 594 | j = i; |
||
| 595 | goto next_char; |
||
| 596 | /* |
||
| 597 | * Bad formatting. |
||
| 598 | */ |
||
| 599 | default: |
||
| 600 | /* Unknown format |
||
| 601 | * now, the j is index of '%' so we will |
||
| 602 | * print whole bad format sequence |
||
| 603 | */ |
||
| 604 | goto next_char; |
||
| 1073 | cejka | 605 | } |
| 606 | |||
| 607 | |||
| 608 | /* Print integers */ |
||
| 609 | /* print number */ |
||
| 610 | switch (qualifier) { |
||
| 2070 | jermar | 611 | case PrintfQualifierByte: |
| 612 | size = sizeof(unsigned char); |
||
| 613 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 614 | break; |
||
| 615 | case PrintfQualifierShort: |
||
| 616 | size = sizeof(unsigned short); |
||
| 617 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 618 | break; |
||
| 619 | case PrintfQualifierInt: |
||
| 620 | size = sizeof(unsigned int); |
||
| 621 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 622 | break; |
||
| 623 | case PrintfQualifierLong: |
||
| 624 | size = sizeof(unsigned long); |
||
| 625 | number = (uint64_t)va_arg(ap, unsigned long); |
||
| 626 | break; |
||
| 627 | case PrintfQualifierLongLong: |
||
| 628 | size = sizeof(unsigned long long); |
||
| 629 | number = (uint64_t)va_arg(ap, unsigned long long); |
||
| 630 | break; |
||
| 631 | case PrintfQualifierPointer: |
||
| 632 | size = sizeof(void *); |
||
| 633 | number = (uint64_t)(unsigned long)va_arg(ap, void *); |
||
| 634 | break; |
||
| 635 | case PrintfQualifierSizeT: |
||
| 636 | size = sizeof(size_t); |
||
| 637 | number = (uint64_t)va_arg(ap, size_t); |
||
| 638 | break; |
||
| 639 | default: /* Unknown qualifier */ |
||
| 640 | goto minus_out; |
||
| 1073 | cejka | 641 | |
| 974 | cejka | 642 | } |
| 1073 | cejka | 643 | |
| 644 | if (flags & __PRINTF_FLAG_SIGNED) { |
||
| 645 | if (number & (0x1 << (size*8 - 1))) { |
||
| 646 | flags |= __PRINTF_FLAG_NEGATIVE; |
||
| 647 | |||
| 648 | if (size == sizeof(uint64_t)) { |
||
| 649 | number = -((int64_t)number); |
||
| 650 | } else { |
||
| 651 | number = ~number; |
||
| 652 | number &= (~((0xFFFFFFFFFFFFFFFFll) << (size * 8))); |
||
| 653 | number++; |
||
| 654 | } |
||
| 655 | } |
||
| 656 | } |
||
| 657 | |||
| 1617 | cejka | 658 | if ((retval = print_number(number, width, precision, base, flags, ps)) < 0 ) { |
| 1616 | palkovsky | 659 | goto minus_out; |
| 1073 | cejka | 660 | }; |
| 661 | |||
| 662 | counter += retval; |
||
| 663 | j = i + 1; |
||
| 974 | cejka | 664 | } |
| 1073 | cejka | 665 | next_char: |
| 666 | |||
| 974 | cejka | 667 | ++i; |
| 668 | } |
||
| 669 | |||
| 975 | cejka | 670 | if (i > j) { |
| 1617 | cejka | 671 | if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */ |
| 1616 | palkovsky | 672 | goto minus_out; |
| 975 | cejka | 673 | } |
| 674 | counter += retval; |
||
| 675 | } |
||
| 676 | |||
| 974 | cejka | 677 | return counter; |
| 1616 | palkovsky | 678 | minus_out: |
| 679 | return -counter; |
||
| 974 | cejka | 680 | } |
| 681 | |||
| 1866 | jermar | 682 | /** @} |
| 1653 | cejka | 683 | */ |