mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 07:55:07 +00:00
LibCore: Make Core::File::open() return a Result<NNRP<File>, String>
It was impractical to return a RefPtr<File> since that left us no way to extract the error string. This is usually needed for the UI, so the old static open() got basically no use.
This commit is contained in:
parent
ba3b561a40
commit
a19690170f
3 changed files with 9 additions and 7 deletions
|
@ -33,11 +33,11 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
RefPtr<File> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
|
Result<NonnullRefPtr<File>, String> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
|
||||||
{
|
{
|
||||||
auto file = File::construct(filename);
|
auto file = File::construct(filename);
|
||||||
if (!file->open_impl(mode, permissions))
|
if (!file->open_impl(mode, permissions))
|
||||||
return nullptr;
|
return String(file->error_string());
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Result.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibCore/IODevice.h>
|
#include <LibCore/IODevice.h>
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ class File final : public IODevice {
|
||||||
public:
|
public:
|
||||||
virtual ~File() override;
|
virtual ~File() override;
|
||||||
|
|
||||||
static RefPtr<File> open(const String& filename, IODevice::OpenMode, mode_t = 0644);
|
static Result<NonnullRefPtr<File>, String> open(const String& filename, IODevice::OpenMode, mode_t = 0644);
|
||||||
|
|
||||||
String filename() const { return m_filename; }
|
String filename() const { return m_filename; }
|
||||||
void set_filename(const StringView& filename) { m_filename = filename; }
|
void set_filename(const StringView& filename) { m_filename = filename; }
|
||||||
|
|
|
@ -993,12 +993,13 @@ void load_history()
|
||||||
|
|
||||||
void save_history()
|
void save_history()
|
||||||
{
|
{
|
||||||
auto history_file = Core::File::open(get_history_path(), Core::IODevice::WriteOnly, 0600);
|
auto file_or_error = Core::File::open(get_history_path(), Core::IODevice::WriteOnly, 0600);
|
||||||
if (!history_file)
|
if (file_or_error.is_error())
|
||||||
return;
|
return;
|
||||||
|
auto& file = *file_or_error.value();
|
||||||
for (const auto& line : editor.history()) {
|
for (const auto& line : editor.history()) {
|
||||||
history_file->write(line);
|
file.write(line);
|
||||||
history_file->write("\n");
|
file.write("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue