2023-04-12 19:46:10 +00:00
|
|
|
#pragma once
|
|
|
|
#include <bits/open-flags.h>
|
|
|
|
#include <luna/Buffer.h>
|
|
|
|
#include <luna/Result.h>
|
|
|
|
#include <luna/SharedPtr.h>
|
|
|
|
#include <luna/StringView.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
namespace os
|
|
|
|
{
|
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum OpenMode
|
|
|
|
{
|
|
|
|
ReadOnly = O_RDONLY,
|
|
|
|
WriteOnly = O_WRONLY | O_TRUNC,
|
|
|
|
ReadWrite = O_RDWR | O_TRUNC,
|
|
|
|
Append = O_WRONLY | O_APPEND,
|
|
|
|
ReadAppend = O_RDWR | O_APPEND,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Result<SharedPtr<File>> open(StringView path, OpenMode flags);
|
|
|
|
static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
|
|
|
|
static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
|
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
static SharedPtr<File> standard_input();
|
|
|
|
static SharedPtr<File> standard_output();
|
|
|
|
static SharedPtr<File> standard_error();
|
|
|
|
|
2023-04-12 19:46:10 +00:00
|
|
|
void set_close_on_exec();
|
|
|
|
|
|
|
|
Result<void> write(StringView str);
|
2023-04-18 14:41:17 +00:00
|
|
|
Result<void> write(const Buffer& buf);
|
2023-04-12 19:46:10 +00:00
|
|
|
|
2023-04-22 11:55:07 +00:00
|
|
|
Result<String> read_line(bool keep_newline = true);
|
2023-04-13 15:31:21 +00:00
|
|
|
|
2023-04-18 14:41:17 +00:00
|
|
|
Result<void> read(Buffer& buf, usize size);
|
|
|
|
|
2023-04-13 15:31:21 +00:00
|
|
|
Result<int> getchar();
|
|
|
|
|
2023-04-12 19:46:10 +00:00
|
|
|
File(Badge<File>);
|
|
|
|
~File();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
|
2023-04-13 15:31:21 +00:00
|
|
|
static void initialize_standard_streams();
|
2023-04-12 19:46:10 +00:00
|
|
|
|
|
|
|
Result<usize> raw_read(u8* buf, usize length);
|
|
|
|
Result<usize> raw_write(const u8* buf, usize length);
|
|
|
|
|
|
|
|
int m_fd { -1 };
|
|
|
|
};
|
|
|
|
}
|