Skip to content

Commit 4bf7c2e

Browse files
authored
Merge pull request #159 from jsams/rdata_single
Add support for Rdata single format (.rds files)
2 parents c3e1ee7 + 7a8b810 commit 4bf7c2e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/registry.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ end
1818

1919
add_format(format"RData", detect_rdata, [".rda", ".RData", ".rdata"], [:RData, LOAD])
2020

21+
function detect_rdata_single(io)
22+
seekstart(io)
23+
res = read(io, UInt8) in (UInt8('A'), UInt8('B'), UInt8('X')) &&
24+
(c = read(io, UInt8); c == UInt8('\n') || (c == UInt8('\r') && read(io, UInt8) == UInt8('\n')))
25+
seekstart(io)
26+
return res
27+
end
28+
29+
add_format(format"RDataSingle", detect_rdata_single, [".rds"], [:RData, LOAD])
30+
2131
add_format(format"CSV", (), [".csv"], [:CSVFiles])
2232
add_format(format"TSV", (), [".tsv"], [:CSVFiles])
2333
add_format(format"Feather", "FEA1", [".feather"], [:FeatherFiles])

test/files/minimal_ascii.rds

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
A
2+
2
3+
197634
4+
131840
5+
787
6+
1
7+
14
8+
2
9+
1.1
10+
2.2
11+
1026
12+
1
13+
262153
14+
5
15+
names
16+
16
17+
1
18+
262153
19+
3
20+
num
21+
1026
22+
1
23+
262153
24+
9
25+
row.names
26+
13
27+
2
28+
NA
29+
-2
30+
1026
31+
1
32+
262153
33+
5
34+
class
35+
16
36+
1
37+
262153
38+
10
39+
data.frame
40+
254

test/query.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ end
332332
@test (position(io) in (5, 6))
333333
end
334334
end
335+
@testset "RDS detection" begin
336+
q = query(joinpath(file_dir, "minimal_ascii.rds"))
337+
@test typeof(q) == File{format"RDataSingle"}
338+
open(q) do io
339+
@test position(io) == 0
340+
@test FileIO.detect_rdata_single(io)
341+
# need to seek to beginning of file where data structure starts
342+
@test position(io) == 0
343+
end
344+
end
335345
@testset "Format with function for magic bytes" begin
336346
add_format(format"FUNCTION_FOR_MAGIC_BYTES", x -> 0x00, ".wav", [:WAV])
337347
del_format(format"FUNCTION_FOR_MAGIC_BYTES")

0 commit comments

Comments
 (0)