21 #define HUGE_VAL (99.e99)
24 #ifndef MAX_FIELD_WIDTH
25 #define MAX_FIELD_WIDTH 99U
28 #if PRINTF_ADVANCED_ENABLE
30 enum _debugconsole_printf_flag
32 kPRINTF_Minus = 0x01U,
34 kPRINTF_Space = 0x04U,
36 kPRINTF_Pound = 0x10U,
37 kPRINTF_LengthChar = 0x20U,
38 kPRINTF_LengthShortInt = 0x40U,
39 kPRINTF_LengthLongInt = 0x80U,
40 kPRINTF_LengthLongLongInt = 0x100U,
55 #if SCANF_ADVANCED_ENABLE
56 kSCANF_LengthChar = 0x100U,
57 kSCANF_LengthShortInt = 0x200U,
58 kSCANF_LengthLongInt = 0x400U,
59 kSCANF_LengthLongLongInt = 0x800U,
61 #if SCANF_FLOAT_ENABLE
62 kSCANF_LengthLongLongDouble = 0x1000U,
69 #pragma diag_suppress 1256
96 #if PRINTF_FLOAT_ENABLE
107 static int32_t ConvertFloatRadixNumToString(
char *numstr,
void *nump, int32_t radix, uint32_t precision_width);
112 double modf(
double input_dbl,
double *intpart_ptr);
116 #if PRINTF_ADVANCED_ENABLE
117 static uint8_t PrintGetSignChar(int32_t ival, uint32_t flags_used,
char *schar)
126 if (0U != (flags_used & (uint32_t)kPRINTF_Plus))
130 else if (0U != (flags_used & (uint32_t)kPRINTF_Space))
146 uint32_t field_width = 0;
153 if ((c >=
'0') && (c <=
'9'))
155 (field_width) = ((field_width)*10U) + ((uint32_t)c - (uint32_t)
'0');
157 #if PRINTF_ADVANCED_ENABLE
160 (field_width) = (uint32_t)va_arg(*ap, uint32_t);
176 uint32_t precision_width = 6U;
179 #if PRINTF_ADVANCED_ENABLE
180 if (
NULL != valid_precision_width)
182 *valid_precision_width =
false;
188 precision_width = 0U;
193 if ((c >=
'0') && (c <=
'9'))
195 precision_width = (precision_width * 10U) + ((uint32_t)c - (uint32_t)
'0');
196 #if PRINTF_ADVANCED_ENABLE
197 if (
NULL != valid_precision_width)
199 *valid_precision_width =
true;
203 #if PRINTF_ADVANCED_ENABLE
206 precision_width = (uint32_t)va_arg(*ap, uint32_t);
207 if (
NULL != valid_precision_width)
209 *valid_precision_width =
true;
227 return precision_width;
233 if ((c ==
'o') || (c ==
'b') || (c ==
'p') || (c ==
'u'))
243 if ((c ==
'd') || (c ==
'i'))
251 uint32_t field_width,
259 #if PRINTF_ADVANCED_ENABLE
261 if (0U != (flags_used & (uint32_t)kPRINTF_Zero))
265 cb(buf,
count, schar, 1);
268 cb(buf,
count,
'0', (
int)field_width - (
int)vlen);
273 if (0U == (flags_used & (uint32_t)kPRINTF_Minus))
275 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
278 cb(buf,
count, schar, 1);
286 cb(buf,
count, schar, 1);
289 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
291 while (
'\0' != (*vstrp))
293 cb(buf,
count, *vstrp--, 1);
295 #if PRINTF_ADVANCED_ENABLE
296 if (0U != (flags_used & (uint32_t)kPRINTF_Minus))
298 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
304 uint32_t field_width,
312 #if PRINTF_ADVANCED_ENABLE
314 if (0U != (flags_used & (uint32_t)kPRINTF_Zero))
316 if (0U != (flags_used & (uint32_t)kPRINTF_Pound))
318 cb(buf,
count,
'0', 1);
319 cb(buf,
count, (use_caps ?
'X' :
'x'), 1);
322 cb(buf,
count,
'0', (
int)field_width - (
int)vlen);
327 if (0U == (flags_used & (uint32_t)kPRINTF_Minus))
329 if (0U != (flags_used & (uint32_t)kPRINTF_Pound))
333 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
334 if (0U != (flags_used & (uint32_t)kPRINTF_Pound))
336 cb(buf,
count,
'0', 1);
337 cb(buf,
count, (use_caps ?
'X' :
'x'), 1);
343 if ((0U != (flags_used & (uint32_t)kPRINTF_Pound)) && (0U == dschar))
345 cb(buf,
count,
'0', 1);
346 cb(buf,
count, (use_caps ?
'X' :
'x'), 1);
350 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
352 while (
'\0' != (*vstrp))
354 cb(buf,
count, *vstrp--, 1);
356 #if PRINTF_ADVANCED_ENABLE
357 if (0U != (flags_used & (uint32_t)kPRINTF_Minus))
359 cb(buf,
count,
' ', (
int)field_width - (
int)vlen);
367 if ((c ==
'f') || (c ==
'F'))
377 if ((c ==
'x') || (c ==
'X'))
384 #if PRINTF_ADVANCED_ENABLE
385 static uint32_t PrintCheckFlags(
const char **s)
389 uint32_t flags_used = 0U;
391 while (
false == done)
396 flags_used |= (uint32_t)kPRINTF_Minus;
399 flags_used |= (uint32_t)kPRINTF_Plus;
402 flags_used |= (uint32_t)kPRINTF_Space;
405 flags_used |= (uint32_t)kPRINTF_Zero;
408 flags_used |= (uint32_t)kPRINTF_Pound;
422 #if PRINTF_ADVANCED_ENABLE
426 static uint32_t PrintGetLengthFlag(
const char **s)
430 uint32_t flags_used = 0U;
437 flags_used |= (uint32_t)kPRINTF_LengthShortInt;
442 flags_used |= (uint32_t)kPRINTF_LengthChar;
448 flags_used |= (uint32_t)kPRINTF_LengthLongInt;
453 flags_used |= (uint32_t)kPRINTF_LengthLongLongInt;
492 if ((c ==
' ') || (c ==
'\t') || (c ==
'\n') || (c ==
'\r') || (c ==
'\v') || (c ==
'\f'))
516 #if PRINTF_ADVANCED_ENABLE
545 #if PRINTF_ADVANCED_ENABLE
546 a = *(int64_t *)nump;
548 a = *(int32_t *)nump;
558 #if PRINTF_ADVANCED_ENABLE
559 b = (int64_t)a / (int64_t)radix;
560 c = (int64_t)a - ((int64_t)b * (int64_t)radix);
565 c = (int64_t)uc_param + 1 + (int64_t)
'0';
574 c = (int32_t)uc_param + 1 + (int32_t)
'0';
579 c = c + (int32_t)
'0';
588 #if PRINTF_ADVANCED_ENABLE
589 ua = *(uint64_t *)nump;
591 ua = *(uint32_t *)nump;
601 #if PRINTF_ADVANCED_ENABLE
602 ub = (uint64_t)ua / (uint64_t)radix;
603 uc = (uint64_t)ua - ((uint64_t)ub * (uint64_t)radix);
605 ub = ua / (uint32_t)radix;
606 uc = ua - (ub * (uint32_t)radix);
611 uc = uc + (uint32_t)
'0';
615 uc = uc - 10U + (uint32_t)(use_caps ?
'A' :
'a');
625 #if PRINTF_FLOAT_ENABLE
626 static int32_t ConvertFloatRadixNumToString(
char *numstr,
void *nump, int32_t radix, uint32_t precision_width)
652 fractpart = modf((
double)r, (
double *)&intpart);
654 for (i = 0; i < precision_width; i++)
660 fa = fractpart + (double)0.5;
661 if (fa >= pow(10, precision_width))
668 fa = fractpart - (double)0.5;
669 if (fa <= -pow(10, precision_width))
674 for (i = 0; i < precision_width; i++)
676 fb = fa / (int32_t)radix;
677 dc = (fa - (int64_t)fb * (int32_t)radix);
682 c = (int32_t)(~uc) + 1 +
'0';
692 *nstrp++ = (char)
'.';
694 a = (int32_t)intpart;
704 b = (int32_t)a / (int32_t)radix;
705 c = (int32_t)a - ((int32_t)b * (int32_t)radix);
709 c = (int32_t)(~uc) + 1 +
'0';
749 uint32_t field_width;
750 uint32_t precision_width;
756 #if PRINTF_ADVANCED_ENABLE
761 bool valid_precision_width;
767 #if PRINTF_FLOAT_ENABLE
788 cb(buf, &
count, c, 1);
796 #if PRINTF_ADVANCED_ENABLE
798 flags_used = PrintCheckFlags(&p);
805 #if PRINTF_ADVANCED_ENABLE
809 (void)precision_width;
812 #if PRINTF_ADVANCED_ENABLE
814 flags_used |= PrintGetLengthFlag(&p);
822 #if PRINTF_ADVANCED_ENABLE
823 if (flags_used & kPRINTF_LengthLongLongInt)
825 ival = (int64_t)va_arg(ap, int64_t);
830 ival = (int32_t)va_arg(ap, int32_t);
834 #if PRINTF_ADVANCED_ENABLE
835 vlen += PrintGetSignChar(ival, flags_used, &schar);
843 #if PRINTF_FLOAT_ENABLE
844 fval = (double)va_arg(ap,
double);
845 vlen = ConvertFloatRadixNumToString(vstr, &fval, 10, precision_width);
848 #if PRINTF_ADVANCED_ENABLE
849 vlen += PrintGetSignChar((int32_t)fval, flags_used, &schar);
856 (void)va_arg(ap,
double);
865 #if PRINTF_ADVANCED_ENABLE
866 if (flags_used & kPRINTF_LengthLongLongInt)
868 uval = (uint64_t)va_arg(ap, uint64_t);
873 uval = (uint32_t)va_arg(ap, uint32_t);
877 #if PRINTF_ADVANCED_ENABLE
885 #if PRINTF_ADVANCED_ENABLE
886 if (flags_used & kPRINTF_LengthLongLongInt)
888 uval = (uint64_t)va_arg(ap, uint64_t);
893 uval = (uint32_t)va_arg(ap, uint32_t);
900 #if PRINTF_ADVANCED_ENABLE
908 cval = (int32_t)va_arg(ap, uint32_t);
909 cb(buf, &
count, cval, 1);
913 sval = (
char *)va_arg(ap,
char *);
916 #if PRINTF_ADVANCED_ENABLE
917 if (valid_precision_width)
919 vlen = precision_width;
926 vlen = (int32_t)strlen(sval);
928 #if PRINTF_ADVANCED_ENABLE
929 if (!(flags_used & kPRINTF_Minus))
932 cb(buf, &
count,
' ', (
int)field_width - (
int)vlen);
935 #if PRINTF_ADVANCED_ENABLE
936 if (valid_precision_width)
938 while ((*sval) && (vlen > 0))
940 cb(buf, &
count, *sval++, 1);
944 vlen = precision_width - vlen;
949 while (
'\0' != (*sval))
951 cb(buf, &
count, *sval++, 1);
953 #if PRINTF_ADVANCED_ENABLE
957 #if PRINTF_ADVANCED_ENABLE
958 if (flags_used & kPRINTF_Minus)
960 cb(buf, &
count,
' ', field_width - vlen);
967 cb(buf, &
count, c, 1);
976 #if SCANF_FLOAT_ENABLE
977 static uint8_t StrFormatScanIsFloat(
char *c)
980 if ((
'a' == (*c)) || (
'A' == (*c)) || (
'e' == (*c)) || (
'E' == (*c)) || (
'f' == (*c)) || (
'F' == (*c)) ||
981 (
'g' == (*c)) || (
'G' == (*c)))
996 else if (*(c + 1) ==
'%')
1014 if ((
s[1] ==
'x') || (
s[1] ==
'X'))
1054 #if SCANF_ADVANCED_ENABLE
1062 case kSCANF_LengthChar:
1065 *va_arg(*args_ptr,
signed char *) = (
signed char)val;
1069 *va_arg(*args_ptr,
unsigned char *) = (
unsigned char)val;
1072 case kSCANF_LengthShortInt:
1075 *va_arg(*args_ptr,
signed short *) = (
signed short)val;
1079 *va_arg(*args_ptr,
unsigned short *) = (
unsigned short)val;
1082 case kSCANF_LengthLongInt:
1085 *va_arg(*args_ptr,
signed long int *) = (
signed long int)val;
1089 *va_arg(*args_ptr,
unsigned long int *) = (
unsigned long int)val;
1092 case kSCANF_LengthLongLongInt:
1095 *va_arg(*args_ptr,
signed long long int *) = (
signed long long int)val;
1099 *va_arg(*args_ptr,
unsigned long long int *) = (
unsigned long long int)val;
1106 *va_arg(*args_ptr,
signed int *) = (
signed int)val;
1110 *va_arg(*args_ptr,
unsigned int *) = (
unsigned int)val;
1118 *va_arg(*args_ptr,
signed int *) = (
signed int)val;
1122 *va_arg(*args_ptr,
unsigned int *) = (
unsigned int)val;
1129 #if SCANF_FLOAT_ENABLE
1130 static uint8_t StrFormatScanFillFloat(uint32_t flag, va_list *args_ptr,
double fnum)
1132 #if SCANF_ADVANCED_ENABLE
1140 if (0U != (flag & (uint32_t)kSCANF_LengthLongLongDouble))
1142 *va_arg(*args_ptr,
double *) = fnum;
1146 *va_arg(*args_ptr,
float *) = (float)fnum;
1155 uint8_t exitPending = 0U;
1161 #if SCANF_ADVANCED_ENABLE
1174 else if (
'h' == (*c))
1185 (*flag) |= (uint32_t)kSCANF_LengthChar;
1190 (*flag) |= (uint32_t)kSCANF_LengthShortInt;
1194 else if (
'l' == (*c))
1205 (*flag) |= (uint32_t)kSCANF_LengthLongLongInt;
1210 (*flag) |= (uint32_t)kSCANF_LengthLongInt;
1216 #if SCANF_FLOAT_ENABLE
1226 (*flag) |= (uint32_t)kSCANF_LengthLongLongDouble;
1231 if (((*c) >=
'0') && ((*c) <=
'9'))
1235 (*field_width) = strtoul(c, &p, 10);
1239 else if (
'd' == (*c))
1245 else if (
'u' == (*c))
1250 else if (
'o' == (*c))
1255 else if ((
'x' == (*c)))
1260 else if (
'X' == (*c))
1265 else if (
'i' == (*c))
1270 #if SCANF_FLOAT_ENABLE
1271 else if (1 == StrFormatScanIsFloat(c))
1276 else if (
'c' == (*c))
1284 else if (
's' == (*c))
1293 if (1U == exitPending)
1327 uint32_t field_width;
1329 uint32_t nassigned = 0;
1331 uint32_t n_decode = 0;
1337 uint8_t exitPending = 0;
1341 const char *p = line_ptr;
1343 #if SCANF_FLOAT_ENABLE
1353 while ((
'\0' != (*c)) && (
'\0' != (*p)))
1389 if (1U == exitPending)
1398 s = (
const char *)p;
1399 buf = va_arg(args_ptr,
char *);
1400 while ((0U != (field_width--))
1406 #if SCANF_ADVANCED_ENABLE
1415 #if SCANF_ADVANCED_ENABLE
1422 #if SCANF_ADVANCED_ENABLE
1433 buf = va_arg(args_ptr,
char *);
1434 while ((0U != (field_width--)) && (*p !=
'\0') && (0U ==
ScanIsWhiteSpace(*p)))
1436 #if SCANF_ADVANCED_ENABLE
1445 #if SCANF_ADVANCED_ENABLE
1452 #if SCANF_ADVANCED_ENABLE
1471 field_width -= added;
1474 if (strlen(p) > field_width)
1478 (void)memcpy(temp, p,
sizeof(temp) - 1U);
1479 temp[
sizeof(temp) - 1U] =
'\0';
1480 val = (int32_t)strtoul(temp, &tempEnd, (
int)base);
1481 p = p + (tempEnd - temp);
1486 val = (int32_t)strtoul(p, &tempEnd, (
int)base);
1489 n_decode += (uint32_t)p - (uint32_t)
s;
1495 #if SCANF_FLOAT_ENABLE
1499 fnum = strtod(p, (
char **)&
s);
1503 n_decode += (int)(
s) - (int)(p);
1505 nassigned += StrFormatScanFillFloat(flag, &args_ptr, fnum);
1515 return (
int)nassigned;