1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-15 08:07:38 +00:00

LibCore: Use new format functions in some places.

This commit is contained in:
asynts 2020-10-15 13:21:23 +02:00 committed by Andreas Kling
parent 43e37c7cde
commit c9ca897a45
7 changed files with 63 additions and 61 deletions

View file

@ -166,16 +166,16 @@ bool Account::sync()
errno = 0; errno = 0;
while ((p = getpwent())) { while ((p = getpwent())) {
if (p->pw_uid == m_uid) { if (p->pw_uid == m_uid) {
new_passwd_file.appendf("%s:%s:%u:%u:%s:%s:%s\n", new_passwd_file.appendff("{}:{}:{}:{}:{}:{}:{}\n",
m_username.characters(), m_username,
m_password_hash.characters(), m_password_hash,
m_uid, m_gid, m_uid, m_gid,
m_gecos.characters(), m_gecos,
m_home_directory.characters(), m_home_directory,
m_shell.characters()); m_shell);
} else { } else {
new_passwd_file.appendf("%s:%s:%u:%u:%s:%s:%s\n", new_passwd_file.appendff("{}:{}:{}:{}:{}:{}:{}\n",
p->pw_name, p->pw_passwd, p->pw_uid, p->pw_name, p->pw_passwd, p->pw_uid,
p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_gid, p->pw_gecos, p->pw_dir,
p->pw_shell); p->pw_shell);

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/Format.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <getopt.h> #include <getopt.h>
@ -105,7 +106,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure)
const char* arg = found_option->requires_argument ? optarg : nullptr; const char* arg = found_option->requires_argument ? optarg : nullptr;
if (!found_option->accept_value(arg)) { if (!found_option->accept_value(arg)) {
fprintf(stderr, "\033[31mInvalid value for option \033[1m%s\033[22m, dude\033[0m\n", found_option->name_for_display().characters()); warnln("\033[31mInvalid value for option \033[1m{}\033[22m, dude\033[0m", found_option->name_for_display());
print_usage_and_exit(); print_usage_and_exit();
return false; return false;
} }
@ -148,7 +149,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure)
for (int j = 0; j < num_values_for_arg[i]; j++) { for (int j = 0; j < num_values_for_arg[i]; j++) {
const char* value = argv[optind++]; const char* value = argv[optind++];
if (!arg.accept_value(value)) { if (!arg.accept_value(value)) {
fprintf(stderr, "Invalid value for argument %s\n", arg.name); warnln("Invalid value for argument {}", arg.name);
print_usage_and_exit(); print_usage_and_exit();
return false; return false;
} }
@ -169,67 +170,67 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure)
void ArgsParser::print_usage(FILE* file, const char* argv0) void ArgsParser::print_usage(FILE* file, const char* argv0)
{ {
fprintf(file, "Usage:\n\t\033[1m%s\033[0m", argv0); new_out(file, "Usage:\n\t\033[1m{}\033[0m", argv0);
for (auto& opt : m_options) { for (auto& opt : m_options) {
if (opt.long_name && !strcmp(opt.long_name, "help")) if (opt.long_name && !strcmp(opt.long_name, "help"))
continue; continue;
if (opt.requires_argument) if (opt.requires_argument)
fprintf(file, " [%s %s]", opt.name_for_display().characters(), opt.value_name); new_out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
else else
fprintf(file, " [%s]", opt.name_for_display().characters()); new_out(file, " [{}]", opt.name_for_display());
} }
for (auto& arg : m_positional_args) { for (auto& arg : m_positional_args) {
bool required = arg.min_values > 0; bool required = arg.min_values > 0;
bool repeated = arg.max_values > 1; bool repeated = arg.max_values > 1;
if (required && repeated) if (required && repeated)
fprintf(file, " <%s...>", arg.name); new_out(file, " <{}...>", arg.name);
else if (required && !repeated) else if (required && !repeated)
fprintf(file, " <%s>", arg.name); new_out(file, " <{}>", arg.name);
else if (!required && repeated) else if (!required && repeated)
fprintf(file, " [%s...]", arg.name); new_out(file, " [{}...]", arg.name);
else if (!required && !repeated) else if (!required && !repeated)
fprintf(file, " [%s]", arg.name); new_out(file, " [{}]", arg.name);
} }
outln(file);
if (!m_options.is_empty()) if (!m_options.is_empty())
fprintf(file, "\nOptions:\n"); outln(file, "\nOptions:");
for (auto& opt : m_options) { for (auto& opt : m_options) {
auto print_argument = [&]() { auto print_argument = [&]() {
if (opt.value_name) { if (opt.value_name) {
if (opt.requires_argument) if (opt.requires_argument)
fprintf(file, " %s", opt.value_name); new_out(file, " {}", opt.value_name);
else else
fprintf(file, " [%s]", opt.value_name); new_out(file, " [{}]", opt.value_name);
} }
}; };
fprintf(file, "\t"); new_out(file, "\t");
if (opt.short_name) { if (opt.short_name) {
fprintf(file, "\033[1m-%c\033[0m", opt.short_name); new_out(file, "\033[1m-{}\033[0m", opt.short_name);
print_argument(); print_argument();
} }
if (opt.short_name && opt.long_name) if (opt.short_name && opt.long_name)
fprintf(file, ", "); new_out(file, ", ");
if (opt.long_name) { if (opt.long_name) {
fprintf(file, "\033[1m--%s\033[0m", opt.long_name); new_out(file, "\033[1m--{}\033[0m", opt.long_name);
print_argument(); print_argument();
} }
if (opt.help_string) if (opt.help_string)
fprintf(file, "\t%s", opt.help_string); new_out(file, "\t{}", opt.help_string);
fprintf(file, "\n"); outln(file);
} }
if (!m_positional_args.is_empty()) if (!m_positional_args.is_empty())
fprintf(file, "\nArguments:\n"); outln(file, "\nArguments:");
for (auto& arg : m_positional_args) { for (auto& arg : m_positional_args) {
fprintf(file, "\t\033[1m%s\033[0m", arg.name); new_out(file, "\t\033[1m{}\033[0m", arg.name);
if (arg.help_string) if (arg.help_string)
fprintf(file, "\t%s", arg.help_string); new_out(file, "\t{}", arg.help_string);
fprintf(file, "\n"); outln(file);
} }
} }

View file

@ -110,7 +110,7 @@ String command(const String& program, const Vector<String>& arguments, Optional<
if (WEXITSTATUS(wstatus) != 0) { if (WEXITSTATUS(wstatus) != 0) {
# ifdef DBG_FAILED_COMMANDS # ifdef DBG_FAILED_COMMANDS
dbg() << "command failed. stderr: " << read_all_from_pipe(stderr_pipe); dbgln("command failed. stderr: {}", read_all_from_pipe(stderr_pipe));
# endif # endif
return {}; return {};
} }

View file

@ -37,7 +37,7 @@ namespace Core {
NonnullRefPtr<ConfigFile> ConfigFile::get_for_lib(const String& lib_name) NonnullRefPtr<ConfigFile> ConfigFile::get_for_lib(const String& lib_name)
{ {
String directory = StandardPaths::config_directory(); String directory = StandardPaths::config_directory();
auto path = String::format("%s/lib/%s.ini", directory.characters(), lib_name.characters()); auto path = String::formatted("{}/lib/{}.ini", directory, lib_name);
return adopt(*new ConfigFile(path)); return adopt(*new ConfigFile(path));
} }
@ -45,13 +45,13 @@ NonnullRefPtr<ConfigFile> ConfigFile::get_for_lib(const String& lib_name)
NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name) NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
{ {
String directory = StandardPaths::config_directory(); String directory = StandardPaths::config_directory();
auto path = String::format("%s/%s.ini", directory.characters(), app_name.characters()); auto path = String::formatted("{}/{}.ini", directory, app_name);
return adopt(*new ConfigFile(path)); return adopt(*new ConfigFile(path));
} }
NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name) NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
{ {
auto path = String::format("/etc/%s.ini", app_name.characters()); auto path = String::formatted("/etc/{}.ini", app_name);
return adopt(*new ConfigFile(path)); return adopt(*new ConfigFile(path));
} }
@ -162,7 +162,7 @@ void ConfigFile::write_bool_entry(const String& group, const String& key, bool v
} }
void ConfigFile::write_color_entry(const String& group, const String& key, Color value) void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
{ {
write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha())); write_entry(group, key, String::formatted("{},{},{},{}", value.red(), value.green(), value.blue(), value.alpha()));
} }
bool ConfigFile::sync() bool ConfigFile::sync()
@ -175,10 +175,10 @@ bool ConfigFile::sync()
return false; return false;
for (auto& it : m_groups) { for (auto& it : m_groups) {
fprintf(fp, "[%s]\n", it.key.characters()); outln(fp, "[{}]", it.key);
for (auto& jt : it.value) for (auto& jt : it.value)
fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters()); outln(fp, "{}={}", jt.key, jt.value);
fprintf(fp, "\n"); outln(fp);
} }
fclose(fp); fclose(fp);
@ -190,10 +190,10 @@ bool ConfigFile::sync()
void ConfigFile::dump() const void ConfigFile::dump() const
{ {
for (auto& it : m_groups) { for (auto& it : m_groups) {
printf("[%s]\n", it.key.characters()); outln("[{}]", it.key);
for (auto& jt : it.value) for (auto& jt : it.value)
printf("%s=%s\n", jt.key.characters(), jt.value.characters()); outln("{}={}", jt.key, jt.value);
printf("\n"); outln();
} }
} }

View file

@ -251,7 +251,7 @@ String DateTime::to_string(const String& format) const
bool DateTime::is_before(const String& other) const bool DateTime::is_before(const String& other) const
{ {
auto now_string = String::format("%04d%02d%02d%02d%02d%02dZ", year(), month(), weekday(), hour(), minute(), second()); auto now_string = String::formatted("{:04}{:02}{:02}{:02}{:02}{:02}Z", year(), month(), weekday(), hour(), minute(), second());
return __builtin_strcasecmp(now_string.characters(), other.characters()) < 0; return __builtin_strcasecmp(now_string.characters(), other.characters()) < 0;
} }
@ -259,4 +259,5 @@ const LogStream& operator<<(const LogStream& stream, const DateTime& value)
{ {
return stream << value.to_string(); return stream << value.to_string();
} }
} }

View file

@ -96,7 +96,7 @@ String DirIterator::next_path()
String DirIterator::next_full_path() String DirIterator::next_full_path()
{ {
return String::format("%s/%s", m_path.characters(), next_path().characters()); return String::formatted("{}/{}", m_path, next_path());
} }
String find_executable_in_path(String filename) String find_executable_in_path(String filename)
@ -109,7 +109,7 @@ String find_executable_in_path(String filename)
} }
for (auto directory : String { getenv("PATH") }.split(':')) { for (auto directory : String { getenv("PATH") }.split(':')) {
auto fullpath = String::format("%s/%s", directory.characters(), filename.characters()); auto fullpath = String::formatted("{}/{}", directory, filename);
if (access(fullpath.characters(), X_OK) == 0) if (access(fullpath.characters(), X_OK) == 0)
return fullpath; return fullpath;

View file

@ -102,7 +102,7 @@ public:
int nread = m_socket->read((u8*)&length, sizeof(length)); int nread = m_socket->read((u8*)&length, sizeof(length));
if (nread == 0) { if (nread == 0) {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "RPC client disconnected"; dbgln("RPC client disconnected");
#endif #endif
shutdown(); shutdown();
return; return;
@ -112,7 +112,7 @@ public:
auto request_json = JsonValue::from_string(request); auto request_json = JsonValue::from_string(request);
if (!request_json.has_value() || !request_json.value().is_object()) { if (!request_json.has_value() || !request_json.value().is_object()) {
dbg() << "RPC client sent invalid request"; dbgln("RPC client sent invalid request");
shutdown(); shutdown();
return; return;
} }
@ -139,7 +139,7 @@ public:
auto type = request.get("type").as_string_or({}); auto type = request.get("type").as_string_or({});
if (type.is_null()) { if (type.is_null()) {
dbg() << "RPC client sent request without type field"; dbgln("RPC client sent request without type field");
return; return;
} }
@ -245,12 +245,12 @@ EventLoop::EventLoop()
if (!s_rpc_server) { if (!s_rpc_server) {
if (!start_rpc_server()) if (!start_rpc_server())
dbg() << "Core::EventLoop: Failed to start an RPC server"; dbgln("Core::EventLoop: Failed to start an RPC server");
} }
} }
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << getpid() << " Core::EventLoop constructed :)"; dbgln("{} Core::EventLoop constructed :)", getpid());
#endif #endif
} }
@ -304,7 +304,7 @@ EventLoop& EventLoop::current()
void EventLoop::quit(int code) void EventLoop::quit(int code)
{ {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop::quit(" << code << ")"; dbgln("Core::EventLoop::quit({})", code);
#endif #endif
m_exit_requested = true; m_exit_requested = true;
m_exit_code = code; m_exit_code = code;
@ -313,7 +313,7 @@ void EventLoop::quit(int code)
void EventLoop::unquit() void EventLoop::unquit()
{ {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop::unquit()"; dbgln("Core::EventLoop::unquit()");
#endif #endif
m_exit_requested = false; m_exit_requested = false;
m_exit_code = 0; m_exit_code = 0;
@ -368,7 +368,7 @@ void EventLoop::pump(WaitMode mode)
auto& event = *queued_event.event; auto& event = *queued_event.event;
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
if (receiver) if (receiver)
dbg() << "Core::EventLoop: " << *receiver << " event " << (int)event.type(); dbgln("Core::EventLoop: {} event {}", *receiver, event.type());
#endif #endif
if (!receiver) { if (!receiver) {
switch (event.type()) { switch (event.type()) {
@ -377,13 +377,13 @@ void EventLoop::pump(WaitMode mode)
return; return;
default: default:
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Event type " << event.type() << " with no receiver :("; dbgln("Event type {} with no receiver :(", event.type());
#endif #endif
break; break;
} }
} else if (event.type() == Event::Type::DeferredInvoke) { } else if (event.type() == Event::Type::DeferredInvoke) {
#ifdef DEFERRED_INVOKE_DEBUG #ifdef DEFERRED_INVOKE_DEBUG
dbg() << "DeferredInvoke: receiver = " << receiver->class_name() << "{" << receiver << "}"; dbgln("DeferredInvoke: receiver = {}", *receiver);
#endif #endif
static_cast<DeferredInvocationEvent&>(event).m_invokee(*receiver); static_cast<DeferredInvocationEvent&>(event).m_invokee(*receiver);
} else { } else {
@ -394,7 +394,7 @@ void EventLoop::pump(WaitMode mode)
if (m_exit_requested) { if (m_exit_requested) {
LOCKER(m_private->lock); LOCKER(m_private->lock);
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Exit requested. Rejigging " << (events.size() - i) << " events."; dbgln("Core::EventLoop: Exit requested. Rejigging {} events.", events.size() - i);
#endif #endif
decltype(m_queued_events) new_event_queue; decltype(m_queued_events) new_event_queue;
new_event_queue.ensure_capacity(m_queued_events.size() + events.size()); new_event_queue.ensure_capacity(m_queued_events.size() + events.size());
@ -411,7 +411,7 @@ void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
{ {
LOCKER(m_private->lock); LOCKER(m_private->lock);
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop::post_event: {" << m_queued_events.size() << "} << receiver=" << receiver << ", event=" << event; dbgln("Core::EventLoop::post_event: ({}) << receivier={}, event={}", m_queued_events.size(), receiver, event);
#endif #endif
m_queued_events.empend(receiver, move(event)); m_queued_events.empend(receiver, move(event));
} }
@ -421,7 +421,7 @@ EventLoop::SignalHandlers::SignalHandlers(int signo)
, m_original_handler(signal(signo, EventLoop::handle_signal)) , m_original_handler(signal(signo, EventLoop::handle_signal))
{ {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Registered handler for signal " << m_signo; dbgln("Core::EventLoop: Registered handler for signal {}", m_signo);
#endif #endif
} }
@ -429,7 +429,7 @@ EventLoop::SignalHandlers::~SignalHandlers()
{ {
if (m_valid) { if (m_valid) {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Unregistering handler for signal " << m_signo; dbgln("Core::EventLoop: Unregistering handler for signal {}", m_signo);
#endif #endif
signal(m_signo, m_original_handler); signal(m_signo, m_original_handler);
} }
@ -461,7 +461,7 @@ void EventLoop::dispatch_signal(int signo)
auto handlers = s_signal_handlers.find(signo); auto handlers = s_signal_handlers.find(signo);
if (handlers != s_signal_handlers.end()) { if (handlers != s_signal_handlers.end()) {
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop: dispatching signal " << signo; dbgln("Core::EventLoop: dispatching signal {}", signo);
#endif #endif
handlers->value.dispatch(); handlers->value.dispatch();
} }
@ -602,7 +602,7 @@ try_select_again:
goto try_select_again; goto try_select_again;
} }
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop::wait_for_event: " << marked_fd_count << " (" << saved_errno << ": " << strerror(saved_errno) << ")"; dbgln("Core::EventLoop::wait_for_event: {} ({}: {})", marked_fd_count, saved_errno, strerror(saved_errno));
#endif #endif
// Blow up, similar to Core::safe_syscall. // Blow up, similar to Core::safe_syscall.
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -645,7 +645,7 @@ try_select_again:
continue; continue;
} }
#ifdef EVENTLOOP_DEBUG #ifdef EVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Timer " << timer.timer_id << " has expired, sending Core::TimerEvent to " << timer.owner; dbgln("Core::EventLoop: Timer {} has expired, sending Core::TimerEvent to {}", timer.timer_id, timer.owner);
#endif #endif
post_event(*timer.owner, make<TimerEvent>(timer.timer_id)); post_event(*timer.owner, make<TimerEvent>(timer.timer_id));
if (timer.should_reload) { if (timer.should_reload) {