From: Amelia Coutard Date: Sun, 9 Oct 2022 01:06:30 +0000 (+0200) Subject: Changed the printing code a bit, to allow for a format argument when printing (unused... X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=27e2fae96245e778d12d0d9cff41ff94e40abe47;p=voyage-au-centre-des-fichiers.git Changed the printing code a bit, to allow for a format argument when printing (unused for now) --- diff --git a/src/serial.cpp b/src/serial.cpp index 5a8e8df..faa4113 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -39,32 +39,35 @@ void os::write_serial(std::uint8_t v) { void os::printc(char c) { write_serial(c); } -void os::print(const char* str) { - for (std::size_t i = 0; str[i] != '\0'; i++) { - os::printc(str[i]); +void os::print(const char* format, const char* val) { + os::assert(format[0] == '\0', "Format string unsupported. TODO."); + for (std::size_t i = 0; val[i] != '\0'; i++) { + os::printc(val[i]); } } -void os::print(std::uint64_t v) { +void os::print(const char* format, std::uint64_t val) { + os::assert(format[0] == '\0', "Format string unsupported. TODO."); for (int i = 60; i >= 0; i -= 4) { - const int c = (v >> i) & 0xF; - os::printc(c < 10 ? c + '0' : c + 'A' - 10); + const int v = (val >> i) & 0xF; + os::printc(v < 10 ? v + '0' : v - 10 + 'a'); } } -void os::print(std::int64_t v) { - if (v < 0) { +void os::print(const char* format, std::int64_t val) { + os::assert(format[0] == '\0', "Format string unsupported. TODO."); + if (val < 0) { os::printc('-'); - os::print(std::uint64_t(-v)); + os::print(format, std::uint64_t(-val)); } else { os::printc(' '); - os::print(std::uint64_t(v)); + os::print(format, std::uint64_t(val)); } } -void os::print(std::uint32_t v) { os::print(std::uint64_t(v)); } -void os::print(std::uint16_t v) { os::print(std::uint64_t(v)); } -void os::print(std::uint8_t v) { os::print(std::uint64_t(v)); } -void os::print(std::int32_t v) { os::print(std::int64_t(v)); } -void os::print(std::int16_t v) { os::print(std::int64_t(v)); } -void os::print(std::int8_t v) { os::print(std::int64_t(v)); } +void os::print(const char* format, std::uint32_t val) { os::print(format, std::uint64_t(val)); } +void os::print(const char* format, std::uint16_t val) { os::print(format, std::uint64_t(val)); } +void os::print(const char* format, std::uint8_t val) { os::print(format, std::uint64_t(val)); } +void os::print(const char* format, std::int32_t val) { os::print(format, std::int64_t(val)); } +void os::print(const char* format, std::int16_t val) { os::print(format, std::int64_t(val)); } +void os::print(const char* format, std::int8_t val) { os::print(format, std::int64_t(val)); } void os::assert(bool cond, const char* diagnostic) { if (!cond) { os::print("Error: "); diff --git a/src/serial.hpp b/src/serial.hpp index 07b61c5..db4f368 100644 --- a/src/serial.hpp +++ b/src/serial.hpp @@ -14,15 +14,18 @@ bool serial_transmit_empty(); void write_serial(std::uint8_t v); void printc(char c); -void print(const char* str); -void print(std::uint64_t v); -void print(std::uint32_t v); -void print(std::uint16_t v); -void print(std::uint8_t v); -void print(std::int64_t v); -void print(std::int32_t v); -void print(std::int16_t v); -void print(std::int8_t v); +void print(const char* format, const char* val); +void print(const char* format, std::uint64_t val); +void print(const char* format, std::int64_t val); +void print(const char* format, std::uint32_t val); +void print(const char* format, std::int32_t val); +void print(const char* format, std::uint16_t val); +void print(const char* format, std::int16_t val); +void print(const char* format, std::uint8_t val); +void print(const char* format, std::int8_t val); +template void print(const T& val) { + print("", val); +} void assert(bool cond, const char* diagnostic); }