#pragma once #include #include #include #include #include #include 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> open(StringView path, OpenMode flags); static Result> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644); static Result> create(StringView path, OpenMode flags, mode_t mode = 0644); /* If path is "-", return standard input (as is common for many CLI apps). Otherwise, open path for reading. This function is a convenience function for CLI apps, so that they don't need to check path and open standard input if necessary. */ static Result> open_input_file(StringView path); static SharedPtr standard_input(); static SharedPtr standard_output(); static SharedPtr standard_error(); void set_close_on_exec(); Result write(StringView str); Result write(const Buffer& buf); Result read_line(); Result read_all_as_string(); Result read(Buffer& buf, usize size); Result read_all(); Result getchar(); int fd() const { return m_fd; } File(Badge); ~File(); private: static Result> construct(StringView path, int flags, mode_t mode); static void initialize_standard_streams(); Result raw_read(u8* buf, usize length); Result raw_write(const u8* buf, usize length); int m_fd { -1 }; }; Result print(StringView fmt, ...); Result println(StringView fmt, ...); Result eprint(StringView fmt, ...); Result eprintln(StringView fmt, ...); }