fortune: Port to Core::Stream

This commit is contained in:
Sam Atkins 2022-09-14 12:35:11 +01:00 committed by Linus Groh
parent 813fc10aae
commit d39552a7b7

View file

@ -12,7 +12,7 @@
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h> #include <LibCore/DateTime.h>
#include <LibCore/File.h> #include <LibCore/Stream.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <stdio.h> #include <stdio.h>
@ -75,19 +75,19 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
TRY(Core::System::pledge("stdio rpath")); TRY(Core::System::pledge("stdio rpath"));
char const* path = "/res/fortunes.json"; StringView path = "/res/fortunes.json"sv;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.set_general_help("Open a fortune cookie, receive a free quote for the day!"); args_parser.set_general_help("Open a fortune cookie, receive a free quote for the day!");
args_parser.add_positional_argument(path, "Path to JSON file with quotes (/res/fortunes.json by default)", "path", Core::ArgsParser::Required::No); args_parser.add_positional_argument(path, "Path to JSON file with quotes (/res/fortunes.json by default)", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments); args_parser.parse(arguments);
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
TRY(Core::System::unveil("/etc/timezone", "r")); TRY(Core::System::unveil("/etc/timezone", "r"));
TRY(Core::System::unveil(nullptr, nullptr)); TRY(Core::System::unveil(nullptr, nullptr));
auto file_contents = file->read_all(); auto file_contents = TRY(file->read_all());
auto json = TRY(JsonValue::from_string(file_contents)); auto json = TRY(JsonValue::from_string(file_contents));
if (!json.is_array()) { if (!json.is_array()) {
warnln("{} does not contain an array of quotes", path); warnln("{} does not contain an array of quotes", path);