Skip to content

Commit 9921fd8

Browse files
committed
add %intent() method
1 parent 5ec7eb9 commit 9921fd8

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

API.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ tf = is_hdf5('myfile.txt') !< probably false
187187
tf = is_hdf5('myfile.h5') !< true if a valid HDF5 file
188188
```
189189

190+
Get the read/write or read-only intent of the HDF5 file.
191+
192+
```fortran
193+
h % intent(filename)
194+
!! returns the read/write or read-only intent of the file, e.g. H5F_ACC_RDWR_F, H5F_ACC_RDONLY_F
195+
```
196+
190197
These are more advanced inquiries into the memory layout of the dataset, for advanced users:
191198

192199
```fortran

fpm.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ main = "test_attributes.f90"
3030
name = "cast"
3131
main = "test_cast.f90"
3232

33+
[[test]]
34+
name = "intent"
35+
main = "test_intent.f90"
36+
3337
[[test]]
3438
name = "write"
3539
main = "test_write.f90"

src/interface.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module h5fortran
4040
procedure, public :: create => hdf_create_user
4141
procedure, public :: flush => hdf_flush
4242
procedure, public :: filesize => hdf_filesize
43+
procedure, public :: intent => hdf_get_intent
4344
procedure, public :: ndim => hdf_get_ndim
4445
procedure, public :: ndims => hdf_get_ndim !< legacy
4546
procedure, public :: shape => hdf_get_shape
@@ -691,6 +692,11 @@ module subroutine h5close(self, close_hdf5_interface)
691692
logical, intent(in), optional :: close_hdf5_interface
692693
end subroutine
693694

695+
module integer function hdf_get_intent(self)
696+
!! get the intent of an object (read, write, readwrite)
697+
class(hdf5_file), intent(in) :: self
698+
end function
699+
694700
module logical function is_open(self)
695701
!! check if file handle is open
696702
class(hdf5_file), intent(in) :: self

src/utils.f90

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
h5eset_auto_f, &
55
h5iis_valid_f, h5iget_name_f, H5Iget_type_f, &
66
h5open_f, h5close_f, &
7-
H5Fopen_f, h5fcreate_f, h5fclose_f, h5fis_hdf5_f, h5fget_filesize_f, &
7+
H5Fopen_f, h5fcreate_f, h5fclose_f, h5fis_hdf5_f, h5fget_filesize_f, H5Fget_intent_f, &
88
h5fget_obj_count_f, h5fget_obj_ids_f, h5fget_name_f, &
99
h5sselect_hyperslab_f, h5screate_simple_f, &
1010
H5Sget_simple_extent_ndims_f, H5Sget_simple_extent_dims_f, H5Sget_simple_extent_npoints_f, &
@@ -226,6 +226,16 @@
226226
end procedure h5close
227227

228228

229+
module procedure hdf_get_intent
230+
231+
integer :: ierr
232+
233+
call H5Fget_intent_f(self%file_id, hdf_get_intent, ierr)
234+
call estop(ierr, "hdf_get_intent:h5fget_intent: could not get file intent", self%filename)
235+
236+
end procedure hdf_get_intent
237+
238+
229239
module procedure is_open
230240

231241
integer :: obj_type, ier

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ endfunction(setup_test)
7272
# --- setup unit tests
7373

7474
set(test_names array attributes
75-
cast deflate_write deflate_read deflate_props destructor exist
75+
cast deflate_write deflate_read deflate_props destructor exist intent
7676
groups layout lt scalar shape string version write
7777
fail_read_size_mismatch fail_read_rank_mismatch fail_nonexist_variable)
7878
if(HAVE_IEEE_ARITH)

test/test_intent.f90

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
program test
2+
3+
use hdf5
4+
use h5fortran
5+
use, intrinsic :: iso_fortran_env, only: stderr=>error_unit
6+
7+
implicit none
8+
9+
type(hdf5_file) :: h
10+
character(*), parameter :: fn = 'test_intent.h5'
11+
integer :: intent(5), exp(5), i
12+
13+
call H5open_f(i)
14+
if(i /= 0) error stop "H5open_f failed [0]"
15+
16+
exp = [H5F_ACC_RDWR_F, H5F_ACC_RDONLY_F, H5F_ACC_RDONLY_F, H5F_ACC_RDWR_F, H5F_ACC_RDWR_F]
17+
18+
!> These are the only two possiblities for the intent of a file
19+
print '(a,i0)', "H5F_ACC_RDONLY_F = ", H5F_ACC_RDONLY_F
20+
print '(a,i0)', "H5F_ACC_RDWR_F = ", H5F_ACC_RDWR_F
21+
22+
call h % open(fn, action='w', debug=.true.)
23+
intent(1) = h % intent()
24+
call h % close()
25+
26+
call h % open(fn, debug=.true.)
27+
intent(2) = h % intent()
28+
call h % close()
29+
30+
call h % open(fn, action='r', debug=.true.)
31+
intent(3) = h % intent()
32+
call h % close()
33+
34+
call h % open(fn, action='r+', debug=.true.)
35+
intent(4) = h % intent()
36+
call h % close()
37+
38+
call h % open(fn, action='rw', debug=.true.)
39+
intent(5) = h % intent()
40+
call h % close()
41+
42+
call H5close_f(i)
43+
if (i /= 0) error stop "H5close() failed"
44+
45+
if (any(intent /= exp)) then
46+
write(stderr, '(a)') 'test_intent: intent does not match expected values'
47+
write(stderr, '(5i0)') intent
48+
write(stderr, '(5i0)') exp
49+
error stop 'intent test failed'
50+
else
51+
print '(a)', 'OK: intent matches expected values'
52+
end if
53+
54+
end program

0 commit comments

Comments
 (0)