aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2025-10-29 14:40:15 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2026-02-08 12:44:14 +0100
commit5d8de5860ba6b1234facfcd5db2d83db9a551f07 (patch)
tree3c721389336a07af605af795ae2b475fd76b7336 /core
parentc0da9be4a9e216d1c050c8b5c000a34f87c5a004 (diff)
gzip -> os2
Diffstat (limited to 'core')
-rw-r--r--core/compress/gzip/doc.odin54
-rw-r--r--core/compress/gzip/gzip.odin22
2 files changed, 34 insertions, 42 deletions
diff --git a/core/compress/gzip/doc.odin b/core/compress/gzip/doc.odin
index c20ebc33a..82eaa6f35 100644
--- a/core/compress/gzip/doc.odin
+++ b/core/compress/gzip/doc.odin
@@ -2,10 +2,11 @@
A small `GZIP` unpacker.
Example:
- import "core:bytes"
- import "core:os"
- import "core:compress"
- import "core:fmt"
+ import "core:bytes"
+ import os "core:os/os2"
+ import "core:compress"
+ import "core:compress/gzip"
+ import "core:fmt"
// Small GZIP file with fextra, fname and fcomment present.
@private
@@ -22,7 +23,8 @@ Example:
main :: proc() {
// Set up output buffer.
- buf := bytes.Buffer{}
+ buf: bytes.Buffer
+ defer bytes.buffer_destroy(&buf)
stdout :: proc(s: string) {
os.write_string(os.stdout, s)
@@ -31,15 +33,13 @@ Example:
os.write_string(os.stderr, s)
}
- args := os.args
-
- if len(args) < 2 {
+ if len(os.args) < 2 {
stderr("No input file specified.\n")
- err := load(data=TEST, buf=&buf, known_gzip_size=len(TEST))
+ err := gzip.load(data=TEST, buf=&buf, known_gzip_size=len(TEST))
if err == nil {
- stdout("Displaying test vector: ")
+ stdout("Displaying test vector: \"")
stdout(bytes.buffer_to_string(&buf))
- stdout("\n")
+ stdout("\"\n")
} else {
fmt.printf("gzip.load returned %v\n", err)
}
@@ -47,35 +47,31 @@ Example:
os.exit(0)
}
- // The rest are all files.
- args = args[1:]
- err: Error
+ for file in os.args[1:] {
+ err: gzip.Error
- for file in args {
if file == "-" {
// Read from stdin
- s := os.stream_from_handle(os.stdin)
ctx := &compress.Context_Stream_Input{
- input = s,
+ input = os.stdin.stream,
}
- err = load(ctx, &buf)
+ err = gzip.load(ctx, &buf)
} else {
- err = load(file, &buf)
+ err = gzip.load(file, &buf)
}
- if err != nil {
- if err != E_General.File_Not_Found {
- stderr("File not found: ")
- stderr(file)
- stderr("\n")
- os.exit(1)
- }
+ switch err {
+ case nil:
+ stdout(bytes.buffer_to_string(&buf))
+ case gzip.E_General.File_Not_Found:
+ stderr("File not found: ")
+ stderr(file)
+ stderr("\n")
+ os.exit(1)
+ case:
stderr("GZIP returned an error.\n")
- bytes.buffer_destroy(&buf)
os.exit(2)
}
- stdout(bytes.buffer_to_string(&buf))
}
- bytes.buffer_destroy(&buf)
}
*/
package compress_gzip
diff --git a/core/compress/gzip/gzip.odin b/core/compress/gzip/gzip.odin
index 7dc8120e4..644a625e7 100644
--- a/core/compress/gzip/gzip.odin
+++ b/core/compress/gzip/gzip.odin
@@ -14,12 +14,12 @@ package compress_gzip
to be the input to a complementary TAR implementation.
*/
-import "core:compress/zlib"
-import "core:compress"
-import "core:os"
-import "core:io"
-import "core:bytes"
-import "core:hash"
+import "core:compress/zlib"
+import "core:compress"
+import os "core:os/os2"
+import "core:io"
+import "core:bytes"
+import "core:hash"
Magic :: enum u16le {
GZIP = 0x8b << 8 | 0x1f,
@@ -107,14 +107,10 @@ load :: proc{load_from_bytes, load_from_file, load_from_context}
load_from_file :: proc(filename: string, buf: ^bytes.Buffer, expected_output_size := -1, allocator := context.allocator) -> (err: Error) {
context.allocator = allocator
- data, ok := os.read_entire_file(filename)
- defer delete(data)
+ file_data, file_err := os.read_entire_file(filename, allocator)
+ defer delete(file_data)
- err = E_General.File_Not_Found
- if ok {
- err = load_from_bytes(data, buf, len(data), expected_output_size)
- }
- return
+ return load_from_bytes(file_data, buf, len(file_data), expected_output_size) if file_err == nil else E_General.File_Not_Found
}
load_from_bytes :: proc(data: []byte, buf: ^bytes.Buffer, known_gzip_size := -1, expected_output_size := -1, allocator := context.allocator) -> (err: Error) {