From ab3b86ecb510ffb66d7cbb6fd29b95577ee2e8ac Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 3 Dec 2021 11:42:34 -0600 Subject: [PATCH] moved file library to os package --- core/{io => os}/file.onyx | 35 +++++++++++++++++------------------ core/std.onyx | 2 +- scripts/run_tests.onyx | 4 ++-- tests/aoc-2021/day01.onyx | 10 ++-------- tests/aoc-2021/day02.onyx | 2 +- tests/aoc-2021/day03.onyx | 2 +- 6 files changed, 24 insertions(+), 31 deletions(-) rename core/{io => os}/file.onyx (90%) diff --git a/core/io/file.onyx b/core/os/file.onyx similarity index 90% rename from core/io/file.onyx rename to core/os/file.onyx index 81eaad09..5518e2a5 100644 --- a/core/io/file.onyx +++ b/core/os/file.onyx @@ -1,5 +1,4 @@ -@TODO // Move this file to the core.os package -package core.io +package core.os #local runtime :: package runtime #if runtime.Runtime != runtime.Runtime_Wasi @@ -177,11 +176,11 @@ file_exists :: (path: str) -> bool { } FileStream :: struct { - use stream : Stream; + use stream : io.Stream; use file : File; } -open :: (path: str, mode := OpenMode.Read) -> (Error, FileStream) { +open :: (path: str, mode := OpenMode.Read) -> (io.Error, FileStream) { fs := FileStream.{ stream = .{ vtable = null }, file = .{ fd = -1 }, @@ -229,17 +228,17 @@ with_file :: (path: str, mode := OpenMode.Read) -> Iterator(^FileStream) { } #package -wasi_file_stream_vtable := Stream_Vtable.{ - seek = (use fs: ^FileStream, to: i32, whence: SeekFrom) -> Error { +wasi_file_stream_vtable := io.Stream_Vtable.{ + seek = (use fs: ^FileStream, to: i32, whence: io.SeekFrom) -> io.Error { // Currently, the new offset is just ignored. newoffset : wasi.Filesize; error := wasi.fd_seek(file.fd, ~~ to, ~~ whence, ^newoffset); if error != .Success do return .BadFile; - return Error.None; + return .None; }, - tell = (use fs: ^FileStream) -> (Error, u32) { + tell = (use fs: ^FileStream) -> (io.Error, u32) { location : wasi.Filesize; error := wasi.fd_tell(file.fd, ^location); if error != .Success do return .BadFile, 0; @@ -247,7 +246,7 @@ wasi_file_stream_vtable := Stream_Vtable.{ return .None, ~~location; }, - read = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { + read = (use fs: ^FileStream, buffer: [] u8) -> (io.Error, u32) { bytes_read : wasi.Size; vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; error := wasi.fd_read(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_read); @@ -256,18 +255,18 @@ wasi_file_stream_vtable := Stream_Vtable.{ return .None, bytes_read; }, - read_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { + read_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (io.Error, u32) { bytes_read : wasi.Size; vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; error := wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_read); - // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? + // FIX: Maybe report io.Error.OutOfBounds if the 'at' was out of bounds? if error != .Success do return .BadFile, 0; return .None, bytes_read; }, - read_byte = (use fs: ^FileStream) -> (Error, u8) { + read_byte = (use fs: ^FileStream) -> (io.Error, u8) { bytes_read : wasi.Size; byte : u8; vec := IOVec.{ buf = cast(u32) ^byte, len = 1}; @@ -277,7 +276,7 @@ wasi_file_stream_vtable := Stream_Vtable.{ return .None, byte; }, - write = (use fs: ^FileStream, buffer: [] u8) -> (Error, u32) { + write = (use fs: ^FileStream, buffer: [] u8) -> (io.Error, u32) { bytes_written : wasi.Size; vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; error := wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^bytes_written); @@ -286,18 +285,18 @@ wasi_file_stream_vtable := Stream_Vtable.{ return .None, bytes_written; }, - write_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (Error, u32) { + write_at = (use fs: ^FileStream, at: u32, buffer: [] u8) -> (io.Error, u32) { bytes_written : wasi.Size; vec := IOVec.{ buf = cast(u32) buffer.data, len = buffer.count }; error := wasi.fd_pwrite(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ~~at, ^bytes_written); - // FIX: Maybe report Error.OutOfBounds if the 'at' was out of bounds? + // FIX: Maybe report io.Error.OutOfBounds if the 'at' was out of bounds? if error != .Success do return .BadFile, 0; return .None, bytes_written; }, - write_byte = (use fs: ^FileStream, byte: u8) -> Error { + write_byte = (use fs: ^FileStream, byte: u8) -> io.Error { bytes_written : wasi.Size; byte_to_write := byte; vec := IOVec.{ buf = cast(u32) ^byte_to_write, len = 1 }; @@ -307,12 +306,12 @@ wasi_file_stream_vtable := Stream_Vtable.{ return .None; }, - close = (use fs: ^FileStream) -> Error { + close = (use fs: ^FileStream) -> io.Error { file_close(file); return .None; }, - flush = (use fs: ^FileStream) -> Error { + flush = (use fs: ^FileStream) -> io.Error { wasi.fd_datasync(file.fd); return .None; }, diff --git a/core/std.onyx b/core/std.onyx index ea66bc54..c076b04f 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -41,7 +41,7 @@ package core #load "./wasi/wasi" #load "./wasi/env" #load "./wasi/clock" - #load "./io/file" + #load "./os/file" #load "./os/os" } diff --git a/scripts/run_tests.onyx b/scripts/run_tests.onyx index cb9affab..40bdffbd 100644 --- a/scripts/run_tests.onyx +++ b/scripts/run_tests.onyx @@ -101,7 +101,7 @@ find_onyx_files :: (root: str, cases: ^[..] Test_Case) { test_case := string.concat(path_buffer, root, "/", it.name) |> string.alloc_copy(); expected_file := test_case[0 .. (test_case.count - 5)]; - if !io.file_exists(expected_file) { + if !os.file_exists(expected_file) { print_color(.Yellow, "Skipping test case {} because an expected output file was not found.\n", test_case); continue; } @@ -137,7 +137,7 @@ test_cases :: (cases) => { continue; } - for expected_file: io.with_file(it.expected_file) { + for expected_file: os.with_file(it.expected_file) { expected_reader := io.reader_make(expected_file); expected_output := io.read_all(^expected_reader); diff --git a/tests/aoc-2021/day01.onyx b/tests/aoc-2021/day01.onyx index d1de70f9..8664c136 100644 --- a/tests/aoc-2021/day01.onyx +++ b/tests/aoc-2021/day01.onyx @@ -14,16 +14,10 @@ count_increasing :: (arr: [] $T) -> u32 { } main :: (args) => { - #if false { - contents := #file_contents "./input/day01.txt"; - reader, stream := io.reader_from_string(contents); - defer cfree(stream); - } - - err, file := io.open("./tests/aoc-2021/input/day01.txt"); + err, file := os.open("./tests/aoc-2021/input/day01.txt"); assert(err == .None, "Error opening file"); reader := io.reader_make(^file); - defer io.close(^file); + defer os.close(^file); nums := array.make(i32); while !io.reader_empty(^reader) { diff --git a/tests/aoc-2021/day02.onyx b/tests/aoc-2021/day02.onyx index 1f19319b..ca1704c1 100644 --- a/tests/aoc-2021/day02.onyx +++ b/tests/aoc-2021/day02.onyx @@ -5,7 +5,7 @@ PART :: 2 use package core main :: (args) => { - for file: io.with_file("tests/aoc-2021/input/day02.txt") { + for file: os.with_file("tests/aoc-2021/input/day02.txt") { reader := io.reader_make(file); #if PART == 1 { diff --git a/tests/aoc-2021/day03.onyx b/tests/aoc-2021/day03.onyx index ad8d4689..fe2b70ba 100644 --- a/tests/aoc-2021/day03.onyx +++ b/tests/aoc-2021/day03.onyx @@ -26,7 +26,7 @@ read_binary :: (r: ^io.Reader) -> i32 { main :: (args) => { BITS :: 12 - for io.with_file("./tests/aoc-2021/input/day03.txt") { + for os.with_file("./tests/aoc-2021/input/day03.txt") { reader := io.reader_make(it); nums := array.make(i32); -- 2.25.1