mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
Add better number formatting to printf
This commit is contained in:
@@ -300,6 +300,7 @@ void console::vprintf(const char *fmt, va_list args)
|
||||
bool done = false;
|
||||
bool right = true;
|
||||
int width = 0;
|
||||
char pad = ' ';
|
||||
|
||||
while (!done) {
|
||||
char c = *r++;
|
||||
@@ -307,6 +308,8 @@ void console::vprintf(const char *fmt, va_list args)
|
||||
case '%': *w = '%'; done = true; break;
|
||||
|
||||
case '0':
|
||||
if (width == 0) pad = '0';
|
||||
// else fall through
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
@@ -319,10 +322,10 @@ void console::vprintf(const char *fmt, va_list args)
|
||||
|
||||
case '-': right = !right; break;
|
||||
|
||||
case 'x': put_hex<uint32_t>(va_arg(args, uint32_t), right ? width : -width); done = true; break;
|
||||
case 'x': put_hex<uint32_t>(va_arg(args, uint32_t), right ? width : -width, pad); done = true; break;
|
||||
|
||||
case 'd':
|
||||
case 'u': put_dec<uint32_t>(va_arg(args, uint32_t), right ? width : -width); done = true; break;
|
||||
case 'u': put_dec<uint32_t>(va_arg(args, uint32_t), right ? width : -width, pad); done = true; break;
|
||||
|
||||
case 's': {
|
||||
const char *s = va_arg(args, const char*);
|
||||
@@ -333,10 +336,10 @@ void console::vprintf(const char *fmt, va_list args)
|
||||
|
||||
case 'l':
|
||||
switch (*r++) {
|
||||
case 'x': put_hex<uint64_t>(va_arg(args, uint64_t), right ? width : -width); done = true; break;
|
||||
case 'x': put_hex<uint64_t>(va_arg(args, uint64_t), right ? width : -width, pad); done = true; break;
|
||||
|
||||
case 'd':
|
||||
case 'u': put_dec<uint32_t>(va_arg(args, uint64_t), right ? width : -width); done = true; break;
|
||||
case 'u': put_dec<uint32_t>(va_arg(args, uint64_t), right ? width : -width, pad); done = true; break;
|
||||
|
||||
default:
|
||||
done = true;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -28,10 +29,10 @@ public:
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void put_hex(T x, int width = 0);
|
||||
void put_hex(T x, int width = 0, char pad = ' ');
|
||||
|
||||
template <typename T>
|
||||
void put_dec(T x, int width = 0);
|
||||
void put_dec(T x, int width = 0, char pad = ' ');
|
||||
|
||||
void echo();
|
||||
|
||||
@@ -52,22 +53,26 @@ inline console * console::get() { return &g_console; }
|
||||
extern const char digits[];
|
||||
|
||||
template <typename T>
|
||||
void console::put_hex(T x, int width)
|
||||
void console::put_hex(T x, int width, char pad)
|
||||
{
|
||||
static const int chars = sizeof(x) * 2;
|
||||
char message[chars + 1];
|
||||
for (int i=0; i<chars; ++i) {
|
||||
message[chars - i - 1] = digits[(x >> (i*4)) & 0xf];
|
||||
|
||||
int len = 1;
|
||||
for (int i = chars - 1; i >= 0; --i) {
|
||||
uint8_t nibble = (x >> (i*4)) & 0xf;
|
||||
if (nibble) len = std::max(i + 1, len);
|
||||
message[chars - i - 1] = digits[nibble];
|
||||
}
|
||||
message[chars] = 0;
|
||||
|
||||
if (width > chars) for(int i=0; i<(width-chars); ++i) puts(" ");
|
||||
puts(message);
|
||||
if (-width > chars) for(int i=0; i<(-width-chars); ++i) puts(" ");
|
||||
if (width > len) for(int i=0; i<(width-len); ++i) putc(pad);
|
||||
puts(message + (chars - len));
|
||||
if (-width > len) for(int i=0; i<(-width-len); ++i) putc(' ');
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void console::put_dec(T x, int width)
|
||||
void console::put_dec(T x, int width, char pad)
|
||||
{
|
||||
static const int chars = sizeof(x) * 3;
|
||||
char message[chars + 1];
|
||||
@@ -80,7 +85,7 @@ void console::put_dec(T x, int width)
|
||||
length += 1;
|
||||
} while (x != 0);
|
||||
|
||||
if (width > length) for(int i=0; i<(width-length); ++i) puts(" ");
|
||||
if (width > length) for(int i=0; i<(width-length); ++i) putc(pad);
|
||||
puts(++p);
|
||||
if (-width > length) for(int i=0; i<(-width-length); ++i) puts(" ");
|
||||
if (-width > length) for(int i=0; i<(-width-length); ++i) putc(' ');
|
||||
}
|
||||
|
||||
@@ -244,13 +244,13 @@ check_function(uint32_t *group, int bus, int dev, int func)
|
||||
uint32_t header = (base[3] >> 16) & 0x7f;
|
||||
bool multi = ((base[3] >> 16) & 0x80) == 0x80;
|
||||
|
||||
log::info(logs::devices, "Found PCIe device at %2d:%d:%d of type %d.%d id %x:%x",
|
||||
log::info(logs::devices, "Found PCIe device at %02d:%02d:%d of type %d.%d id %04x:%04x",
|
||||
bus, dev, func, devclass, subclass, vendor, device);
|
||||
|
||||
if (header == 0) {
|
||||
log::debug(logs::devices, " Interrupt: %d", (base[15] >> 8) & 0xff);
|
||||
for (int i = 0; i < 5; ++i)
|
||||
log::debug(logs::devices, " BAR %d: %x", i + 1, base[i + 4]);
|
||||
log::debug(logs::devices, " BAR %d: %08x", i + 1, base[i + 4]);
|
||||
}
|
||||
|
||||
return multi;
|
||||
|
||||
@@ -114,3 +114,9 @@ memory_manager::pop_free(unsigned size)
|
||||
block->next = nullptr;
|
||||
return block;
|
||||
}
|
||||
|
||||
void * operator new (size_t, void *p) { return p; }
|
||||
void * operator new (size_t n) { return g_kernel_memory_manager.allocate(n); }
|
||||
void * operator new[] (size_t n) { return g_kernel_memory_manager.allocate(n); }
|
||||
void operator delete (void *p) { return g_kernel_memory_manager.free(p); }
|
||||
void operator delete[] (void *p){ return g_kernel_memory_manager.free(p); }
|
||||
|
||||
@@ -65,8 +65,3 @@ inline void * kalloc(size_t length) { return g_kernel_memory_manager.allocate(le
|
||||
/// Free kernel space memory.
|
||||
/// \arg p The pointer to free
|
||||
inline void kfree(void *p) { g_kernel_memory_manager.free(p); }
|
||||
|
||||
inline void * operator new (size_t n) { return g_kernel_memory_manager.allocate(n); }
|
||||
inline void * operator new[] (size_t n) { return g_kernel_memory_manager.allocate(n); }
|
||||
inline void operator delete (void *p) { return g_kernel_memory_manager.free(p); }
|
||||
inline void operator delete[] (void *p){ return g_kernel_memory_manager.free(p); }
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
inline void * operator new (size_t, void *p) throw() { return p; }
|
||||
|
||||
using addr_t = uint64_t;
|
||||
|
||||
namespace kutil {
|
||||
|
||||
Reference in New Issue
Block a user