1
0

print: Add print_func() - output text w/ custom putc()

This commit is contained in:
Aaron Lindsay 2012-10-02 23:07:31 -04:00
parent ea66b3dd3a
commit 9150109729
2 changed files with 40 additions and 21 deletions

View File

@ -21,7 +21,8 @@
#ifndef PRINT_H #ifndef PRINT_H
#define PRINT_H #define PRINT_H
int print(char *fmt, ...);
void print_init(void (*putc)(char)); void print_init(void (*putc)(char));
int print(char *fmt, ...);
int print_func(void (putcf)(char), char *fmt, ...);
#endif /* PRINT_H */ #endif /* PRINT_H */

View File

@ -26,19 +26,19 @@
void putc_initial(char c) { void putc_initial(char c) {
(void)c; (void)c;
} }
void (*putc)(char) = &putc_initial; void (*print_putc)(char) = &putc_initial;
void print_init(void (*putcfn)(char)) { void print_init(void (*putcfn)(char)) {
if (putcfn) if (putcfn)
putc = putcfn; print_putc = putcfn;
} }
void puts(const char *s) void puts(void (*putc)(char), const char *s)
{ {
while (*s) putc (*s++); while (*s) putc (*s++);
} }
void puti(int i) void puti(void (*putc)(char), int i)
{ {
unsigned int left; unsigned int left;
char buf[1 << (sizeof(int)*8) / 10]; char buf[1 << (sizeof(int)*8) / 10];
@ -64,10 +64,10 @@ void puti(int i)
putc(*p); putc(*p);
} }
void putx(unsigned int i) { void putx(void (*putc)(char), unsigned int i) {
int j; int j;
puts("0x"); puts(putc, "0x");
for (j = 0; j < 8; j++) { for (j = 0; j < 8; j++) {
unsigned int toprint = (i >> (4*(7-j))) & 0xf; unsigned int toprint = (i >> (4*(7-j))) & 0xf;
@ -78,21 +78,18 @@ void putx(unsigned int i) {
} }
} }
void putb(unsigned int i) { void putb(void (*putc)(char), unsigned int i) {
int j; int j;
puts("0b"); puts(putc, "0b");
for (j = 0; j < 32; j++) { for (j = 0; j < 32; j++) {
putc((i>>(31-j)) & 1 ? '1' : '0'); putc((i>>(31-j)) & 1 ? '1' : '0');
} }
} }
int print(char *fmt, ...) { int _print(void (*putc)(char), char *fmt, va_list arg) {
char *c; char *c;
va_list arg;
va_start(arg, fmt);
for (c = fmt; *c; c++) { for (c = fmt; *c; c++) {
if (*c == '%') { if (*c == '%') {
switch (*++c) { switch (*++c) {
@ -100,24 +97,24 @@ int print(char *fmt, ...) {
putc('%'); putc('%');
break; break;
case 's': case 's':
puts(va_arg(arg, char *)); puts(putc, va_arg(arg, char *));
break; break;
case 'c': case 'c':
putc(va_arg(arg, unsigned int)); putc(va_arg(arg, unsigned int));
break; break;
case 'd': case 'd':
puti(va_arg(arg, int)); puti(putc, va_arg(arg, int));
break; break;
case 'x': case 'x':
putx(va_arg(arg, unsigned int)); putx(putc, va_arg(arg, unsigned int));
break; break;
case 'b': case 'b':
putb(va_arg(arg, unsigned int)); putb(putc, va_arg(arg, unsigned int));
break; break;
default: default:
puts("\nError: print(): Invalid formatting character: '"); puts(putc, "\nError: print(): Invalid formatting character: '");
putc(*c); putc(*c);
puts("'\n"); puts(putc, "'\n");
return -1; return -1;
} }
} else { } else {
@ -126,7 +123,28 @@ int print(char *fmt, ...) {
putc('\r'); putc('\r');
} }
} }
va_end(arg);
return 0; return 0;
} }
int print(char *fmt, ...) {
int ret;
va_list arg;
va_start(arg, fmt);
ret = _print(print_putc, fmt, arg);
va_end(arg);
return ret;
}
int print_func(void (*putc)(char), char *fmt, ...) {
int ret;
va_list arg;
va_start(arg, fmt);
ret = _print(putc, fmt, arg);
va_end(arg);
return ret;
}