Add a String::format() and use that in place of ksprintf() in the Kernel.

You're never gonna be right 100% of the time when guessing how much buffer
space you need. This avoids having to make that type of decision in a bunch
of cases. :^)
This commit is contained in:
Andreas Kling 2019-01-30 16:28:51 +01:00
parent e9b948103d
commit 027d26cd5d
11 changed files with 40 additions and 34 deletions

View file

@ -95,6 +95,8 @@ public:
ByteBuffer to_byte_buffer() const; ByteBuffer to_byte_buffer() const;
static String format(const char*, ...);
private: private:
RetainPtr<StringImpl> m_impl; RetainPtr<StringImpl> m_impl;
}; };

View file

@ -1,5 +1,7 @@
#include "AKString.h" #include "AKString.h"
#include "StdLibExtras.h" #include "StdLibExtras.h"
#include "StringBuilder.h"
#include <LibC/stdarg.h>
namespace AK { namespace AK {
@ -92,4 +94,14 @@ unsigned String::toUInt(bool& ok) const
return value; return value;
} }
String String::format(const char* fmt, ...)
{
StringBuilder builder;
va_list ap;
va_start(ap, fmt);
builder.appendvf(fmt, ap);
va_end(ap);
return builder.build();
}
} }

View file

@ -41,13 +41,18 @@ void StringBuilder::append(char ch)
m_length += 1; m_length += 1;
} }
void StringBuilder::appendvf(const char* fmt, va_list ap)
{
printfInternal([this] (char*&, char ch) {
append(ch);
}, nullptr, fmt, ap);
}
void StringBuilder::appendf(const char* fmt, ...) void StringBuilder::appendf(const char* fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
printfInternal([this] (char*&, char ch) { appendvf(fmt, ap);
append(ch);
}, nullptr, fmt, ap);
va_end(ap); va_end(ap);
} }

View file

@ -2,6 +2,7 @@
#include "AKString.h" #include "AKString.h"
#include "Vector.h" #include "Vector.h"
#include <LibC/stdarg.h>
namespace AK { namespace AK {
@ -14,6 +15,7 @@ public:
void append(char); void append(char);
void append(const char*, size_t); void append(const char*, size_t);
void appendf(const char*, ...); void appendf(const char*, ...);
void appendvf(const char*, va_list);
String build(); String build();
ByteBuffer to_byte_buffer(); ByteBuffer to_byte_buffer();

View file

@ -205,15 +205,11 @@ char* ELFLoader::symbol_ptr(const char* name)
bool ELFLoader::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable) bool ELFLoader::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable)
{ {
ASSERT(alloc_section_hook); ASSERT(alloc_section_hook);
char namebuf[16]; return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, String::format("elf-alloc-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
ksprintf(namebuf, "elf-alloc-%s%s", is_readable ? "r" : "", is_writable ? "w" : "");
return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, namebuf);
} }
bool ELFLoader::map_section(LinearAddress laddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable) bool ELFLoader::map_section(LinearAddress laddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable)
{ {
ASSERT(alloc_section_hook); ASSERT(alloc_section_hook);
char namebuf[16]; return map_section_hook(laddr, size, alignment, offset_in_image, is_readable, is_writable, String::format("elf-map-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
ksprintf(namebuf, "elf-map-%s%s", is_readable ? "r" : "", is_writable ? "w" : "");
return map_section_hook(laddr, size, alignment, offset_in_image, is_readable, is_writable, namebuf);
} }

View file

@ -292,16 +292,10 @@ String FileDescriptor::absolute_path()
Stopwatch sw("absolute_path"); Stopwatch sw("absolute_path");
if (is_tty()) if (is_tty())
return tty()->tty_name(); return tty()->tty_name();
if (is_fifo()) { if (is_fifo())
char buf[32]; return String::format("fifo:%x", m_fifo.ptr());
ksprintf(buf, "fifo:%x", m_fifo.ptr()); if (is_character_device())
return buf; return String::format("device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name());
}
if (is_character_device()) {
char buf[128];
ksprintf(buf, "device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name());
return buf;
}
ASSERT(m_inode); ASSERT(m_inode);
return VFS::the().absolute_path(*m_inode); return VFS::the().absolute_path(*m_inode);
} }

View file

@ -14,9 +14,7 @@ MasterPTY::~MasterPTY()
String MasterPTY::pts_name() const String MasterPTY::pts_name() const
{ {
char buffer[32]; return String::format("/dev/pts/%u", m_index);
ksprintf(buffer, "/dev/pts/%u", m_index);
return buffer;
} }
ssize_t MasterPTY::read(Process&, byte* buffer, size_t size) ssize_t MasterPTY::read(Process&, byte* buffer, size_t size)

View file

@ -153,9 +153,7 @@ ByteBuffer procfs$pid_cwd(Process& process)
void ProcFS::add_process(Process& process) void ProcFS::add_process(Process& process)
{ {
InterruptDisabler disabler; InterruptDisabler disabler;
char buf[16]; auto dir = add_file(create_directory(String::format("%d", process.pid())));
ksprintf(buf, "%d", process.pid());
auto dir = add_file(create_directory(buf));
m_pid2inode.set(process.pid(), dir.index()); m_pid2inode.set(process.pid(), dir.index());
add_file(create_generated_file("vm", [&process] (SynthFSInode&) { return procfs$pid_vm(process); }), dir.index()); add_file(create_generated_file("vm", [&process] (SynthFSInode&) { return procfs$pid_vm(process); }), dir.index());
add_file(create_generated_file("vmo", [&process] (SynthFSInode&) { return procfs$pid_vmo(process); }), dir.index()); add_file(create_generated_file("vmo", [&process] (SynthFSInode&) { return procfs$pid_vmo(process); }), dir.index());

View file

@ -19,9 +19,7 @@ SlavePTY::~SlavePTY()
String SlavePTY::tty_name() const String SlavePTY::tty_name() const
{ {
char buffer[32]; return String::format("/dev/pts/%u", m_index);
ksprintf(buffer, "/dev/pts/%u", m_index);
return buffer;
} }
void SlavePTY::on_master_write(const byte* buffer, size_t size) void SlavePTY::on_master_write(const byte* buffer, size_t size)

View file

@ -508,9 +508,7 @@ void VirtualConsole::on_tty_write(const byte* data, size_t size)
String VirtualConsole::tty_name() const String VirtualConsole::tty_name() const
{ {
char buf[16]; return String::format("/dev/tty%u", m_index);
ksprintf(buf, "/dev/tty%u", m_index);
return String(buf);
} }
void VirtualConsole::set_vga_start_row(word row) void VirtualConsole::set_vga_start_row(word row)

View file

@ -191,9 +191,12 @@ void WSWindowManager::paint_window_frame(WSWindow& window)
m_back_painter->draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, title_color); m_back_painter->draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, title_color);
Color metadata_color(96, 96, 96); Color metadata_color(96, 96, 96);
char buffer[64]; m_back_painter->draw_text(
ksprintf(buffer, "%d:%d", window.pid(), window.window_id()); titleBarTitleRect,
m_back_painter->draw_text(titleBarTitleRect, buffer, Painter::TextAlignment::CenterRight, metadata_color); String::format("%d:%d", window.pid(), window.window_id()),
Painter::TextAlignment::CenterRight,
metadata_color
);
} }
void WSWindowManager::add_window(WSWindow& window) void WSWindowManager::add_window(WSWindow& window)