Luna/libos/include/os/File.h

62 lines
1.6 KiB
C++

#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);
static SharedPtr<File> standard_input();
static SharedPtr<File> standard_output();
static SharedPtr<File> standard_error();
void set_close_on_exec();
Result<void> write(StringView str);
Result<void> write(const Buffer& buf);
Result<String> read_line();
Result<String> read_all_as_string();
Result<void> read(Buffer& buf, usize size);
Result<Buffer> read_all();
Result<int> getchar();
int fd() const
{
return m_fd;
}
File(Badge<File>);
~File();
private:
static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
static void initialize_standard_streams();
Result<usize> raw_read(u8* buf, usize length);
Result<usize> raw_write(const u8* buf, usize length);
int m_fd { -1 };
};
}