Skip to content

Commit f37341f

Browse files
authored
Merge pull request #53 from jbabyhacker/feature/get_member_names
Added functionality to get all the members in a group
2 parents ef94007 + bd7fc83 commit f37341f

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

hdf5-sys/src/h5o.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ pub const H5O_HDR_ALL_FLAGS: c_uint = H5O_HDR_CHUNK0_SIZE
4444
pub const H5O_SHMESG_MAX_NINDEXES: c_uint = 8;
4545
pub const H5O_SHMESG_MAX_LIST_SIZE: c_uint = 5000;
4646

47+
#[cfg(hdf5_1_10_3)]
48+
pub const H5O_INFO_BASIC: c_uint = 0x0001;
49+
#[cfg(hdf5_1_10_3)]
50+
pub const H5O_INFO_TIME: c_uint = 0x0002;
51+
#[cfg(hdf5_1_10_3)]
52+
pub const H5O_INFO_NUM_ATTRS: c_uint = 0x0004;
53+
#[cfg(hdf5_1_10_3)]
54+
pub const H5O_INFO_HDR: c_uint = 0x0008;
55+
#[cfg(hdf5_1_10_3)]
56+
pub const H5O_INFO_META_SIZE: c_uint = 0x0010;
57+
#[cfg(hdf5_1_10_3)]
58+
pub const H5O_INFO_ALL: c_uint =
59+
H5O_INFO_BASIC | H5O_INFO_TIME | H5O_INFO_NUM_ATTRS | H5O_INFO_HDR | H5O_INFO_META_SIZE;
60+
4761
#[repr(C)]
4862
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
4963
pub enum H5O_type_t {

src/hl/group.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use std::fmt::{self, Debug};
22
use std::ops::Deref;
33

44
use 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

Comments
 (0)