Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ detect_compressed(io, len=getlength(io); kwargs...) = detect_compressor(io, len;
const compressed_fits_exten = r"\.(fit|fits|fts|FIT|FITS|FTS)\.(gz|GZ)\>"
name_matches_compressed_fits(io) = (:name propertynames(io)) && endswith(io.name, compressed_fits_exten)

# We only fall back to treating a bare
# compressed stream as RData when the filename actually carries an RData
# extension; otherwise `detect_rdata` would claim any compressed file.
const rdata_exten = r"\.(rda|RData|rdata)\>"
name_matches_rdata(io) = (:name propertynames(io)) && endswith(io.name, rdata_exten)

# test for RD?n magic sequence at the beginning of R data input stream
function detect_rdata(io)
seekstart(io)
Expand All @@ -98,11 +104,14 @@ function detect_rdata(io)
return true
end
checked_match(io) && return true
return detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"]) && !name_matches_compressed_fits(io)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't make any sense, all compressed files should not be read by a rdata loader, just because it isn't a compressed fits file.

return name_matches_rdata(io) && detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"])
end

add_format(format"RData", detect_rdata, [".rda", ".RData", ".rdata"], [idRData, LOAD])

const rdata_single_exten = r"\.rds\>"
name_matches_rdata_single(io) = (:name propertynames(io)) && endswith(io.name, rdata_single_exten)

function detect_rdata_single(io)
seekstart(io)
function checked_match(io)
Expand All @@ -119,12 +128,14 @@ function detect_rdata_single(io)

res = checked_match(io)
if !res
res = detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"]) && !name_matches_compressed_fits(io)
res = name_matches_rdata_single(io) && detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"])
end
seekstart(io)
return res
end

add_format(format"RDataSingle", detect_rdata_single, [".rds"], [idRData, LOAD])

function detect_excel(io)
# All OOXML Excel files are ZIPs starting with PK\x03\x04
magic = try
Expand All @@ -138,8 +149,6 @@ function detect_excel(io)
# so just claiming the ZIP magic is sufficient to beat NPZ.
end

add_format(format"RDataSingle", detect_rdata_single, [".rds"], [idRData, LOAD])

add_format(format"AVSfld", "# AVS", [".fld"], [idAVSfldIO])
add_format(format"CSV", (), [".csv"], [idCSVFiles])
add_format(format"TSV", (), [".tsv"], [idCSVFiles])
Expand Down Expand Up @@ -507,18 +516,6 @@ end
add_format(format"STL_ASCII", detect_stlascii, [".stl", ".STL"], [idMeshIO])
add_format(format"STL_BINARY", detect_stlbinary, [".stl", ".STL"], [idMeshIO])

# GZip has two simple magic bytes [0x1f, 0x8b] but we don't want to dispatch to Libz
# for file extensions like .fits.gz
function detect_gzip(io)
if name_matches_compressed_fits(io)
return false
end
getlength(io) >= 2 || return false
magic = read!(io, Vector{UInt8}(undef, 2))
return magic == [0x1f, 0x8b]
end
add_format(format"GZIP", detect_gzip, ".gz", [:Libz => UUID("2ec943e9-cfe8-584d-b93d-64dcb6d567b7")])


# Astro Data
# FITS files are often gziped and given the extension ".fits.gz". We want to load those directly and not dispatch to Libz
Expand Down
4 changes: 0 additions & 4 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module TestLoadSave
load(file::File{format"PBMText"}) = "PBMText"
load(file::File{format"PBMBinary"}) = "PBMBinary"
load(file::File{format"JLD"}) = "JLD"
load(file::File{format"GZIP"}) = "GZIP"
end
module TestLoadSave2
import FileIO: File, @format_str
Expand All @@ -33,7 +32,6 @@ end
add_loader(format"PBMBinary", TestLoadSave)
add_loader(format"HDF5", TestLoadSave2)
add_loader(format"JLD", TestLoadSave)
add_loader(format"GZIP", TestLoadSave)
add_loader(format"BIB", TestLoadSave2)
add_loader(format"DCM", TestLoadSave2)

Expand All @@ -51,8 +49,6 @@ end
@test load(joinpath(fp,"file2.h5")) == "HDF5"
# JLD file saved with .jld extension
@test load(joinpath(fp,"file.jld")) == "JLD"
# GZIP file saved with .gz extension
@test load(joinpath(fp,"file.csv.gz")) == "GZIP"
# Bibliography file saved with .bib extension
@test load(joinpath(fp,"file.bib")) == "BIB"
# DICOM file saved with .dcm extension
Expand Down
Loading