|
| 1 | +submodule (h5fortran:hdf5_read) iterate_smod |
| 2 | + use hdf5, only : H5Literate_f, h5l_info_t, H5O_info_t, H5_INDEX_NAME_F, H5_ITER_NATIVE_F, & |
| 3 | + H5Gopen_f, H5Gclose_f, H5Oget_info_by_name_f, H5O_TYPE_GROUP_F, & |
| 4 | + H5O_TYPE_DATASET_F, H5O_TYPE_NAMED_DATATYPE_F |
| 5 | + use, intrinsic :: iso_c_binding, only: c_char, c_long, c_funloc |
| 6 | + |
| 7 | + implicit none |
| 8 | + |
| 9 | + interface |
| 10 | + subroutine user_callback_interface(group_name, object_name, object_type) |
| 11 | + character(*), intent(in) :: group_name |
| 12 | + !! The name of the group being traversed. |
| 13 | + character(*), intent(in) :: object_name |
| 14 | + !! The name of the object encountered. |
| 15 | + character(*), intent(in) :: object_type |
| 16 | + !!A short description such as "group", "dataset", |
| 17 | + !! "datatype", or "other" |
| 18 | + end subroutine |
| 19 | + end interface |
| 20 | + |
| 21 | + type :: iterate_data_t |
| 22 | + procedure(user_callback_interface), nopass, pointer :: callback => null() |
| 23 | + end type iterate_data_t |
| 24 | + |
| 25 | +contains |
| 26 | + |
| 27 | + module procedure hdf_iterate |
| 28 | + use, intrinsic :: iso_c_binding, only: c_funptr, C_NULL_PTR, c_int |
| 29 | + implicit none |
| 30 | + integer(hid_t) :: group_id |
| 31 | + integer(c_int) :: status |
| 32 | + integer(hsize_t) :: idx |
| 33 | + type(c_funptr) :: funptr |
| 34 | + type(c_ptr) :: op_data_ptr |
| 35 | + integer(c_int) :: return_value |
| 36 | + |
| 37 | + type(iterate_data_t) :: data |
| 38 | + |
| 39 | + ! Fill the iteration data with the user’s group name and callback. |
| 40 | + data % callback => callback |
| 41 | + |
| 42 | + ! Open the group. |
| 43 | + call H5Gopen_f(self%file_id, trim(group_name), group_id, status) |
| 44 | + call estop(status, "hdf_iterate:H5Gopen_f", self%filename, "Error opening group: " // trim(group_name)) |
| 45 | + |
| 46 | + idx = 0 |
| 47 | + op_data_ptr = C_NULL_PTR |
| 48 | + ! Get the C function pointer for our internal callback. |
| 49 | + funptr = c_funloc(internal_iterate_callback) |
| 50 | + |
| 51 | + ! Call H5Literate_f to iterate over the group. |
| 52 | + call H5Literate_f(group_id, H5_INDEX_NAME_F, H5_ITER_NATIVE_F, idx, & |
| 53 | + funptr, op_data_ptr, return_value, status) |
| 54 | + call estop(status, "hdf_iterate:H5Literate_f", self%filename, "Error during iteration of group: " // trim(group_name)) |
| 55 | + |
| 56 | + ! Close the group and file. |
| 57 | + call H5Gclose_f(group_id, status) |
| 58 | + |
| 59 | + contains |
| 60 | + |
| 61 | + integer(c_int) function internal_iterate_callback(grp_id, name, info, op_data) bind(C) |
| 62 | + !! internal_iterate_callback: |
| 63 | + !! |
| 64 | + !! This is the callback procedure that will be passed to H5Literate_f. |
| 65 | + !! It matches HDF5’s expected signature (using bind(C)) and is called |
| 66 | + !! for each object in the group. |
| 67 | + !! |
| 68 | + !! It extracts the object name from the provided character array, |
| 69 | + !! calls H5Oget_info_by_name_f to determine the object type, and then |
| 70 | + !! calls the user's callback with the high-level parameters. |
| 71 | + use ISO_C_BINDING, only: c_int, c_ptr, c_null_char |
| 72 | + implicit none |
| 73 | + integer(c_long), value :: grp_id |
| 74 | + character(kind=c_char, len=1) :: name(0:255) |
| 75 | + type(h5l_info_t) :: info |
| 76 | + type(c_ptr) :: op_data |
| 77 | + |
| 78 | + integer :: status, i, len |
| 79 | + type(H5O_info_t) :: infobuf |
| 80 | + character(len=256) :: name_string |
| 81 | + character(:), allocatable :: object_type |
| 82 | + |
| 83 | + ! Build a Fortran string from the character array. |
| 84 | + do i = 0, 255 |
| 85 | + len = i |
| 86 | + if (name(i) == c_null_char) exit |
| 87 | + name_string(i+1:i+1) = name(i)(1:1) |
| 88 | + end do |
| 89 | + |
| 90 | + ! Retrieve object info using the object name. |
| 91 | + call H5Oget_info_by_name_f(grp_id, name_string(1:len), infobuf, status) |
| 92 | + if (status /= 0) then |
| 93 | + internal_iterate_callback = status |
| 94 | + return |
| 95 | + end if |
| 96 | + |
| 97 | + if(infobuf % type == H5O_TYPE_GROUP_F)then |
| 98 | + object_type = "group" |
| 99 | + else if(infobuf % type == H5O_TYPE_DATASET_F)then |
| 100 | + object_type = "dataset" |
| 101 | + else if(infobuf % type == H5O_TYPE_NAMED_DATATYPE_F)then |
| 102 | + object_type = "datatype" |
| 103 | + else |
| 104 | + object_type = "other" |
| 105 | + endif |
| 106 | + |
| 107 | + ! Call the user’s callback procedure. |
| 108 | + call data % callback(group_name, name_string(1:len), object_type) |
| 109 | + |
| 110 | + internal_iterate_callback = 0 ! Indicate success. |
| 111 | + end function internal_iterate_callback |
| 112 | + |
| 113 | + end procedure hdf_iterate |
| 114 | + |
| 115 | +end submodule |
0 commit comments