diff --git a/src/registry.jl b/src/registry.jl index 43bea24f..7dae0af7 100644 --- a/src/registry.jl +++ b/src/registry.jl @@ -18,6 +18,16 @@ end add_format(format"RData", detect_rdata, [".rda", ".RData", ".rdata"], [:RData, LOAD]) +function detect_rdata_single(io) + seekstart(io) + res = read(io, UInt8) in (UInt8('A'), UInt8('B'), UInt8('X')) && + (c = read(io, UInt8); c == UInt8('\n') || (c == UInt8('\r') && read(io, UInt8) == UInt8('\n'))) + seekstart(io) + return res +end + +add_format(format"RDataSingle", detect_rdata_single, [".rds"], [:RData, LOAD]) + add_format(format"CSV", (), [".csv"], [:CSVFiles]) add_format(format"TSV", (), [".tsv"], [:CSVFiles]) add_format(format"Feather", "FEA1", [".feather"], [:FeatherFiles]) diff --git a/test/files/minimal_ascii.rds b/test/files/minimal_ascii.rds new file mode 100644 index 00000000..a5b5591f --- /dev/null +++ b/test/files/minimal_ascii.rds @@ -0,0 +1,40 @@ +A +2 +197634 +131840 +787 +1 +14 +2 +1.1 +2.2 +1026 +1 +262153 +5 +names +16 +1 +262153 +3 +num +1026 +1 +262153 +9 +row.names +13 +2 +NA +-2 +1026 +1 +262153 +5 +class +16 +1 +262153 +10 +data.frame +254 diff --git a/test/query.jl b/test/query.jl index f09735f3..cc0b89a6 100644 --- a/test/query.jl +++ b/test/query.jl @@ -332,6 +332,16 @@ end @test (position(io) in (5, 6)) end end +@testset "RDS detection" begin + q = query(joinpath(file_dir, "minimal_ascii.rds")) + @test typeof(q) == File{format"RDataSingle"} + open(q) do io + @test position(io) == 0 + @test FileIO.detect_rdata_single(io) + # need to seek to beginning of file where data structure starts + @test position(io) == 0 + end +end @testset "Format with function for magic bytes" begin add_format(format"FUNCTION_FOR_MAGIC_BYTES", x -> 0x00, ".wav", [:WAV]) del_format(format"FUNCTION_FOR_MAGIC_BYTES")