Refactoring w/reader struct
parent
348f9633c3
commit
08964fdb53
83
src/file.jl
83
src/file.jl
|
@ -2,23 +2,82 @@ module file
|
|||
|
||||
export FITFile, read_file
|
||||
|
||||
include("header.jl")
|
||||
include("body.jl")
|
||||
include("body.jl"); using .body
|
||||
include("bytes.jl"); using .bytes
|
||||
|
||||
using .header
|
||||
using .body
|
||||
|
||||
struct FITFile
|
||||
header::FITHeader
|
||||
body::DefinitionHeader
|
||||
HEADER_SZ = 14
|
||||
|
||||
struct FITHeader
|
||||
sz::UInt8
|
||||
protocol::UInt8
|
||||
version::Integer
|
||||
size::Integer
|
||||
type::String
|
||||
crc::AbstractVector{<:Real}
|
||||
end
|
||||
|
||||
function read_file(filepath::AbstractString)::FITFile
|
||||
open(filepath, "r") do f
|
||||
header = read_header(f)
|
||||
body = read_row(f)
|
||||
FITFile(header, body)
|
||||
mutable struct FITFileReader
|
||||
filepath::AbstractString
|
||||
fp::Union{IO, Nothing}
|
||||
bytes_read::Int64
|
||||
header::Union{FITHeader, Nothing}
|
||||
body::Union{DefinitionHeader, Nothing}
|
||||
end
|
||||
|
||||
|
||||
blank_file(fp::AbstractString) = FITFileReader(fp, nothing, 0, nothing, nothing)
|
||||
|
||||
"""
|
||||
open_fit_file!(f::FITFileReader)
|
||||
|
||||
Open the fit file, and set f.fp
|
||||
"""
|
||||
function open_fit_file!(f::FITFileReader)
|
||||
f.fp = open(f.filepath, "r")
|
||||
end
|
||||
|
||||
"""
|
||||
close_fit_file!(f::FITFileReader)
|
||||
|
||||
Close the fit file, and set our fp to nothing.
|
||||
"""
|
||||
function close_fit_file!(f::FITFileReader)
|
||||
close(f.fp)
|
||||
f.fp = nothing
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
read_header(fit_file::IOStream)
|
||||
|
||||
Read the header of the given fit file, and return a FITHeader struct
|
||||
"""
|
||||
function read_header!(f::FITFileReader)
|
||||
header = read(f.fp, HEADER_SZ; all=false)
|
||||
f.header = FITHeader(
|
||||
header[1],
|
||||
header[2],
|
||||
byte_vec_to_int(header[3:4]),
|
||||
byte_vec_to_int(header[5:8]),
|
||||
byte_vec_to_string(header[9:12]),
|
||||
header[13:14]
|
||||
)
|
||||
f.bytes_read += HEADER_SZ
|
||||
end
|
||||
|
||||
function read_file(filepath::AbstractString)::FITFileReader
|
||||
f = blank_file(filepath)
|
||||
|
||||
open_fit_file!(f)
|
||||
try
|
||||
read_header!(f)
|
||||
catch e
|
||||
rethrow(e)
|
||||
finally
|
||||
close_fit_file!(f)
|
||||
end
|
||||
f
|
||||
end
|
||||
|
||||
end
|
|
@ -1,40 +0,0 @@
|
|||
|
||||
|
||||
"""
|
||||
length in bytes
|
||||
"""
|
||||
|
||||
module header
|
||||
|
||||
export FITHeader, read_header
|
||||
|
||||
include("bytes.jl"); using .bytes
|
||||
|
||||
const DATA_SIZE_LENGTH = 4
|
||||
|
||||
struct FITHeader
|
||||
sz::UInt8
|
||||
protocol::UInt8
|
||||
version::Integer
|
||||
size::Integer
|
||||
type::String
|
||||
crc::AbstractVector{<:Real}
|
||||
end
|
||||
|
||||
"""
|
||||
read_header(fit_file::IOStream)
|
||||
|
||||
Read the header of the given fit file, and return a FITHeader struct
|
||||
"""
|
||||
function read_header(fit_file::IOStream)::FITHeader
|
||||
sz = read(fit_file, 1; all=false)[1]
|
||||
protocol = read(fit_file, 1; all=false)[1]
|
||||
version = byte_vec_to_int(read(fit_file, 2; all=false))
|
||||
size = byte_vec_to_int(read(fit_file, 4; all=false))
|
||||
type = byte_vec_to_string(read(fit_file, 4; all=false))
|
||||
crc = read(fit_file, 2; all=false)
|
||||
|
||||
FITHeader(sz, protocol, version, size, type, crc)
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue