mirror of https://github.com/tailix/libkernaux.git
Add function "kernaux_console_printf"
This commit is contained in:
parent
8197388340
commit
fac70421d7
|
@ -23,7 +23,7 @@ API
|
|||
|
||||
* [Simple command line parser](/include/kernaux/cmdline.h) *(work in progress)*
|
||||
* [Multiboot 2 (GRUB 2) information parser](/include/kernaux/multiboot2.h)
|
||||
* [Serial console](/include/kernaux/console.h)
|
||||
* [Serial console](/include/kernaux/console.h) *(work in progress)*
|
||||
* [Page Frame Allocator](/include/kernaux/pfa.h) *(work in progress)*
|
||||
* ELF utils *(planned)*
|
||||
* [Architecture-specific helpers](/include/kernaux/arch/)
|
||||
|
|
|
@ -6,6 +6,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
void kernaux_console_print(const char *s);
|
||||
void kernaux_console_printf(const char *format, ...);
|
||||
void kernaux_console_putc(char c);
|
||||
void kernaux_console_puts(const char *s);
|
||||
void kernaux_console_write(const char *data, unsigned int size);
|
||||
|
|
|
@ -31,3 +31,62 @@ void kernaux_console_write(const char *const data, const unsigned int size)
|
|||
kernaux_console_putc(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void kernaux_console_printf(const char *format, ...)
|
||||
{
|
||||
char **arg = (char **) &format;
|
||||
int c;
|
||||
char buf[20];
|
||||
arg++;
|
||||
|
||||
while ((c = *format++) != 0)
|
||||
{
|
||||
if (c == '\n') {
|
||||
if (*format != 0) {
|
||||
kernaux_console_putc('\n');
|
||||
}
|
||||
}
|
||||
else if (c != '%') {
|
||||
kernaux_console_putc(c);
|
||||
}
|
||||
else {
|
||||
char *p, *p2;
|
||||
int pad0 = 0, pad = 0;
|
||||
c = *format++;
|
||||
if (c == '0')
|
||||
{
|
||||
pad0 = 1;
|
||||
c = *format++;
|
||||
}
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
pad = c - '0';
|
||||
c = *format++;
|
||||
}
|
||||
switch (c)
|
||||
{
|
||||
case 'd':
|
||||
case 'u':
|
||||
case 'x':
|
||||
kernaux_itoa(*((int*)arg++), buf, c);
|
||||
p = buf;
|
||||
goto string;
|
||||
break;
|
||||
case 's':
|
||||
p = *arg++;
|
||||
if (! p)
|
||||
p = "(null)";
|
||||
string:
|
||||
for (p2 = p; *p2; p2++);
|
||||
for (; p2 < p + pad; p2++)
|
||||
kernaux_console_putc(pad0 ? '0' : ' ');
|
||||
while (*p)
|
||||
kernaux_console_putc(*p++);
|
||||
break;
|
||||
default:
|
||||
kernaux_console_putc(*((int *) arg++));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue