Skip to content

Commit e096705

Browse files
authored
refactor: cleanup a bit (#5)
1 parent fb269e0 commit e096705

File tree

6 files changed

+23
-80
lines changed

6 files changed

+23
-80
lines changed

src/dataset.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function CDFDataset(filename)
2626
compression = CompressionType(compression_bytes)
2727
RecordSizeType = is_cdf_v3(magic_bytes) ? UInt64 : UInt32
2828
# Parse CDF header to extract version, majority, and compression
29-
cdr = load_cdr(io, 8, RecordSizeType)
30-
gdr = load_gdr(io, cdr.gdr_offset, RecordSizeType)
29+
cdr = CDR(io, 8, RecordSizeType)
30+
gdr = GDR(io, cdr.gdr_offset, RecordSizeType)
3131
return CDFDataset{compression, RecordSizeType}(filename, cdr, gdr)
3232
end
3333
end
@@ -105,7 +105,7 @@ function Base.keys(cdf::CDFDataset)
105105
i = 1
106106
for current_offset in (gdr.rVDRhead, gdr.zVDRhead)
107107
while current_offset != 0
108-
vdr = load_vdr(io, current_offset, RecordSizeType)
108+
vdr = VDR(io, current_offset, RecordSizeType)
109109
varnames[i] = vdr.name
110110
i += 1
111111
current_offset = vdr.vdr_next

src/loading/cdr.jl

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,17 @@
22
# Handles structured loading of the main CDF file header
33

44
"""
5-
load_cdr(io::IO, offset::Int, is_v3::Bool) -> CDR
5+
CDR(io::IO, RecordSizeType) -> CDR
66
77
Load a CDF Descriptor Record from the IO stream at the specified offset.
88
This follows the CDF specification for CDR record structure.
9-
10-
# Arguments
11-
- `io`: IO stream to read from
12-
- `offset`: Byte offset where CDR starts (typically 8)
13-
- `is_v3`: Whether this is a CDF v3 file (affects field sizes)
14-
15-
# Returns
16-
- `CDR`: Parsed CDR structure with all fields
179
"""
18-
@inline function load_cdr(io::IO, offset, RecordSizeType)
19-
seek(io, offset)
10+
@inline function CDR(io::IO, RecordSizeType)
2011
# Read header
2112
header = Header(io, RecordSizeType)
2213
@assert header.record_type == 1 "Invalid CDR record type"
2314
# Read remaining CDR fields in order as per CDF specification
2415
gdr_offset = read_be(io, RecordSizeType)
2516
fields = read_be(io, 9, Int32)
2617
return CDR(header, gdr_offset, fields...)
27-
end
28-
29-
"""
30-
parse_cdf_header(io::IO, magic_bytes::Vector{UInt8}, compression_bytes::Vector{UInt8}) -> FileHeader
31-
32-
Parse the CDF file header using a structured approach.
33-
This is the main entry point for reading CDF file metadata.
34-
35-
# Returns
36-
- `FileHeader`: Parsed file header with version, majority, compression, and CDR
37-
"""
38-
function parse_cdf_header(io::IO, RecordSizeType, compression)
39-
# Load CDR record starting at offset 8
40-
cdr = load_cdr(io, 8, RecordSizeType)
41-
# Extract file properties from CDR
42-
version, majority = extract_file_properties(cdr)
43-
44-
return FileHeader(version, majority, compression, cdr)
45-
end
18+
end

src/loading/gdr.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""
2-
load_gdr(io::IO, offset::UInt64, is_v3::Bool) -> GDR
2+
GDR(io::IO, RecordSizeType)
33
44
Load a Global Descriptor Record from the IO stream at the specified offset.
55
"""
6-
@inline function load_gdr(io::IO, offset::UInt64, RecordSizeType)
7-
seek(io, offset)
6+
@inline function GDR(io::IO, RecordSizeType)
87
# Read header
98
header = Header(io, RecordSizeType)
109
@assert header.record_type == 2

src/loading/vdr.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ function readname(io::IO)
99
end
1010

1111
"""
12-
load_vdr(io::IO, offset, RecordSizeType) -> VDR
12+
VDR(io::IO, RecordSizeType) -> VDR
1313
1414
Load a Variable Descriptor Record from the IO stream at the specified offset.
1515
"""
16-
@inline function load_vdr(io::IO, offset, RecordSizeType)
17-
seek(io, offset)
16+
@inline function VDR(io::IO, RecordSizeType)
1817
header = Header(io, RecordSizeType)
1918
@assert header.record_type in (3, 8)
2019

@@ -36,12 +35,8 @@ Load a Variable Descriptor Record from the IO stream at the specified offset.
3635
)
3736
end
3837

39-
function load_rVDR(io::IO, offset, RecordSizeType)
40-
return load_vdr(io, offset, RecordSizeType)
41-
end
42-
4338
function load_zVDR(io::IO, offset, RecordSizeType)
44-
vdr = load_vdr(io, offset, RecordSizeType)
39+
vdr = VDR(io, offset, RecordSizeType)
4540
z_num_dims = read_uint32_be(io)
4641

4742
# Read dimension sizes

src/loading/vxr.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""
2-
VXR(io::IO, offset, RecordSizeType)
2+
VXR(io::IO, RecordSizeType)
33
44
Load a Variable Index Record from the IO stream at the specified offset.
55
"""
6-
function VXR(io::IO, offset, RecordSizeType)
7-
seek(io, offset)
8-
6+
function VXR(io::IO, RecordSizeType)
97
# Read header
108
header = Header(io, RecordSizeType)
119
@assert header.record_type == 6 "Invalid VXR record type"

src/records.jl

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ version(cdr::CDR; verbose = true) = verbose ? (cdr.version, cdr.release, cdr.inc
3838
Majority(cdr::CDR) = (cdr.flags & 0x01) != 0 ? Majority(0) : Majority(1) # Row=0, Column=1
3939
is_cdf_v3(cdr::CDR) = cdr.version == 3
4040

41-
"""
42-
File header containing parsed information from CDR
43-
"""
44-
struct FileHeader
45-
version::Tuple{UInt32, UInt32, UInt32}
46-
majority::Majority
47-
compression::CompressionType
48-
cdr::CDR # Store the parsed CDR for reference
49-
end
50-
5141
"""
5242
Global Descriptor Record (GDR) - contains global information about the CDF file
5343
Points to variable and attribute descriptor records
@@ -143,6 +133,15 @@ struct VVR{T}
143133
data::Vector{T} # Raw variable data
144134
end
145135

136+
for R in (:CDR, :GDR, :VXR, :VDR)
137+
@eval begin
138+
@inline function $R(io::IO, offset, RecordSizeType)
139+
seek(io, offset)
140+
return $R(io, RecordSizeType)
141+
end
142+
end
143+
end
144+
146145
# Utility functions to decode CDR flags
147146
"""
148147
decode_cdr_flags(flags::UInt32)
@@ -165,20 +164,6 @@ function decode_cdr_flags(flags)
165164
)
166165
end
167166

168-
"""
169-
extract_file_properties(cdr::CDR)
170-
171-
Extract high-level file properties from parsed CDR record.
172-
"""
173-
function extract_file_properties(cdr)
174-
# Extract version tuple
175-
version = (cdr.version, cdr.release, cdr.increment)
176-
177-
# Extract majority from flags (bit 0: 1=row major, 0=column major)
178-
majority = (cdr.flags & 0x01) != 0 ? Majority(0) : Majority(1) # Row=0, Column=1
179-
return (version, majority)
180-
end
181-
182167
# Pretty printing for CDR structure
183168
function Base.show(io::IO, cdr::CDR)
184169
flag_info = decode_cdr_flags(cdr.flags)
@@ -195,11 +180,4 @@ function Base.show(io::IO, cdr::CDR)
195180
println(io, " - Checksum Used: $(flag_info.checksum_used)")
196181
println(io, " - MD5 Checksum: $(flag_info.md5_checksum)")
197182
return println(io, " Identifier: $(cdr.identifier)")
198-
end
199-
200-
function Base.show(io::IO, header::FileHeader)
201-
println(io, "CDF File Header:")
202-
println(io, " Version: $(header.version)")
203-
println(io, " Majority: $(header.majority)")
204-
return println(io, " Compression: $(header.compression)")
205-
end
183+
end

0 commit comments

Comments
 (0)