Refactoring w/reader struct
This commit is contained in:
+71
-12
@@ -2,23 +2,82 @@ module file
|
|||||||
|
|
||||||
export FITFile, read_file
|
export FITFile, read_file
|
||||||
|
|
||||||
include("header.jl")
|
include("body.jl"); using .body
|
||||||
include("body.jl")
|
include("bytes.jl"); using .bytes
|
||||||
|
|
||||||
using .header
|
|
||||||
using .body
|
|
||||||
|
|
||||||
struct FITFile
|
HEADER_SZ = 14
|
||||||
header::FITHeader
|
|
||||||
body::DefinitionHeader
|
struct FITHeader
|
||||||
|
sz::UInt8
|
||||||
|
protocol::UInt8
|
||||||
|
version::Integer
|
||||||
|
size::Integer
|
||||||
|
type::String
|
||||||
|
crc::AbstractVector{<:Real}
|
||||||
end
|
end
|
||||||
|
|
||||||
function read_file(filepath::AbstractString)::FITFile
|
mutable struct FITFileReader
|
||||||
open(filepath, "r") do f
|
filepath::AbstractString
|
||||||
header = read_header(f)
|
fp::Union{IO, Nothing}
|
||||||
body = read_row(f)
|
bytes_read::Int64
|
||||||
FITFile(header, body)
|
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
|
end
|
||||||
|
f
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
|
||||||
Reference in New Issue
Block a user