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 done = false;
|
||||||
bool right = true;
|
bool right = true;
|
||||||
int width = 0;
|
int width = 0;
|
||||||
|
char pad = ' ';
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
char c = *r++;
|
char c = *r++;
|
||||||
@@ -307,6 +308,8 @@ void console::vprintf(const char *fmt, va_list args)
|
|||||||
case '%': *w = '%'; done = true; break;
|
case '%': *w = '%'; done = true; break;
|
||||||
|
|
||||||
case '0':
|
case '0':
|
||||||
|
if (width == 0) pad = '0';
|
||||||
|
// else fall through
|
||||||
case '1':
|
case '1':
|
||||||
case '2':
|
case '2':
|
||||||
case '3':
|
case '3':
|
||||||
@@ -319,10 +322,10 @@ void console::vprintf(const char *fmt, va_list args)
|
|||||||
|
|
||||||
case '-': right = !right; break;
|
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 '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': {
|
case 's': {
|
||||||
const char *s = va_arg(args, const char*);
|
const char *s = va_arg(args, const char*);
|
||||||
@@ -333,10 +336,10 @@ void console::vprintf(const char *fmt, va_list args)
|
|||||||
|
|
||||||
case 'l':
|
case 'l':
|
||||||
switch (*r++) {
|
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 '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:
|
default:
|
||||||
done = true;
|
done = true;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <algorithm>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -28,10 +29,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void put_hex(T x, int width = 0);
|
void put_hex(T x, int width = 0, char pad = ' ');
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void put_dec(T x, int width = 0);
|
void put_dec(T x, int width = 0, char pad = ' ');
|
||||||
|
|
||||||
void echo();
|
void echo();
|
||||||
|
|
||||||
@@ -52,22 +53,26 @@ inline console * console::get() { return &g_console; }
|
|||||||
extern const char digits[];
|
extern const char digits[];
|
||||||
|
|
||||||
template <typename T>
|
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;
|
static const int chars = sizeof(x) * 2;
|
||||||
char message[chars + 1];
|
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;
|
message[chars] = 0;
|
||||||
|
|
||||||
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);
|
puts(message + (chars - len));
|
||||||
if (-width > chars) for(int i=0; i<(-width-chars); ++i) puts(" ");
|
if (-width > len) for(int i=0; i<(-width-len); ++i) putc(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
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;
|
static const int chars = sizeof(x) * 3;
|
||||||
char message[chars + 1];
|
char message[chars + 1];
|
||||||
@@ -80,7 +85,7 @@ void console::put_dec(T x, int width)
|
|||||||
length += 1;
|
length += 1;
|
||||||
} while (x != 0);
|
} 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);
|
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;
|
uint32_t header = (base[3] >> 16) & 0x7f;
|
||||||
bool multi = ((base[3] >> 16) & 0x80) == 0x80;
|
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);
|
bus, dev, func, devclass, subclass, vendor, device);
|
||||||
|
|
||||||
if (header == 0) {
|
if (header == 0) {
|
||||||
log::debug(logs::devices, " Interrupt: %d", (base[15] >> 8) & 0xff);
|
log::debug(logs::devices, " Interrupt: %d", (base[15] >> 8) & 0xff);
|
||||||
for (int i = 0; i < 5; ++i)
|
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;
|
return multi;
|
||||||
|
|||||||
@@ -114,3 +114,9 @@ memory_manager::pop_free(unsigned size)
|
|||||||
block->next = nullptr;
|
block->next = nullptr;
|
||||||
return block;
|
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.
|
/// Free kernel space memory.
|
||||||
/// \arg p The pointer to free
|
/// \arg p The pointer to free
|
||||||
inline void kfree(void *p) { g_kernel_memory_manager.free(p); }
|
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>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
inline void * operator new (size_t, void *p) throw() { return p; }
|
|
||||||
|
|
||||||
using addr_t = uint64_t;
|
using addr_t = uint64_t;
|
||||||
|
|
||||||
namespace kutil {
|
namespace kutil {
|
||||||
|
|||||||
Reference in New Issue
Block a user