Skip to content

Commit

Permalink
Fix datastart calcs for writing
Browse files Browse the repository at this point in the history
  • Loading branch information
halleysfifthinc committed Aug 10, 2024
1 parent bec84af commit 22f2a70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/C3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function Header{END}(f::C3DFile{OEND}) where {END<:AbstractEndian,OEND<:Abstract
end
ampf::UInt16 = aspf*f.groups[:ANALOG][Int, :USED]

datastart::UInt8 = 2+round(sum(writesize, Iterators.flatten((groups(f), parameters(f))))/512, RoundUp)
datastart::UInt8 = 2+cld(sum(writesize, Iterators.flatten((groups(f), parameters(f)))), 512)

return Header{END}(paramptr, datafmt, npoints, ampf, h.fframe, h.lframe, h.maxinterp,
h.scale, datastart, aspf, pointrate, h.res1, h.labeltype, h.numevents, h.res2,
Expand Down
11 changes: 6 additions & 5 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,25 @@ function writec3d(io, f::C3DFile{END}) where END
nb += write(io, 0x54)
end

# nb += sum(g -> write(io, g), sort(groups(f); by=(g->abs(g.gid))))
nb += sum(g -> write(io, g), groups(f))
params = collect(parameters(f))
nb += sum(p -> write(io, p, END), params[1:end-1])
# Properly, the `pointer` in the last parameter should be zero to signify the end of the
# parameter section
nb += write(io, last(params), END; last=true)

datastart = header.datastart*512
# pad with zeros until the beginning of the data section
# @debug "padding from $(position(io)) to $(max((header.datastart - 1)*512 - position(io), 0))"
@debug "padding from $(position(io)) to $(max(datastart - position(io), 0))"
nb += sum(x -> write(io, x),
Iterators.repeated(0x00, max((header.datastart - 1)*512 - position(io), 0)); init=0)
Iterators.repeated(0x00, max(datastart - position(io), 0)); init=0)

nb += writedata(io, f)

# @debug "padding from $(position(io)) to end of next block (512 bytes) multiple ($(min(round(Int, nb/512, RoundUp)*512 - position(io), 512)))"
fileend = cld(nb, 512)*512
@debug "padding from $(position(io)) to end of next block (512 bytes) multiple ($(min(fileend - position(io), 512)))"
nb += sum(x -> write(io, x),
Iterators.repeated(0x00, min(round(Int, nb/512, RoundUp)*512 - position(io), 512));
Iterators.repeated(0x00, min(fileend - position(io), 512));
init=0)

return nb
Expand Down

0 comments on commit 22f2a70

Please sign in to comment.