c - va_start va_end with fatfs f_printf -
i'm trying implement function, write sdcard. should called printf additional optional arguments:
void writedatatofiles(int anything, const tchar* fmt, ...){ .... va_list args; va_start(args, fmt); f_printf(file, fmt, args); //this doesnt work vprintf(fmt, args); //this works va_end(args); f_sync(file); ... }
i try call this
int m=5 writedatatofiles(0,"my int value %i\n", m);
vprintf work, f_printf saving string "i" instead of replacing actual value
this f_printf implementation
/*-----------------------------------------------------------------------*/ /* put formatted string file */ /*-----------------------------------------------------------------------*/ int f_printf(fil* fil, /* pointer file object */ const tchar* str, /* pointer format string */ ... /* optional arguments... */ ) { va_list arp; byte f, r; uint i, j, w; ulong v; tchar c, d, s[16], *p; int res, cc; va_start(arp, str); (cc = res = 0; cc != eof; res += cc) { c = *str++; if (c == 0) break; /* end of string */ if (c != '%') { /* non escape character */ cc = f_putc(c, fil); if (cc != eof) cc = 1; continue; } w = f = 0; c = *str++; if (c == '0') { /* flag: '0' padding */ f = 1; c = *str++; } else { if (c == '-') { /* flag: left justified */ f = 2; c = *str++; } } while (isdigit(c)) { /* precision */ w = w * 10 + c - '0'; c = *str++; } if (c == 'l' || c == 'l') { /* prefix: size long int */ f |= 4; c = *str++; } if (!c) break; d = c; if (islower(d)) d -= 0x20; switch (d) { /* type is... */ case 's': /* string */ p = va_arg(arp, tchar*); (j = 0; p[j]; j++) ; res = 0; while (!(f & 2) && j++ < w) res += (cc = f_putc(' ', fil)); res += (cc = f_puts(p, fil)); while (j++ < w) res += (cc = f_putc(' ', fil)); if (cc != eof) cc = res; continue; case 'c': /* character */ cc = f_putc((tchar) va_arg(arp, int), fil); continue; case 'b': /* binary */ r = 2; break; case 'o': /* octal */ r = 8; break; case 'd': /* signed decimal */ case 'u': /* unsigned decimal */ r = 10; break; case 'x': /* hexdecimal */ r = 16; break; default: /* unknown type (passthrough) */ cc = f_putc(c, fil); continue; } /* argument , put in numeral */ v = (f & 4) ? va_arg(arp, long) : ((d == 'd') ? (long) va_arg(arp, int) : va_arg(arp, unsigned int)); if (d == 'd' && (v & 0x80000000)) { v = 0 - v; f |= 8; } = 0; { d = (tchar) (v % r); v /= r; if (d > 9) d += (c == 'x') ? 0x27 : 0x07; s[i++] = d + '0'; } while (v && < sizeof(s) / sizeof(s[0])); if (f & 8) s[i++] = '-'; j = i; d = (f & 1) ? '0' : ' '; res = 0; while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil)); res += (cc = f_putc(s[--i], fil)); while (i); while (j++ < w) res += (cc = f_putc(' ', fil)); if (cc != eof) cc = res; } va_end(arp); return (cc == eof) ? cc : res; }
edit: tried use va_list parameter f_print method, still doesnt work:
int f_printf(fil* fil, /* pointer file object */ const tchar* str, /* pointer format string */ va_list arp /* optional arguments... */ ) { byte f, r; uint i, j, w; ulong v; tchar c, d, s[16], *p; int res, cc; (cc = res = 0; cc != eof; res += cc) { c = *str++; if (c == 0) break; /* end of string */ if (c != '%') { /* non escape character */ cc = f_putc(c, fil); if (cc != eof) cc = 1; continue; } w = f = 0; c = *str++; if (c == '0') { /* flag: '0' padding */ f = 1; c = *str++; } else { if (c == '-') { /* flag: left justified */ f = 2; c = *str++; } } while (isdigit(c)) { /* precision */ w = w * 10 + c - '0'; c = *str++; } if (c == 'l' || c == 'l') { /* prefix: size long int */ f |= 4; c = *str++; } if (!c) break; d = c; if (islower(d)) d -= 0x20; switch (d) { /* type is... */ case 's': /* string */ p = va_arg(arp, tchar*); (j = 0; p[j]; j++) ; res = 0; while (!(f & 2) && j++ < w) res += (cc = f_putc(' ', fil)); res += (cc = f_puts(p, fil)); while (j++ < w) res += (cc = f_putc(' ', fil)); if (cc != eof) cc = res; continue; case 'c': /* character */ cc = f_putc((tchar) va_arg(arp, int), fil); continue; case 'b': /* binary */ r = 2; break; case 'o': /* octal */ r = 8; break; case 'd': /* signed decimal */ case 'u': /* unsigned decimal */ r = 10; break; case 'x': /* hexdecimal */ r = 16; break; default: /* unknown type (passthrough) */ cc = f_putc(c, fil); continue; } /* argument , put in numeral */ v = (f & 4) ? va_arg(arp, long) : ((d == 'd') ? (long) va_arg(arp, int) : va_arg(arp, unsigned int)); if (d == 'd' && (v & 0x80000000)) { v = 0 - v; f |= 8; } = 0; { d = (tchar) (v % r); v /= r; if (d > 9) d += (c == 'x') ? 0x27 : 0x07; s[i++] = d + '0'; } while (v && < sizeof(s) / sizeof(s[0])); if (f & 8) s[i++] = '-'; j = i; d = (f & 1) ? '0' : ' '; res = 0; while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil)); res += (cc = f_putc(s[--i], fil)); while (i); while (j++ < w) res += (cc = f_putc(' ', fil)); if (cc != eof) cc = res; } return (cc == eof) ? cc : res; }
Comments
Post a Comment