3 * - By John Hodge (thePowersGang)
6 * - *scanf family of functions
12 #include <ctype.h> // toupper
13 #include <assert.h> // assert()
21 typedef void (*printf_putch_t)(void *h, char ch);
29 void itoa(char *buf, uint64_t num, size_t base, int minLength, char pad, int bSigned);
30 size_t _printf_itoa(printf_putch_t putch_cb, void *putch_h, uint64_t num,
31 size_t base, int bUpper,
32 int bSigned, char SignChar, int Precision,
33 int PadLength, char PadChar, int bPadRight);
34 size_t _printf_ftoa_hex(printf_putch_t putch_cb, void *putch_h, long double num, int Precision, int bForcePoint, int bForceSign, int bCapitals);
35 size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, size_t Base, enum eFPN Notation, int Precision, int bForcePoint, int bForceSign, int bCapitals);
39 * \fn EXPORT void vsnprintf(char *buf, const char *format, va_list args)
40 * \brief Prints a formatted string to a buffer
41 * \param buf Pointer - Destination Buffer
42 * \param format String - Format String
43 * \param args VarArgs List - Arguments
45 EXPORT int _vcprintf_int(printf_putch_t putch_cb, void *putch_h, const char *format, va_list args)
48 int c, minSize, precision, len;
53 // char cPositiveChar;
54 BOOL bLongLong, bLong, bJustifyLeft, bAltForm;
57 #define _addchar(ch) do { \
58 putch_cb(putch_h, ch); \
64 while((c = *format++) != 0)
66 // Non-control character
74 if(c == '%') { // Literal %
91 // Alternate form (0<oct>, 0x<hex>, 123.)
103 // Include space for positive sign
107 // Always include sign
114 while( c = *format++ );
119 minSize = va_arg(args, size_t);
122 else if('1' <= c && c <= '9')
125 while('0' <= c && c <= '9')
137 precision = va_arg(args, size_t);
140 else if('1' <= c && c <= '9')
143 while('0' <= c && c <= '9')
146 precision += c - '0';
152 // Check for long long
163 // Just help things along later
173 arg = bLongLong ? va_arg(args, int64_t) : va_arg(args, int32_t);
174 if( arg == 0 && precision == 0 )
176 pos += _printf_itoa(putch_cb, putch_h, arg, 10, FALSE,
177 TRUE, cPlus, precision, minSize, cNumPad, bJustifyLeft);
182 arg = bLongLong ? va_arg(args, int64_t) : va_arg(args, int32_t);
183 pos += _printf_itoa(putch_cb, putch_h, arg, 10, FALSE,
184 FALSE, '\0', precision, minSize, cNumPad, bJustifyLeft);
192 arg = va_arg(args, intptr_t);
193 pos += _printf_itoa(putch_cb, putch_h, arg, 16, FALSE,
194 FALSE, '\0', sizeof(intptr_t)*2, 0,'\0',FALSE);
196 // Unsigned Hexadecimal
203 arg = bLongLong ? va_arg(args, int64_t) : va_arg(args, int32_t);
204 pos += _printf_itoa(putch_cb, putch_h, arg, 16, c=='X',
205 FALSE, '\0', precision, minSize,cNumPad,bJustifyLeft);
213 arg = bLongLong ? va_arg(args, int64_t) : va_arg(args, int32_t);
214 pos += _printf_itoa(putch_cb, putch_h, arg, 8, FALSE,
215 FALSE, '\0', precision, minSize,cNumPad,bJustifyLeft);
224 arg = bLongLong ? va_arg(args, int64_t) : va_arg(args, int32_t);
225 pos += _printf_itoa(putch_cb, putch_h, arg, 2, FALSE,
226 FALSE, '\0', precision, minSize,cNumPad,bJustifyLeft);
232 arg_f = bLong ? va_arg(args, long double) : va_arg(args, double);
233 pos += _printf_ftoa(putch_cb, putch_h, arg_f, 10, FPN_STD,
234 precision, 0, bJustifyLeft, c == 'F');
239 arg_f = bLong ? va_arg(args, long double) : va_arg(args, double);
240 pos += _printf_ftoa(putch_cb, putch_h, arg_f, 10, FPN_SCI,
241 precision, 0, bJustifyLeft, c == 'E');
243 // Hexadecimal Scientific
246 arg_f = bLong ? va_arg(args, long double) : va_arg(args, double);
247 pos += _printf_ftoa_hex(putch_cb, putch_h, arg_f, precision, 0, bJustifyLeft, c == 'A');
252 p = va_arg(args, char*);
255 //_SysDebug("vsnprintf: p = '%s'", p);
257 len = strnlen(p, precision);
261 while(minSize > len++) _addchar(' ');
263 if(precision >= 0 && precision -- == 0)
268 while(minSize > len++) _addchar(' ');
271 // Unknown, just treat it as a character
273 arg = va_arg(args, uint32_t);
283 struct s_sprintf_info {
289 void _vsnprintf_putch(void *h, char ch)
291 struct s_sprintf_info *info = h;
292 if(info->ofs < info->maxlen)
293 info->dest[info->ofs++] = ch;
296 EXPORT int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list __args)
298 struct s_sprintf_info info = {__s, 0, __maxlen};
300 ret = _vcprintf_int(_vsnprintf_putch, &info, __format, __args);
301 _vsnprintf_putch(&info, '\0');
305 EXPORT int snprintf(char *buf, size_t maxlen, const char *format, ...)
309 va_start(args, format);
310 ret = vsnprintf((char*)buf, maxlen, (char*)format, args);
316 EXPORT int vsprintf(char * __s, const char *__format, va_list __args)
318 return vsnprintf(__s, 0x7FFFFFFF, __format, __args);
320 EXPORT int sprintf(char *buf, const char *format, ...)
324 va_start(args, format);
325 ret = vsprintf((char*)buf, (char*)format, args);
330 void _vfprintf_putch(void *h, char ch)
335 EXPORT int vfprintf(FILE *__fp, const char *__format, va_list __args)
337 return _vcprintf_int(_vfprintf_putch, __fp, __format, __args);
340 EXPORT int fprintf(FILE *fp, const char *format, ...)
346 va_start(args, format);
347 ret = vfprintf(fp, (char*)format, args);
353 EXPORT int vprintf(const char *__format, va_list __args)
355 return vfprintf(stdout, __format, __args);
358 EXPORT int printf(const char *format, ...)
364 va_start(args, format);
365 ret = vprintf(format, args);
372 void itoa(char *buf, uint64_t num, size_t base, int minLength, char pad, int bSigned)
374 struct s_sprintf_info info = {buf, 0, 1024};
376 _printf_itoa(_vsnprintf_putch, &info, num, base, FALSE, bSigned, '\0', 0, minLength, pad, FALSE);
377 buf[info.ofs] = '\0';
380 const char cDIGITS[] = "0123456789abcdef";
381 const char cUDIGITS[] = "0123456789ABCDEF";
383 * \brief Convert an integer into a character string
384 * \param buf Destination Buffer
385 * \param num Number to convert
386 * \param base Base-n number output
387 * \param minLength Minimum length of output
388 * \param pad Padding used to ensure minLength
389 * \param bSigned Signed number output?
391 size_t _printf_itoa(printf_putch_t putch_cb, void *putch_h, uint64_t num,
392 size_t base, int bUpper,
393 int bSigned, char SignChar, int Precision,
394 int PadLength, char PadChar, int bPadRight)
400 const char *map = bUpper ? cUDIGITS : cDIGITS;
402 if(base > 16 || base < 2) {
406 if(bSigned && (int64_t)num < 0)
412 // Encode into reversed string
413 while(num > base-1) {
414 tmpBuf[pos++] = map[ num % base ];
415 num = (uint64_t) num / (uint64_t)base; // Shift {number} right 1 digit
418 tmpBuf[pos++] = map[ num % base ]; // Last digit of {number}
420 // length of number, minus the sign character
421 PadLength -= pos - sign_is_neg - (SignChar != '\0');
424 while(PadLength-- > 0)
425 putch_cb(putch_h, PadChar), ret ++;
429 putch_cb(putch_h, '-'), ret++; // Negative sign character
431 putch_cb(putch_h, SignChar), ret++; // positive sign character
435 while( Precision-- > 0 )
436 putch_cb(putch_h, '0'), ret++;
438 putch_cb(putch_h, tmpBuf[pos]), ret++; // Reverse the order of characters
442 while(PadLength-- > 0)
443 putch_cb(putch_h, PadChar), ret ++;
449 int expand_double(double num, uint64_t *Significand, int16_t *Exponent, int *SignIsNeg)
454 uint64_t test_exp = 0xC000000000000000;
455 double test_value = -2.0f;
456 assert( *((uint64_t*)&test_value) == test_exp );
460 const uint64_t *bit_rep = (void*)#
462 *SignIsNeg = *bit_rep >> 63;
463 *Exponent = ((*bit_rep >> 52) & 0x7FF) - 1023;
464 *Significand = (*bit_rep & ((1ULL << 52)-1)) << (64-52);
466 // printf("%llx %i %i %llx\n", *bit_rep, (int)*SignIsNeg, (int)*Exponent, *Significand);
469 if( *Exponent == -1023 && *Significand != 0 )
472 if( *Exponent == 0x800 && *Significand == 0)
475 if( *Exponent == 0x800 && *Significand != 0)
485 double _longdiv(double num, double den, int *quot)
489 // printf("%llu / %llu\n", (long long int)num, (long long int)den);
492 assert(num < den*10);
497 assert( *quot < 10 );
499 // printf(" %i\n", *quot);
503 size_t _printf_ftoa_hex(printf_putch_t putch_cb, void *putch_h, long double num, int Precision, int bForcePoint, int bForceSign, int bCapitals)
505 uint64_t significand;
511 #define _putch(_ch) do{\
513 putch_cb(putch_h, toupper(_ch));\
515 putch_cb(putch_h, _ch);\
519 if( Precision == -1 )
520 Precision = (64-4)/4;
522 rv = expand_double(num, &significand, &exponent, &signisneg);
525 // 0: No errors, nothing special
530 // TODO: Subnormal = 0?
545 // int exp_ofs = exponent % 4;
546 // int exp_16 = exponent - exp_ofs;
547 //uint8_t whole = (1 << exp_ofs) | ((significand >> (64-3)) >> exp_ofs);
548 //significand <<= 3 - exp_ofs;
549 uint8_t whole = (rv != 1) ? 1 : 0;
553 else if( bForceSign )
559 _putch(cDIGITS[whole]);
560 if( significand || bForcePoint )
563 while( significand && Precision -- )
565 uint8_t val = significand >> (64-4);
566 _putch(cDIGITS[val]);
570 //ret += _printf_itoa(putch_cb, putch_h, exp_16, 16, bCapitals, TRUE, '+', 0, 0, '\0', 0);
571 ret += _printf_itoa(putch_cb, putch_h, exponent, 10, bCapitals, TRUE, '+', 0, 0, '\0', 0);
577 size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, size_t Base, enum eFPN Notation, int Precision, int bForcePoint, int bForceSign, int bCapitals)
579 uint64_t significand;
585 #define _putch(_ch) do{\
587 putch_cb(putch_h, toupper(_ch));\
589 putch_cb(putch_h, _ch);\
593 if( Base <= 1 || Base > 16 )
596 rv = expand_double(num, &significand, &exponent, &signisneg);
599 // 0: No errors, nothing special
604 // TODO: Subnormal = 0?
619 // - Used as 0/1 bools in arithmatic later on
620 bForcePoint = !!bForcePoint;
621 bForceSign = !!bForceSign;
623 // Apply default to precision
624 if( Precision == -1 )
630 // Select optimum type
631 if( Notation == FPN_SHORTEST )
634 //int first_set_sig = BSL(significand);
635 // TODO: if( num > pos(Base, 2+Precision+2+log_base(exponent) )
639 double precision_max = 1;
641 precision_max /= Base;
643 // Determine scientific's exponent and starting denominator
645 int sci_exponent = 0;
646 if( Notation == FPN_SCI )
656 else if( num >= Base )
680 else if( bForceSign )
689 num = _longdiv(num, den, &value);
690 _putch(cDIGITS[value]);
694 // Decimal point (if needed/forced)
695 if( den >= precision_max || bForcePoint )
698 while( den >= precision_max )
700 num = _longdiv(num, den, &value);
701 _putch(cDIGITS[value]);
705 if( Notation == FPN_SCI )
711 ret += _printf_itoa(putch_cb, putch_h, sci_exponent, Base, FALSE, TRUE, '+', 0, 0, '\0', FALSE);