print: Add print_func() - output text w/ custom putc()
This commit is contained in:
parent
ea66b3dd3a
commit
9150109729
@ -21,7 +21,8 @@
|
||||
#ifndef PRINT_H
|
||||
#define PRINT_H
|
||||
|
||||
int print(char *fmt, ...);
|
||||
void print_init(void (*putc)(char));
|
||||
int print(char *fmt, ...);
|
||||
int print_func(void (putcf)(char), char *fmt, ...);
|
||||
|
||||
#endif /* PRINT_H */
|
||||
|
@ -26,19 +26,19 @@
|
||||
void putc_initial(char c) {
|
||||
(void)c;
|
||||
}
|
||||
void (*putc)(char) = &putc_initial;
|
||||
void (*print_putc)(char) = &putc_initial;
|
||||
|
||||
void print_init(void (*putcfn)(char)) {
|
||||
if (putcfn)
|
||||
putc = putcfn;
|
||||
print_putc = putcfn;
|
||||
}
|
||||
|
||||
void puts(const char *s)
|
||||
void puts(void (*putc)(char), const char *s)
|
||||
{
|
||||
while (*s) putc (*s++);
|
||||
}
|
||||
|
||||
void puti(int i)
|
||||
void puti(void (*putc)(char), int i)
|
||||
{
|
||||
unsigned int left;
|
||||
char buf[1 << (sizeof(int)*8) / 10];
|
||||
@ -64,10 +64,10 @@ void puti(int i)
|
||||
putc(*p);
|
||||
}
|
||||
|
||||
void putx(unsigned int i) {
|
||||
void putx(void (*putc)(char), unsigned int i) {
|
||||
int j;
|
||||
|
||||
puts("0x");
|
||||
puts(putc, "0x");
|
||||
|
||||
for (j = 0; j < 8; j++) {
|
||||
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;
|
||||
|
||||
puts("0b");
|
||||
puts(putc, "0b");
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
putc((i>>(31-j)) & 1 ? '1' : '0');
|
||||
}
|
||||
}
|
||||
|
||||
int print(char *fmt, ...) {
|
||||
int _print(void (*putc)(char), char *fmt, va_list arg) {
|
||||
char *c;
|
||||
va_list arg;
|
||||
|
||||
va_start(arg, fmt);
|
||||
for (c = fmt; *c; c++) {
|
||||
if (*c == '%') {
|
||||
switch (*++c) {
|
||||
@ -100,24 +97,24 @@ int print(char *fmt, ...) {
|
||||
putc('%');
|
||||
break;
|
||||
case 's':
|
||||
puts(va_arg(arg, char *));
|
||||
puts(putc, va_arg(arg, char *));
|
||||
break;
|
||||
case 'c':
|
||||
putc(va_arg(arg, unsigned int));
|
||||
break;
|
||||
case 'd':
|
||||
puti(va_arg(arg, int));
|
||||
puti(putc, va_arg(arg, int));
|
||||
break;
|
||||
case 'x':
|
||||
putx(va_arg(arg, unsigned int));
|
||||
putx(putc, va_arg(arg, unsigned int));
|
||||
break;
|
||||
case 'b':
|
||||
putb(va_arg(arg, unsigned int));
|
||||
putb(putc, va_arg(arg, unsigned int));
|
||||
break;
|
||||
default:
|
||||
puts("\nError: print(): Invalid formatting character: '");
|
||||
puts(putc, "\nError: print(): Invalid formatting character: '");
|
||||
putc(*c);
|
||||
puts("'\n");
|
||||
puts(putc, "'\n");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
@ -126,7 +123,28 @@ int print(char *fmt, ...) {
|
||||
putc('\r');
|
||||
}
|
||||
}
|
||||
va_end(arg);
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user