BAN: Allow String::formatted to fail

This commit is contained in:
Bananymous 2024-06-25 09:48:13 +03:00
parent b1869bced4
commit 67dfe0bcf3
10 changed files with 25 additions and 22 deletions

View File

@ -26,10 +26,15 @@ namespace BAN
~String() { clear(); } ~String() { clear(); }
template<typename... Args> template<typename... Args>
static String formatted(const char* format, const Args&... args) static BAN::ErrorOr<String> formatted(const char* format, Args&&... args)
{ {
size_type length = 0;
BAN::Formatter::print([&](char) { length++; }, format, BAN::forward<Args>(args)...);
String result; String result;
BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, args...); TRY(result.reserve(length));
BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, BAN::forward<Args>(args)...);
return result; return result;
} }

View File

@ -41,7 +41,7 @@ namespace Kernel
FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp) FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp)
: CharacterDevice(mode, uid, gid) : CharacterDevice(mode, uid, gid)
, m_name(BAN::String::formatted("fb{}", minor(rdev))) , m_name(MUST(BAN::String::formatted("fb{}", minor(rdev))))
, m_rdev(rdev) , m_rdev(rdev)
, m_video_memory_paddr(paddr) , m_video_memory_paddr(paddr)
, m_width(width) , m_width(width)

View File

@ -29,7 +29,7 @@ namespace Kernel
BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process) BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = TRY(BAN::String::formatted("{}", process.pid()));
auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid())); auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid()));
TRY(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path)); TRY(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
return {}; return {};
@ -37,7 +37,7 @@ namespace Kernel
void ProcFileSystem::on_process_delete(Process& process) void ProcFileSystem::on_process_delete(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = MUST(BAN::String::formatted("{}", process.pid()));
auto inode = MUST(root_inode()->find_inode(path)); auto inode = MUST(root_inode()->find_inode(path));
static_cast<ProcPidInode*>(inode.ptr())->cleanup(); static_cast<ProcPidInode*>(inode.ptr())->cleanup();

View File

@ -12,7 +12,7 @@ namespace Kernel::Input
PS2Device::PS2Device(PS2Controller& controller) PS2Device::PS2Device(PS2Controller& controller)
: CharacterDevice(0440, 0, 901) : CharacterDevice(0440, 0, 901)
, m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device())) , m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device()))
, m_name(BAN::String::formatted("input{}", minor(m_rdev))) , m_name(MUST(BAN::String::formatted("input{}", minor(m_rdev))))
, m_controller(controller) , m_controller(controller)
{ } { }

View File

@ -21,7 +21,7 @@ namespace Kernel
, m_first_block(first_block) , m_first_block(first_block)
, m_last_block(last_block) , m_last_block(last_block)
, m_attributes(attr) , m_attributes(attr)
, m_name(BAN::String::formatted("{}{}", name_prefix, index)) , m_name(MUST(BAN::String::formatted("{}{}", name_prefix, index)))
, m_rdev(makedev(major(device->rdev()), index)) , m_rdev(makedev(major(device->rdev()), index))
{ {
memcpy(m_label, label, sizeof(m_label)); memcpy(m_label, label, sizeof(m_label));

View File

@ -179,11 +179,10 @@ namespace Kernel
SerialTTY::SerialTTY(Serial serial) SerialTTY::SerialTTY(Serial serial)
: TTY(0600, 0, 0) : TTY(0600, 0, 0)
, m_name(MUST(BAN::String::formatted("ttyS{}", minor(rdev()))))
, m_serial(serial) , m_serial(serial)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {}
m_name = BAN::String::formatted("ttyS{}", minor(rdev()));
}
BAN::ErrorOr<BAN::RefPtr<SerialTTY>> SerialTTY::create(Serial serial) BAN::ErrorOr<BAN::RefPtr<SerialTTY>> SerialTTY::create(Serial serial)
{ {

View File

@ -43,11 +43,10 @@ namespace Kernel
VirtualTTY::VirtualTTY(TerminalDriver* driver) VirtualTTY::VirtualTTY(TerminalDriver* driver)
: TTY(0600, 0, 0) : TTY(0600, 0, 0)
, m_name(MUST(BAN::String::formatted("tty{}", minor(rdev()))))
, m_terminal_driver(driver) , m_terminal_driver(driver)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {
m_name = BAN::String::formatted("tty{}", minor(rdev()));
m_width = m_terminal_driver->width(); m_width = m_terminal_driver->width();
m_height = m_terminal_driver->height(); m_height = m_terminal_driver->height();

View File

@ -41,7 +41,7 @@ BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
if (command[i] == '?') if (command[i] == '?')
{ {
i++; i++;
return BAN::String::formatted("{}", last_return); return MUST(BAN::String::formatted("{}", last_return));
} }
if (isalnum(command[i])) if (isalnum(command[i]))
{ {

View File

@ -52,7 +52,7 @@ Config parse_config()
return config; return config;
} }
auto config_path = BAN::String::formatted("{}/.config/WindowServer.conf", home_env); auto config_path = MUST(BAN::String::formatted("{}/.config/WindowServer.conf", home_env));
FILE* fconfig = fopen(config_path.data(), "r"); FILE* fconfig = fopen(config_path.data(), "r");
if (!fconfig) if (!fconfig)
{ {

View File

@ -72,14 +72,14 @@ BAN::String build_access_string(mode_t mode)
BAN::String build_hard_links_string(nlink_t links) BAN::String build_hard_links_string(nlink_t links)
{ {
return BAN::String::formatted("{}", links); return MUST(BAN::String::formatted("{}", links));
} }
BAN::String build_owner_name_string(uid_t uid) BAN::String build_owner_name_string(uid_t uid)
{ {
struct passwd* passwd = getpwuid(uid); struct passwd* passwd = getpwuid(uid);
if (passwd == nullptr) if (passwd == nullptr)
return BAN::String::formatted("{}", uid); return MUST(BAN::String::formatted("{}", uid));
return BAN::String(BAN::StringView(passwd->pw_name)); return BAN::String(BAN::StringView(passwd->pw_name));
} }
@ -87,13 +87,13 @@ BAN::String build_owner_group_string(gid_t gid)
{ {
struct group* grp = getgrgid(gid); struct group* grp = getgrgid(gid);
if (grp == nullptr) if (grp == nullptr)
return BAN::String::formatted("{}", gid); return MUST(BAN::String::formatted("{}", gid));
return BAN::String(BAN::StringView(grp->gr_name)); return BAN::String(BAN::StringView(grp->gr_name));
} }
BAN::String build_size_string(off_t size) BAN::String build_size_string(off_t size)
{ {
return BAN::String::formatted("{}", size); return MUST(BAN::String::formatted("{}", size));
} }
BAN::String build_month_string(BAN::Time time) BAN::String build_month_string(BAN::Time time)
@ -104,15 +104,15 @@ BAN::String build_month_string(BAN::Time time)
BAN::String build_day_string(BAN::Time time) BAN::String build_day_string(BAN::Time time)
{ {
return BAN::String::formatted("{}", time.day); return MUST(BAN::String::formatted("{}", time.day));
} }
BAN::String build_time_string(BAN::Time time) BAN::String build_time_string(BAN::Time time)
{ {
static uint32_t current_year = ({ timespec real_time; clock_gettime(CLOCK_REALTIME, &real_time); BAN::from_unix_time(real_time.tv_sec).year; }); static uint32_t current_year = ({ timespec real_time; clock_gettime(CLOCK_REALTIME, &real_time); BAN::from_unix_time(real_time.tv_sec).year; });
if (time.year != current_year) if (time.year != current_year)
return BAN::String::formatted("{}", time.year); return MUST(BAN::String::formatted("{}", time.year));
return BAN::String::formatted("{2}:{2}", time.hour, time.minute); return MUST(BAN::String::formatted("{2}:{2}", time.hour, time.minute));
} }
int list_directory(const BAN::String& path, config_t config) int list_directory(const BAN::String& path, config_t config)
@ -213,7 +213,7 @@ int list_directory(const BAN::String& path, config_t config)
GET_ENTRY_STRING(day, time); GET_ENTRY_STRING(day, time);
GET_ENTRY_STRING(time, time); GET_ENTRY_STRING(time, time);
full_entry.full_name = BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name); full_entry.full_name = MUST(BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name));
MUST(full_entries.push_back(BAN::move(full_entry))); MUST(full_entries.push_back(BAN::move(full_entry)));
} }