Streaming files

I’ve been looking for a way to process files for the past two days.

The core:os library has os.open (not to mention that one of the errors involves an enum with nearly 3,000 values idk why), which returns ^os.File.

To avoid overloading my computer with a large file, I wanted to use streaming. Since odin is syntactically similar to go, I checked out os.bufio.

The documentation didn’t include information on how to use this package, and there are only brief descriptions, so I started reviewing the package’s code (core:bufio and core:io).

To create a bufio.Reader (bufio.reader_init), I need an io.Reader; in the io package, io.Reader is an alias for Stream:

Stream_Proc :: #type proc(stream_data: rawptr, mode: Stream_Mode, p: []byte, offset: i64, whence: Seek_From) -> (n: i64, err: Error)

Stream :: struct {
	procedure: Stream_Proc,
	data:      rawptr,
}

I’ve gone through the entire core.io package, but I can’t find a function anywhere to create a stream from os.File. There are conversion functions, but they only cover types within the package.

Am I looking in the wrong place, or should I just implement this myself, or should I do it the C way instead (using getchar with a pointer to a file + a buffer)?

Hi Michal!
There is an article from GB on the Odin homepage with examples:

2 Likes

thx for help

I try to copy it but it’s not working xD

Error: 'stream_from_handle' is not declared by 'os'
	... .reader_init_with_buf(&r, {os.stream_from_handle(f)}, buffer[:])

But i found (probably new implementation) os.to_strem and bufio.reader_init which works

r: bufio.Reader
bufio.reader_init(&r, os.to_stream(f))
// NOTE: bufio.reader_init can be used if you want to use a dynamic backing buffer
defer bufio.reader_destroy(&r)

That article is probably outdated (or I’m doing somthing wrong (more probably)) but it allows me to find the solution so thx

1 Like

Awesome that you got it working on your own.
Yea, the article is probably outdated.

Maybe @gingerBill can publish an update?