@@ -3,45 +3,51 @@ class Attribute
33 def initialize ( dataset_id , attr_name )
44 @dataset_id = dataset_id
55 @attr_name = attr_name
6- @attr_id = FFI . H5Aopen ( @dataset_id , @attr_name , 0 )
7- raise 'Failed to open attribute' if @attr_id < 0
6+ @attr_id = HDF5 :: FFI . H5Aopen ( @dataset_id , @attr_name , HDF5 :: DEFAULT_PROPERTY_LIST )
7+ raise HDF5 :: Error , 'Failed to open attribute' if @attr_id < 0
88 end
99
1010 def read
11- type_id = FFI . H5Aget_type ( @attr_id )
12- space_id = FFI . H5Aget_space ( @attr_id )
11+ type_id = HDF5 :: FFI . H5Aget_type ( @attr_id )
12+ space_id = HDF5 :: FFI . H5Aget_space ( @attr_id )
1313
14- size = FFI . H5Sget_simple_extent_npoints ( space_id )
14+ size = HDF5 :: FFI . H5Sget_simple_extent_npoints ( space_id )
1515
1616 buffer = \
17- case FFI . H5Tget_class ( type_id )
17+ case HDF5 :: FFI . H5Tget_class ( type_id )
1818 when :H5T_INTEGER
1919 ::FFI ::MemoryPointer . new ( :int , size )
2020 when :H5T_FLOAT
2121 ::FFI ::MemoryPointer . new ( :double , size )
2222 when :H5T_STRING
2323 ::FFI ::MemoryPointer . new ( :pointer , size )
2424 else
25- raise 'Unsupported data type'
25+ raise HDF5 :: Error , 'Unsupported data type'
2626 end
2727
28- status = FFI . H5Aread ( @attr_id , type_id , buffer )
29- raise 'Failed to read attribute' if status < 0
28+ status = HDF5 :: FFI . H5Aread ( @attr_id , type_id , buffer )
29+ raise HDF5 :: Error , 'Failed to read attribute' if status < 0
3030
31- case FFI . H5Tget_class ( type_id )
31+ case HDF5 :: FFI . H5Tget_class ( type_id )
3232 when :H5T_INTEGER
3333 buffer . read_array_of_int ( size )
3434 when :H5T_FLOAT
3535 buffer . read_array_of_double ( size )
3636 when :H5T_STRING
3737 buffer . read_pointer . read_string
3838 else
39- raise 'Unsupported data type'
39+ raise HDF5 :: Error , 'Unsupported data type'
4040 end
41+ ensure
42+ HDF5 ::FFI . H5Tclose ( type_id ) if type_id && type_id >= 0
43+ HDF5 ::FFI . H5Sclose ( space_id ) if space_id && space_id >= 0
4144 end
4245
4346 def close
44- FFI . H5Aclose ( @attr_id )
47+ return if @attr_id . nil?
48+
49+ HDF5 ::FFI . H5Aclose ( @attr_id )
50+ @attr_id = nil
4551 end
4652 end
4753
0 commit comments