29 #include "user_config.h"
73 if(c >=
'0' && c <=
'9')
97 for (i = 0; i < (
len >> 1); i++)
99 temp = str[
len - i - 1];
100 str[
len - i - 1] = str[i];
116 if(*str >=
'a' && *str <=
'z')
117 *str -= (
'a' -
'A');
123 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
124 #error bin2num ASSUME little endian
143 int bin2num(uint8_t *str,
int strmax,
int nummin,
int base, uint8_t *nump,
int numsize,
int sign_ch)
148 int numbits = numsize * 8;
150 for(i=0;i<=nummin;++i)
156 for(i = numbits - 1; i>= 0; --i)
163 carry = ( data & (1 << (i & 7)) ) ? 1 : 0;
169 for(j=0;(j<nummin || carry) ;++j)
171 if(j >= (strmax - 2))
175 data = (data<<1) | carry;
190 for(i=0;i<nummin;++i)
194 else str[i] +=
'a'-10;
198 if(sign_ch && i <= (strmax - 2))
314 int p_ntoa(uint8_t *nump,
int numsize,
char *str,
int strmax,
int radix,
int width,
int prec,
f_t f)
316 unsigned int sign_ch;
368 ind =
bin2num((uint8_t *)str, strmax, digits, radix, nump, numsize, sign_ch);
382 int p_ftoa(
double val,
char *str,
int max,
int width,
int prec,
f_t f)
454 digits = width - idigits -
pch_ind(p) - prec -1;
456 digits = width - idigits -
pch_ind(p);
471 val -= (double) digit;
476 if(f.
b.
prec && prec > 0 )
482 val -= (double) digit;
501 int p_etoa(
double val,
char *str,
int max,
int width,
int prec,
f_t f)
507 uint8_t exp10_str[7];
597 expsize =
bin2num(exp10_str+1,
sizeof(exp10_str)-1-1, 2, 10, (uint8_t *) &exp10,
sizeof(
int), sign_ch);
614 exp10_str[ind] =
'0' + (exp % 10);
616 exp10_str[ind] =
'0' + (exp % 10);
618 exp10_str[ind] =
'0' + (exp % 10);
627 digits = width -
pch_ind(p) - prec - 6;
629 digits = width -
pch_ind(p) - 5;
643 val -= (double) digit;
647 if(f.
b.
prec && prec > 0 )
653 val -= (double) digit;
661 for(i=0;exp10_str[i];++i)
735 void _puts_pad_X(
printf_t *fn,
__memx const char *s,
int width,
int count,
int left)
788 strlen_X(
__memx const char *str)
818 #ifdef __SIZEOF_INT128__
829 __memx const char *fmtptr;
836 char buff[307 * 2 + 5 + 1];
838 char buff[
sizeof(
long long ) * 8 + 2];
864 while(*fmt ==
'#' || *fmt ==
'+' || *fmt ==
'-' || *fmt ==
' ' || *fmt ==
'0')
870 else if(!f.
b.
left && *fmt ==
'-')
872 else if(!f.
b.
space && *fmt ==
' ')
874 else if(!f.
b.
zero && *fmt ==
'0')
887 width = width*10 + *fmt++ -
'0';
899 prec = prec*10 + *fmt++ -
'0';
915 size = size*10 + *fmt++ -
'0';
916 if(size == 0 || size & 7)
924 size =
sizeof(short);
933 size =
sizeof(
long long);
943 if(spec ==
'd' || spec ==
'D')
946 nump = (uint8_t *) &numi;
953 size =
sizeof(
void *);
984 if(size ==
sizeof(
short))
986 nums = (short) va_arg(va,
int);
992 nump = (uint8_t *) &nums;
994 else if(size ==
sizeof(
int))
996 numi = (int) va_arg(va,
int);
1002 nump = (uint8_t *) &numi;
1004 else if(size ==
sizeof(
long))
1006 numl = (long) va_arg(va,
long);
1007 if(sign && numl < 0)
1012 nump = (uint8_t *) &numl;
1014 else if(size ==
sizeof(
long long))
1016 numll = (
long long) va_arg(va,
long long);
1017 if(sign && numll < 0)
1022 nump = (uint8_t *) &numll;
1024 #ifdef __SIZEOF_INT128__
1025 else if(size ==
sizeof(__uint128_t))
1027 num128 = (__uint128_t) va_arg(va, __uint128_t);
1028 if(sign && numll < 0)
1033 nump = (uint8_t *) &num128;
1036 else if(size ==
sizeof(
void *))
1038 numv = (
void *) va_arg(va,
void *);
1039 nump = (uint8_t *) &numv;
1059 dnum = va_arg(va,
double);
1084 count =
p_ntoa(nump, size, buff,
sizeof(buff), 10, width, prec, f);
1090 count =
p_ntoa(nump, size, buff,
sizeof(buff), 10, width, prec, f);
1095 count =
p_ntoa(nump, size, buff,
sizeof(buff), 2, width, prec,f);
1100 count =
p_ntoa(nump, size, buff,
sizeof(buff), 8, width, prec,f);
1108 count =
p_ntoa(nump, size, buff,
sizeof(buff), 16, width, prec,f);
1109 if(spec ==
'X' || spec ==
'P')
1116 count =
p_ftoa(dnum, buff,
sizeof(buff), width, prec, f);
1122 count =
p_etoa(dnum, buff,
sizeof(buff), width, prec, f);
1134 ptr = va_arg(va,
char *);
1140 chartmp[0] = (char) va_arg(va,
int);
1147 if(count > width && width != 0)
1156 Xptr = va_arg(va, __flash
const char *);
1158 Xptr = (
__memx const char *)
"(null)";
1159 count = strlen_X(Xptr);
1162 if(count > width && width != 0)
1164 _puts_pad_X(fn, Xptr, width, count, f.
b.
left);
1168 while(fmtptr <= fmt && *fmtptr)
1169 fn->
put(fn, *fmtptr++);
1196 str = (
char *) p->
buffer;
1198 p->
buffer = (
void *) str;
1201 *((
char *)p->
buffer) = 0;
1206 #ifdef DEFINE_PRINTF
1207 #error DEFINE_PRINTF must not be defined when testing
1220 int vsnprintf(
char* str,
size_t size,
const char *format, va_list va)
1232 fn.
buffer = (
void *) str;
1250 int snprintf(
char* str,
size_t size,
const char *format, ...)
1255 va_start(va, format);
1271 int vsnprintf_P(
char* str,
size_t size,
__memx const char *format, va_list va)
1283 fn.
buffer = (
void *) str;
1300 int snprintf_P(
char* str,
size_t size,
__memx const char *format, ...)
1305 va_start(va, format);
1306 len = vsnprintf_P(str, size, format, va);
1319 int sprintf_P(
char* str,
__memx const char *format, ...)
1324 va_start(va, format);
1326 len = vsnprintf_P(str, 1024, format, va);
1336 #ifdef DEFINE_PRINTF
1344 static void _putc_fn(
struct _printf_t *p,
char ch)
1360 printf(
const char *format, ...)
1368 va_start(va, format);
1372 return ((
int)fn.
sent);
1386 printf_P(
__memx const char *format, ...)
1394 va_start(va, format);
1398 return ((
int)fn.
sent);