diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-08-28 18:25:07 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-08-28 18:25:07 +0200 |
| commit | f74e281efadbbd3c042f2f3aed13f0552cd881dd (patch) | |
| tree | 145cd457c3660ca26598da971b4f923dc4ea1732 /core/compress | |
| parent | 6363013dd8537a6d8321b4ab9dd4b9f1a3c922b8 (diff) | |
Various changes to TGA reader
- Style changes
- Change ptr usage to slice indexing
- Add TGA Footer
Also, add `peek_data` with offset to `compress`.
Diffstat (limited to 'core/compress')
| -rw-r--r-- | core/compress/common.odin | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/core/compress/common.odin b/core/compress/common.odin index f4e378269..b42cbefff 100644 --- a/core/compress/common.odin +++ b/core/compress/common.odin @@ -295,6 +295,24 @@ peek_data_from_memory :: #force_inline proc(z: ^Context_Memory_Input, $T: typeid } @(optimization_mode="speed") +peek_data_at_offset_from_memory :: #force_inline proc(z: ^Context_Memory_Input, $T: typeid, #any_int offset: int) -> (res: T, err: io.Error) { + size :: size_of(T) + + #no_bounds_check { + if len(z.input_data) >= size + offset { + buf := z.input_data[offset:][:size] + return (^T)(&buf[0])^, .None + } + } + + if len(z.input_data) == 0 { + return T{}, .EOF + } else { + return T{}, .Short_Buffer + } +} + +@(optimization_mode="speed") peek_data_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid) -> (res: T, err: io.Error) { size :: size_of(T) @@ -321,7 +339,44 @@ peek_data_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid return res, .None } -peek_data :: proc{peek_data_from_memory, peek_data_from_stream} +@(optimization_mode="speed") +peek_data_at_offset_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid, #any_int offset: int) -> (res: T, err: io.Error) { + size :: size_of(T) + + // Get current position to return to. + cur_pos, e1 := z.input->impl_seek(0, .Current) + if e1 != .None { + return T{}, e1 + } + + // Seek to offset. + pos, e2 := z.input->impl_seek(offset, .Start) + if e2 != .None { + return T{}, e2 + } + + r, e3 := io.to_reader_at(z.input) + if !e3 { + return T{}, .Empty + } + when size <= 128 { + b: [size]u8 + } else { + b := make([]u8, size, context.temp_allocator) + } + _, e4 := io.read_at(r, b[:], pos) + if e4 != .None { + return T{}, .Empty + } + + // Return read head to original position. + z.input->impl_seek(cur_pos, .Start) + + res = (^T)(&b[0])^ + return res, .None +} + +peek_data :: proc{peek_data_from_memory, peek_data_from_stream, peek_data_at_offset_from_memory, peek_data_at_offset_from_stream} |