#! @file file.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `file`, a file the program has open, which reads, writes, and seeks, #! and closes where its scope ends. import system; module std::io; #! A file the program has open, which it reads and writes through, and which is #! closed where its scope ends. #! #! A file is opened by `file::open` or `file::create`, which give why where they #! cannot. It cannot be copied, since only one value may close it, but it can be #! moved. Its zero value is not open, and using one that is not open fails with #! `io_error::invalid`. pub struct file : owning, moveable, readable, writable, seekable { var _handle: system::fd; var _open: bool; } #! The system's handle of the file, for what `system` does that a file does #! not. #! @param this The file to read the handle of. #! @returns The handle, which is only good while the file is open. pub val file::handle: system::fd { get => this._handle; } #! Whether the file is open. #! @param this The file to check. #! @returns Whether it was opened, and has not been closed or moved from. pub val file::is_open: bool { get => this._open; } #! Opens the file at a path. #! @param at The path of the file to open. #! @param flags How to open it, which is for reading unless they say #! otherwise. #! @param mode The permissions a file made by `open_flags::create` is given, #! less what the system's mask takes away. #! @returns The open file, or why it could not be opened. pub def file::open( at: &path, flags: open_flags = open_flags::read, mode: permissions = permissions::default_file ): result { val handle := system::open(at.span, flags, mode); if handle < 0 { return system::error_of(handle); } return file { _handle = handle, _open = true }; } #! Opens the file at a path for writing, emptied, making it where there is none. #! @param at The path of the file to write. #! @param mode The permissions a file this makes is given, less the mask. #! @returns The open file, or why it could not be opened. pub def file::create(at: &path, mode: permissions = permissions::default_file): result { return file::open(at, open_flags::write | open_flags::create | open_flags::truncate, mode); } #! Creates a file by taking another's handle, which leaves the other not open. #! @param other The file to move from. #! @returns The file, which closes the handle where its scope ends. pub def file::move_of(other: &mut file): file { val made: file = file { _handle = other._handle, _open = other._open }; other._open = false; return made; } #! Closes the file. Closing a file that is not open does nothing. #! @param this The file to close. pub def file::close(&mut this): void { if !this._open { return; } this._handle.close(); this._open = false; } #! Reads from the file, where its position is, and moves the position past what #! was read. #! @param this The file to read. #! @param into Where the bytes read are written. #! @returns How many bytes were read, which may be fewer than there is room for, #! and is zero only at the end of the file. pub def file::read(&mut this, into: slice): result { if !this._open { return io_error::invalid; } val count := this._handle.read(into.data, into.length); if count < 0 { return system::error_of(count); } return count as u64; } #! Reads the rest of the file, from its position to its end. #! @param this The file to read. #! @param alloc The optional allocator for the text to be held in. #! @returns The bytes read, or why they could not be. pub def file::read_all(&mut this, alloc: maybe<&mut allocator> = nothing{}): result { if !this._open { return io_error::invalid; } # What the system says the file holds is only a hint, so the text grows to hold # whatever is actually read. var made := string::new(alloc); var info: file_info; if this._handle.stat(&mut info) == 0 { made.reserve(info.size); } var chunk: u8[4096]; while true { val count := this._handle.read((&mut chunk) as *mut u8, 4096); if count < 0 { return system::error_of(count); } if count == 0 { break; } made.append(span::::over((&chunk) as *u8, count as u64)); } return move(made); } #! Writes all of some bytes to the file, where its position is, and moves the #! position past them. #! @param this The file to write. #! @param data The bytes to write. #! @returns Why they could not all be written, where they could not. pub def file::write(&mut this, data: span): result { if !this._open { return io_error::invalid; } return write_all(this._handle, data); } #! Moves the position in the file that reads and writes happen at. #! @param this The file to seek in. #! @param offset The offset to move to. #! @param from Where the offset is measured from. #! @returns The new position, from the start of the file. pub def file::seek(&mut this, offset: i64, from: whence = whence::start): result { if !this._open { return io_error::invalid; } val position := this._handle.seek(offset, from); if position < 0 { return system::error_of(position); } return position as u64; } #! Describes the file. #! @param this The file to describe. #! @returns What is known of it, or why it could not be described. pub def file::info(&this): result { if !this._open { return io_error::invalid; } var out: file_info; val status := this._handle.stat(&mut out); if status < 0 { return system::error_of(status); } return out; } #! Sets who may do what with the file. #! @param this The file whose permissions to set. #! @param mode The permissions to give it. #! @returns Why they could not be set, where they could not. pub def file::set_permissions(&mut this, mode: permissions): result { if !this._open { return io_error::invalid; } return outcome(this._handle.chmod(mode)); }