From c757db94756f90a526a609aafdafc382842151ac Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 14 Sep 2022 12:45:09 +0100 Subject: [PATCH] gml-format: Port to Core::Stream --- Userland/Utilities/gml-format.cpp | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/Userland/Utilities/gml-format.cpp b/Userland/Utilities/gml-format.cpp index 7c44d1d45d..3b6c1ca1d8 100644 --- a/Userland/Utilities/gml-format.cpp +++ b/Userland/Utilities/gml-format.cpp @@ -5,24 +5,18 @@ */ #include -#include +#include #include #include #include -ErrorOr format_file(StringView, bool); - -ErrorOr format_file(StringView path, bool inplace) +static ErrorOr format_file(StringView path, bool inplace) { auto read_from_stdin = path == "-"; - RefPtr file; - if (read_from_stdin) { - file = Core::File::standard_input(); - } else { - auto open_mode = inplace ? Core::OpenMode::ReadWrite : Core::OpenMode::ReadOnly; - file = TRY(Core::File::open(path, open_mode)); - } - auto contents = file->read_all(); + auto open_mode = (inplace && !read_from_stdin) ? Core::Stream::OpenMode::ReadWrite : Core::Stream::OpenMode::Read; + auto file = TRY(Core::Stream::File::open_file_or_standard_stream(path, open_mode)); + + auto contents = TRY(file->read_all()); auto formatted_gml_or_error = GUI::GML::format_gml(contents); if (formatted_gml_or_error.is_error()) { warnln("Failed to parse GML: {}", formatted_gml_or_error.error()); @@ -32,14 +26,9 @@ ErrorOr format_file(StringView path, bool inplace) if (inplace && !read_from_stdin) { if (formatted_gml == contents) return true; - if (!file->seek(0) || !file->truncate(0)) { - warnln("Could not truncate {}: {}", path, file->error_string()); - return false; - } - if (!file->write(formatted_gml)) { - warnln("Could not write to {}: {}", path, file->error_string()); - return false; - } + TRY(file->seek(0, Core::Stream::SeekMode::SetPosition)); + TRY(file->truncate(0)); + TRY(file->write(formatted_gml.bytes())); } else { out("{}", formatted_gml); }