@@ -2,9 +2,13 @@ use std::fmt::{self, Debug};
22use std:: ops:: Deref ;
33
44use hdf5_sys:: {
5+ h5:: { hsize_t, H5_index_t , H5_iter_order_t } ,
56 h5d:: H5Dopen2 ,
67 h5g:: { H5G_info_t , H5Gcreate2 , H5Gget_info , H5Gopen2 } ,
7- h5l:: { H5Lcreate_hard , H5Lcreate_soft , H5Ldelete , H5Lexists , H5Lmove , H5L_SAME_LOC } ,
8+ h5l:: {
9+ H5L_info_t , H5L_iterate_t , H5Lcreate_hard , H5Lcreate_soft , H5Ldelete , H5Lexists ,
10+ H5Literate , H5Lmove , H5L_SAME_LOC ,
11+ } ,
812 h5p:: { H5Pcreate , H5Pset_create_intermediate_group } ,
913} ;
1014
@@ -167,6 +171,35 @@ impl Group {
167171 let name = to_cstring ( name) ?;
168172 Dataset :: from_id ( h5try ! ( H5Dopen2 ( self . id( ) , name. as_ptr( ) , H5P_DEFAULT ) ) )
169173 }
174+
175+ /// Returns names of all the members in the group, non-recursively.
176+ pub fn member_names ( & self ) -> Result < Vec < String > > {
177+ extern "C" fn members_callback (
178+ _id : hid_t , name : * const c_char , _info : * const H5L_info_t , op_data : * mut c_void ,
179+ ) -> herr_t {
180+ let other_data: & mut Vec < String > = unsafe { & mut * ( op_data as * mut Vec < String > ) } ;
181+
182+ other_data. push ( string_from_cstr ( name) ) ;
183+
184+ 0 // Continue iteration
185+ }
186+
187+ let callback_fn: H5L_iterate_t = Some ( members_callback) ;
188+ let iteration_position: * mut hsize_t = & mut { 0 as u64 } ;
189+ let mut result: Vec < String > = Vec :: new ( ) ;
190+ let other_data: * mut c_void = & mut result as * mut _ as * mut c_void ;
191+
192+ h5call ! ( H5Literate (
193+ self . id( ) ,
194+ H5_index_t :: H5_INDEX_NAME ,
195+ H5_iter_order_t :: H5_ITER_INC ,
196+ iteration_position,
197+ callback_fn,
198+ other_data
199+ ) ) ?;
200+
201+ Ok ( result)
202+ }
170203}
171204
172205#[ cfg( test) ]
@@ -351,4 +384,20 @@ pub mod tests {
351384 file. new_dataset :: < u8 > ( ) . resizable ( true ) . create_anon ( ( 10 , 20 ) ) . unwrap ( ) ;
352385 } ) ;
353386 }
387+
388+ #[ test]
389+ pub fn test_get_member_names ( ) {
390+ with_tmp_file ( |file| {
391+ file. create_group ( "a" ) . unwrap ( ) ;
392+ file. create_group ( "b" ) . unwrap ( ) ;
393+ let group_a = file. group ( "a" ) . unwrap ( ) ;
394+ let group_b = file. group ( "b" ) . unwrap ( ) ;
395+ file. new_dataset :: < u32 > ( ) . no_chunk ( ) . create ( "a/foo" , ( 10 , 20 ) ) . unwrap ( ) ;
396+ file. new_dataset :: < u32 > ( ) . no_chunk ( ) . create ( "a/123" , ( 10 , 20 ) ) . unwrap ( ) ;
397+ file. new_dataset :: < u32 > ( ) . no_chunk ( ) . create ( "a/bar" , ( 10 , 20 ) ) . unwrap ( ) ;
398+ assert_eq ! ( group_a. member_names( ) . unwrap( ) , vec![ "123" , "bar" , "foo" ] ) ;
399+ assert_eq ! ( group_b. member_names( ) . unwrap( ) . len( ) , 0 ) ;
400+ assert_eq ! ( file. member_names( ) . unwrap( ) , vec![ "a" , "b" ] ) ;
401+ } )
402+ }
354403}
0 commit comments