diff options
author | Christian Cleberg <hello@cleberg.net> | 2025-05-05 18:09:27 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2025-05-05 18:09:27 -0500 |
commit | 3ec668ac415d2d92b28e658b17d8932d0cbe2b3b (patch) | |
tree | 7bfb32b5b0d5731e9d9468809b3b0cdd1e744201 /hare/files | |
download | learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.tar.gz learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.tar.bz2 learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.zip |
initial commit
Diffstat (limited to 'hare/files')
-rw-r--r-- | hare/files/files.ha | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/hare/files/files.ha b/hare/files/files.ha new file mode 100644 index 0000000..121af45 --- /dev/null +++ b/hare/files/files.ha @@ -0,0 +1,60 @@ +use bufio; +use fmt; +use fs; +use io; +use os; +use strings; + +export fn main() void = { + // Get user input + const filename = get_filename(); + const filedata = get_input(); + + // Save to file and return the message + let message = save_input(filename, filedata); + fmt::printfln("{}", message)!; +}; + +fn get_filename() str = { + // Ask user for filename + fmt::printfln("Enter a file name to create:")!; + + // Read the buffer + const input = bufio::scanline(os::stdin)! as []u8; + + // Convert to string and return + return strings::fromutf8(input)!; +}; + +fn get_input() []u8 = { + // Ask user for input + fmt::printfln("Enter some words or data to write to a file:")!; + + // Read the buffer + const input = bufio::scanline(os::stdin)! as []u8; + + // Return the data + return input; +}; + +fn save_input(file_name: str, file_data: []u8) str = { + // USER_RW = 384, Read and write permissions for the file owner + let file_mode: fs::mode = 384; + + // Create a file buffer as USER_RW using the provided file name + let file_buffer = os::create(file_name, file_mode); + + // Handle the tagged union for (1) a successful file buffer, or (2) an + // error + match (file_buffer) { + // (1) Write the data, close the file, and return a success message + case let file: io::file => + io::write(file, file_data)!; + io::close(file)!; + return "Data saved successfully. Check the file you created!"; + // (2) Return the error message as a string + case let e: fs::error => + return fs::strerror(e); + }; +}; + |