diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-09-12 21:37:32 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-09-20 07:48:45 -0400 |
commit | f640921371a9e8dc8109f3e455a45d813c27f392 (patch) | |
tree | 5a1d03e824a65540a4f491e9d87c682db41aff6b /Userland | |
parent | 68032d27244a37aa49ace477b819ba45f72ff9c4 (diff) | |
download | serenity-f640921371a9e8dc8109f3e455a45d813c27f392.zip |
MasterWord: Port to Core::Stream
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Games/MasterWord/WordGame.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/Userland/Games/MasterWord/WordGame.cpp b/Userland/Games/MasterWord/WordGame.cpp index ca5b9e9ecc..7791177d90 100644 --- a/Userland/Games/MasterWord/WordGame.cpp +++ b/Userland/Games/MasterWord/WordGame.cpp @@ -8,7 +8,7 @@ #include <AK/QuickSort.h> #include <AK/Random.h> #include <LibConfig/Client.h> -#include <LibCore/File.h> +#include <LibCore/Stream.h> #include <LibGUI/Application.h> #include <LibGUI/MessageBox.h> #include <LibGUI/Painter.h> @@ -159,18 +159,25 @@ bool WordGame::is_in_dictionary(AK::StringView guess) void WordGame::read_words() { m_words.clear(); - auto response = Core::File::open("/res/words.txt", Core::OpenMode::ReadOnly); - if (response.is_error()) { + + auto try_load_words = [&]() -> ErrorOr<void> { + auto response = TRY(Core::Stream::File::open("/res/words.txt"sv, Core::Stream::OpenMode::Read)); + auto words_file = TRY(Core::Stream::BufferedFile::create(move(response))); + Array<u8, 128> buffer; + + while (!words_file->is_eof()) { + auto current_word = TRY(words_file->read_line(buffer)); + if (!current_word.starts_with('#') and current_word.length() > 0) + m_words.ensure(current_word.length()).append(current_word.to_uppercase_string()); + } + + return {}; + }; + + if (try_load_words().is_error()) { GUI::MessageBox::show(nullptr, "Could not read /res/words.txt.\nPlease ensure this file exists and restart MasterWord."sv, "MasterWord"sv); exit(0); } - auto words_file = response.value(); - - while (!words_file->eof()) { - auto current_word = words_file->read_line(); - if (!current_word.starts_with('#') and current_word.length() > 0) - m_words.ensure(current_word.length()).append(current_word.to_uppercase()); - } } Optional<String> WordGame::random_word(size_t length) |