If we cannot fail to output, it doesn't make sense to propagate errors. So if you're SURE there are no errors, use pure_cstyle_format(). If, however, output can fail, use cstyle_format(). This has a drawback of adding quite a bit of code duplication to Format.cpp. Some of it is dealt using templates, but some code still remains duplicate.
33 lines
648 B
C++
33 lines
648 B
C++
#include "arch/Serial.h"
|
|
#include <luna/Format.h>
|
|
#include <luna/Result.h>
|
|
#include <stdarg.h>
|
|
|
|
namespace Serial
|
|
{
|
|
void write(const char* str, usize size)
|
|
{
|
|
while (size--) putchar((u8)*str++);
|
|
}
|
|
|
|
void print(const char* str)
|
|
{
|
|
while (*str) putchar((u8)*str++);
|
|
}
|
|
|
|
void println(const char* str)
|
|
{
|
|
print(str);
|
|
putchar('\n');
|
|
}
|
|
|
|
usize printf(const char* format, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, format);
|
|
auto rc = pure_cstyle_format(
|
|
format, [](char c, void*) { putchar((u8)c); }, nullptr, ap);
|
|
va_end(ap);
|
|
return rc;
|
|
}
|
|
} |