Changeset f1249e1 in mainline for libc/generic/io/printf_core.c
- Timestamp:
- 2006-06-06T16:45:43Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f4b961f
- Parents:
- 0d11fc1c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libc/generic/io/printf_core.c
r0d11fc1c rf1249e1 74 74 * @param count 75 75 * @param ps output method and its data 76 * @return 0 on success, EOF on fail76 * @return number of printed characters 77 77 */ 78 78 static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) 79 79 { 80 if (ps->write((void *)buf, count, ps->data) == count) { 81 return 0; 82 } 83 84 return EOF; 80 return ps->write((void *)buf, count, ps->data); 85 81 } 86 82 … … 88 84 * @param str string to print 89 85 * @param ps write function specification and support data 90 * @return 0 on success or EOF on fail86 * @return number of printed characters 91 87 */ 92 88 static int printf_putstr(const char * str, struct printf_spec *ps) … … 110 106 * @param c one character 111 107 * @param ps output method 112 * @return printed character or EOF108 * @return number of printed characters 113 109 */ 114 110 static int printf_putchar(int c, struct printf_spec *ps) … … 116 112 unsigned char ch = c; 117 113 118 if (ps->write((void *) &ch, 1, ps->data) == 1) { 119 return c; 120 } 121 122 return EOF; 114 return ps->write((void *) &ch, 1, ps->data); 123 115 } 124 116 … … 127 119 * @param width 128 120 * @param flags 129 * @return number of printed characters or EOF121 * @return number of printed characters 130 122 */ 131 123 static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) … … 135 127 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 136 128 while (--width > 0) { /* one space is consumed by character itself hence predecrement */ 137 /* FIXME: painful slow */ 138 printf_putchar(' ', ps); 129 if (printf_putchar(' ', ps) > 0) 130 ++counter; 131 } 132 } 133 134 if (printf_putchar(c, ps) > 0) 135 counter++; 136 137 while (--width > 0) { /* one space is consumed by character itself hence predecrement */ 138 if (printf_putchar(' ', ps) > 0) 139 139 ++counter; 140 }141 }142 143 if (printf_putchar(c, ps) == EOF) {144 return EOF;145 }146 147 while (--width > 0) { /* one space is consumed by character itself hence predecrement */148 printf_putchar(' ', ps);149 ++counter;150 140 } 151 141 … … 158 148 * @param precision 159 149 * @param flags 160 * @return number of printed characters or EOF150 * @return number of printed characters 161 151 */ 162 152 … … 165 155 int counter = 0; 166 156 size_t size; 157 int retval; 167 158 168 159 if (s == NULL) { … … 181 172 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 182 173 while (width-- > 0) { 183 printf_putchar(' ', ps);184 counter++;174 if (printf_putchar(' ', ps) == 1) 175 counter++; 185 176 } 186 177 } … … 188 179 while (precision > size) { 189 180 precision--; 190 printf_putchar(' ', ps);191 ++counter;192 } 193 194 if ( printf_putnchars(s, precision, ps) == EOF) {195 return EOF;196 } 197 198 counter += precision;181 if (printf_putchar(' ', ps) == 1) 182 ++counter; 183 } 184 185 if ((retval = printf_putnchars(s, precision, ps)) < 0) { 186 return -counter; 187 } 188 189 counter += retval; 199 190 200 191 while (width-- > 0) { 201 printf_putchar(' ', ps);202 ++counter;203 } 204 205 return ++counter;192 if (printf_putchar(' ', ps) == 1) 193 ++counter; 194 } 195 196 return counter; 206 197 } 207 198 … … 218 209 * be in range 2 .. 16). 219 210 * @param flags output modifiers 220 * @return number of written characters or EOF211 * @return number of printed characters 221 212 * 222 213 */ … … 228 219 int size = 0; /* size of number with all prefixes and signs */ 229 220 int number_size; /* size of plain number */ 230 int written = 0;231 221 char sgn; 222 int retval; 223 int counter = 0; 232 224 233 225 if (flags & __PRINTF_FLAG_BIGCHARS) … … 296 288 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 297 289 while (width-- > 0) { 298 printf_putchar(' ', ps);299 written++;290 if (printf_putchar(' ', ps) == 1) 291 counter++; 300 292 } 301 293 } … … 303 295 /* print sign */ 304 296 if (sgn) { 305 printf_putchar(sgn, ps);306 written++;297 if (printf_putchar(sgn, ps) == 1) 298 counter++; 307 299 } 308 300 … … 312 304 switch(base) { 313 305 case 2: /* Binary formating is not standard, but usefull */ 314 printf_putchar('0', ps); 306 if (printf_putchar('0', ps) == 1) 307 counter++; 315 308 if (flags & __PRINTF_FLAG_BIGCHARS) { 316 printf_putchar('B', ps); 309 if (printf_putchar('B', ps) == 1) 310 counter++; 317 311 } else { 318 printf_putchar('b', ps);319 }320 written += 2;312 if (printf_putchar('b', ps) == 1) 313 counter++; 314 } 321 315 break; 322 316 case 8: 323 printf_putchar('o', ps);324 written++;317 if (printf_putchar('o', ps) == 1) 318 counter++; 325 319 break; 326 320 case 16: 327 printf_putchar('0', ps); 321 if (printf_putchar('0', ps) == 1) 322 counter++; 328 323 if (flags & __PRINTF_FLAG_BIGCHARS) { 329 printf_putchar('X', ps); 324 if (printf_putchar('X', ps) == 1) 325 counter++; 330 326 } else { 331 printf_putchar('x', ps);332 }333 written += 2;327 if (printf_putchar('x', ps) == 1) 328 counter++; 329 } 334 330 break; 335 331 } … … 339 335 precision -= number_size; 340 336 while (precision-- > 0) { 341 printf_putchar('0', ps);342 written++;337 if (printf_putchar('0', ps) == 1) 338 counter++; 343 339 } 344 340 … … 346 342 /* print number itself */ 347 343 348 written += printf_putstr(++ptr, ps); 344 if ((retval = printf_putstr(++ptr, ps)) > 0) { 345 counter += retval; 346 } 349 347 350 348 /* print ending spaces */ 351 349 352 350 while (width-- > 0) { 353 printf_putchar(' ', ps);354 written++;355 } 356 357 return written;351 if (printf_putchar(' ', ps) == 1) 352 counter++; 353 } 354 355 return counter; 358 356 } 359 357 … … 457 455 /* print common characters if any processed */ 458 456 if (i > j) { 459 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */457 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */ 460 458 goto minus_out; 461 459 } … … 553 551 */ 554 552 case 's': 555 if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {553 if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) { 556 554 goto minus_out; 557 555 }; … … 562 560 case 'c': 563 561 c = va_arg(ap, unsigned int); 564 if ((retval = print_char(c, width, flags, ps)) == EOF) {562 if ((retval = print_char(c, width, flags, ps)) < 0) { 565 563 goto minus_out; 566 564 }; … … 662 660 } 663 661 664 if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF) {662 if ((retval = print_number(number, width, precision, base, flags, ps)) < 0 ) { 665 663 goto minus_out; 666 664 }; … … 675 673 676 674 if (i > j) { 677 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */675 if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */ 678 676 goto minus_out; 679 677 }
Note:
See TracChangeset
for help on using the changeset viewer.