mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:18:11 +00:00
gml-format: Port to Core::Stream
This commit is contained in:
parent
d39552a7b7
commit
c757db9475
1 changed files with 9 additions and 20 deletions
|
@ -5,24 +5,18 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibGUI/GML/Formatter.h>
|
#include <LibGUI/GML/Formatter.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
|
||||||
ErrorOr<bool> format_file(StringView, bool);
|
static ErrorOr<bool> format_file(StringView path, bool inplace)
|
||||||
|
|
||||||
ErrorOr<bool> format_file(StringView path, bool inplace)
|
|
||||||
{
|
{
|
||||||
auto read_from_stdin = path == "-";
|
auto read_from_stdin = path == "-";
|
||||||
RefPtr<Core::File> file;
|
auto open_mode = (inplace && !read_from_stdin) ? Core::Stream::OpenMode::ReadWrite : Core::Stream::OpenMode::Read;
|
||||||
if (read_from_stdin) {
|
auto file = TRY(Core::Stream::File::open_file_or_standard_stream(path, open_mode));
|
||||||
file = Core::File::standard_input();
|
|
||||||
} else {
|
auto contents = TRY(file->read_all());
|
||||||
auto open_mode = inplace ? Core::OpenMode::ReadWrite : Core::OpenMode::ReadOnly;
|
|
||||||
file = TRY(Core::File::open(path, open_mode));
|
|
||||||
}
|
|
||||||
auto contents = file->read_all();
|
|
||||||
auto formatted_gml_or_error = GUI::GML::format_gml(contents);
|
auto formatted_gml_or_error = GUI::GML::format_gml(contents);
|
||||||
if (formatted_gml_or_error.is_error()) {
|
if (formatted_gml_or_error.is_error()) {
|
||||||
warnln("Failed to parse GML: {}", formatted_gml_or_error.error());
|
warnln("Failed to parse GML: {}", formatted_gml_or_error.error());
|
||||||
|
@ -32,14 +26,9 @@ ErrorOr<bool> format_file(StringView path, bool inplace)
|
||||||
if (inplace && !read_from_stdin) {
|
if (inplace && !read_from_stdin) {
|
||||||
if (formatted_gml == contents)
|
if (formatted_gml == contents)
|
||||||
return true;
|
return true;
|
||||||
if (!file->seek(0) || !file->truncate(0)) {
|
TRY(file->seek(0, Core::Stream::SeekMode::SetPosition));
|
||||||
warnln("Could not truncate {}: {}", path, file->error_string());
|
TRY(file->truncate(0));
|
||||||
return false;
|
TRY(file->write(formatted_gml.bytes()));
|
||||||
}
|
|
||||||
if (!file->write(formatted_gml)) {
|
|
||||||
warnln("Could not write to {}: {}", path, file->error_string());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
out("{}", formatted_gml);
|
out("{}", formatted_gml);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue