72 if(c >=
'0' && c <=
'9')
95 for (i = 0; i < (len >> 1); i++)
97 temp = str[len - i - 1];
98 str[len - i - 1] = str[i];
113 if(*str >=
'a' && *str <=
'z')
114 *str -= (
'a' -
'A');
119 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ 120 #error bin2num ASSUME little endian 144 int numbits = numsize * 8;
146 for(i=0;i<=nummin;++i)
152 for(i = numbits - 1; i>= 0; --i)
159 carry = ( data & (1 << (i & 7)) ) ? 1 : 0;
165 for(j=0;(j<nummin || carry) ;++j)
167 if(j >= (strmax - 2))
171 data = (data<<1) | carry;
186 for(i=0;i<nummin;++i)
190 else str[i] +=
'a'-10;
194 if(sign_ch && i <= (strmax - 2))
241 _pch.
str[_pch.
ind++] = ch;
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;
369 ind =
bin2num((
uint8_t *)str, strmax, digits, radix, nump, numsize, sign_ch);
383 int p_ftoa(
double val,
char *str,
int max,
int width,
int prec,
f_t f)
454 digits = width - idigits -
pch_ind() - prec -1;
456 digits = width - idigits -
pch_ind();
471 val -= (double) digit;
476 if(f.
b.
prec && prec > 0 )
482 val -= (double) digit;
502 int p_etoa(
double val,
char *str,
int max,
int width,
int prec,
f_t f)
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() - prec - 6;
629 digits = width -
pch_ind() - 5;
644 val -= (double) digit;
648 if(f.
b.
prec && prec > 0 )
654 val -= (double) digit;
662 for(i=0;exp10_str[i];++i)
753 #ifdef __SIZEOF_INT128__ 764 __memx const char *fmtptr;
769 char buff[307 * 2 + 5 + 1];
771 char buff[
sizeof(
long long ) * 8 + 2];
798 while(*fmt ==
'#' || *fmt ==
'+' || *fmt ==
'-' || *fmt ==
' ' || *fmt ==
'0')
804 else if(!f.
b.
left && *fmt ==
'-')
806 else if(!f.
b.
space && *fmt ==
' ')
808 else if(!f.
b.
zero && *fmt ==
'0')
821 width = width*10 + *fmt++ -
'0';
833 prec = prec*10 + *fmt++ -
'0';
849 size = size*10 + *fmt++ -
'0';
850 if(size == 0 || size & 7)
858 size =
sizeof(short);
867 size =
sizeof(
long long);
877 if(spec ==
'd' || spec ==
'D')
888 size =
sizeof(
void *);
919 if(size ==
sizeof(
short))
921 nums = (short) va_arg(va,
int);
929 else if(size ==
sizeof(
int))
931 numi = (int) va_arg(va,
int);
939 else if(size ==
sizeof(
long))
941 numl = (long) va_arg(va,
long);
949 else if(size ==
sizeof(
long long))
951 numll = (
long long) va_arg(va,
long long);
952 if(sign && numll < 0)
959 #ifdef __SIZEOF_INT128__ 960 else if(size ==
sizeof(__uint128_t))
962 num128 = (__uint128_t) va_arg(va, __uint128_t);
963 if(sign && numll < 0)
971 else if(size ==
sizeof(
void *))
973 numv = (
void *) va_arg(va,
void *);
994 dnum = va_arg(va,
double);
1016 count =
p_ntoa(nump, size, buff,
sizeof(buff), 10, width, prec, f);
1022 count =
p_ntoa(nump, size, buff,
sizeof(buff), 10, width, prec, f);
1027 count =
p_ntoa(nump, size, buff,
sizeof(buff), 2, width, prec,f);
1032 count =
p_ntoa(nump, size, buff,
sizeof(buff), 8, width, prec,f);
1040 count =
p_ntoa(nump, size, buff,
sizeof(buff), 16, width, prec,f);
1041 if(spec ==
'X' || spec ==
'P')
1048 count =
p_ftoa(dnum, buff,
sizeof(buff), width, prec, f);
1054 count =
p_etoa(dnum, buff,
sizeof(buff), width, prec, f);
1065 ptr = va_arg(va,
char *);
1071 chartmp[0] = (char) va_arg(va,
int);
1078 if(count > width && width != 0)
1084 while(fmtptr <= fmt && *fmtptr)
1085 fn->
put(fn, *fmtptr++);
1112 str = (
char *) p->
buffer;
1114 p->
buffer = (
void *) str;
1117 *((
char *)p->
buffer) = 0;
1121 #ifdef DEFINE_PRINTF 1122 #error DEFINE_PRINTF must not be defined when testing 1135 int vsnprintf(
char* str,
size_t size,
const char *format, va_list va)
1147 fn.
buffer = (
void *) str;
1164 int snprintf(
char* str,
size_t size,
const char *format, ...)
1169 va_start(va, format);
1184 int vsnprintf_P(
char* str,
size_t size,
__memx const char *format, va_list va)
1196 fn.
buffer = (
void *) str;
1212 int snprintf_P(
char* str,
size_t size,
__memx const char *format, ...)
1217 va_start(va, format);
1218 len = vsnprintf_P(str, size, format, va);
1230 int sprintf_P(
char* str,
__memx const char *format, ...)
1235 va_start(va, format);
1237 len = vsnprintf_P(str, 1024, format, va);
1247 #ifdef DEFINE_PRINTF 1255 static void _putc_fn(
struct _printf_t *p,
char ch)
1270 printf(
const char *format, ...)
1278 va_start(va, format);
1282 return ((
int)fn.
sent);
1294 printf_P(
__memx const char *format, ...)
1302 va_start(va, format);
1306 return ((
int)fn.
sent);
MEMSPACE int bin2num(uint8_t *str, int strmax, int nummin, int base, uint8_t *nump, int numsize, int sign_ch)
Convert an unsigned number (numsize bytes in size) to ASCII in specified base Notes: No limit except ...
Data structure for character buffer with limits.
Master include file for project Includes all project includes and defines here.
MEMSPACE int WEAK_ATR isdigit(int c)
test if a character is a digit
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
MEMSPACE int snprintf(char *str, size_t size, const char *format,...)
snprintf function
MEMSPACE int p_ntoa(uint8_t *nump, int numsize, char *str, int strmax, int radix, int width, int prec, f_t f)
Convert number an base 2 .. 16 to ASCII with optional sign Notes: 1) Numbers can be any number of dig...
int ind
base of string to write to
MEMSPACE void _puts_pad(printf_t *fn, char *s, int width, int count, int left)
MEMSPACE void print_flags(f_t f)
print flags set in t_t structure
MEMSPACE void WEAK_ATR reverse(char *str)
Reverse a string in place Example: abcdef -> fedcba.
int putchar(int c)
put a character to stdout See fdevopen() sets stream->put get for TTY devices
MEMSPACE void _putc_buffer_fn(struct _printf_t *p, char ch)
_putc_buffer_fn - character output to a string buffer Used by snprintf and vsnprintf You can make _pr...
MEMSPACE int vsnprintf(char *str, size_t size, const char *format, va_list va)
vsnprintf function
MEMSPACE void WEAK_ATR strupper(char *str)
UPPERCASE a string.
Math IO functions, and verious conversion code with floating point support.
MEMSPACE void _printf_fn(printf_t *fn, __memx const char *fmt, va_list va)
vsnprintf function
MEMSPACE int pch_max_ind()
Return maximum valid index for character buffer.
MEMSPACE int printf(const char *format,...)
MEMSPACE int p_etoa(double val, char *str, int max, int width, int prec, f_t f)
MEMSPACE int pch_ind()
Return current index of character buffer with limits.
MEMSPACE void pch_init(char *str, int max)
Initialize character buffer with limits.
MEMSPACE int pch(char ch)
Put character in buffer with limits.
int max
current string index
MEMSPACE int p_ftoa(double val, char *str, int max, int width, int prec, f_t f)
void(* put)(struct _printf_t *, char)
undefine any potential macro version of these functions
p_ch_t _pch
Define data structure for character buffer with limits.