FileIO - Minimal example

I am a newbie with julia and I am struggling a bit to understand FileIO.

I am trying to parse a file. I’d like to see a minimal working example. For example a module where I read text file (e.g. a .unv file and parse it line by line).

For instance, if we had a module call Uff.jl:

module Uff

using FileIO

# Should the format be registered here or when using the module
add_format(format"UFF", (), [".uff",".unv"])

# Streaming based reader
function load(s::Stream{format"UFF"}; keywords...)
    # s is already positioned after the magic bytes
    # Do the stuff to read a PNG file
    readline(s)

end

struct UFFReader
    io::IO
    ownstream::Bool
end

function Base.read(reader::UFFReader ) #, frames::Int)
    # read and decode audio samples from reader.io
    print(reader)
end

function Base.close(reader::UFFReader)
    # do whatever cleanup the reader needs
    reader.ownstream && close(reader.io)
end

loadstreaming(f::File{format"UFF"}) = UFFReader(open(f), true)
loadstreaming(s::Stream{format"UFF"}) = UFFReader(s, false)

# Reader based on the streaming
function load(q::Formatted{format"Uff"}, args...; kwargs...)
    loadstreaming(q, args...; kwargs...) do stream
        read(stream)
    end
end
 
end

So it can be used as suggested in the documentation:

using FileIO

add_format(format"UFF", (), [".uff",".unv"], [:Uff])

test = loadstreaming("example.unv")

try
    while !eof(audio)
        line = read(test)
    end
finally
    close(test)
end

The following seems to work.

I have created a module Uff.jl:

module Uff

using FileIO

struct UFFReader
    io::IO
    ownstream::Bool
end

function Base.read( reader::UFFReader )
    # read and decode audio samples from reader.io
    readline(reader.io)
end

function Base.close(reader::UFFReader)
    # do whatever cleanup the reader needs
    reader.ownstream && close(reader.io)
end

Base.eof(reader::UFFReader) = eof( reader.io )

loadstreaming(f::File{format"UFF"}) = UFFReader(open(f.filename), true)
loadstreaming(s::Stream{format"UFF"}) = UFFReader(s, false)

end

Just for the record it seems to work when I do:

using FileIO
using Uff

add_format(format"UFF", (), [".uff",".unv"], [:Uff])

loadstreaming("example.unv") do io
   while !eof(io)
     print(read(io))
   end
end

So it is a matter of replacing readline(reader.io) for whatever parser needs to be used and replacing print(read(io)) for whatever processing we want to do in the future.

I will keep posting improvements as I learn them. (Just for the record)