Skip to content

Commit 91ff2b5

Browse files
committed
updated alloc string api for default args
1 parent 4a59950 commit 91ff2b5

File tree

4 files changed

+111
-22
lines changed

4 files changed

+111
-22
lines changed

src/json_file_module.F90

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ end subroutine wrap_json_file_get_string_vec
18761876
! Get an (allocatable length) string vector from a JSON file.
18771877
! This is just a wrapper for [[json_get_alloc_string_vec_by_path]].
18781878

1879-
subroutine json_file_get_alloc_string_vec(me, path, vec, ilen, found)
1879+
subroutine json_file_get_alloc_string_vec(me, path, vec, ilen, found, default, default_ilen)
18801880

18811881
implicit none
18821882

@@ -1887,8 +1887,11 @@ subroutine json_file_get_alloc_string_vec(me, path, vec, ilen, found)
18871887
!! of each character
18881888
!! string in the array
18891889
logical(LK),intent(out),optional :: found
1890+
character(kind=CK,len=*),dimension(:),intent(in),optional :: default
1891+
integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
1892+
!! length of `default`
18901893

1891-
call me%core%get(me%p, path, vec, ilen, found)
1894+
call me%core%get(me%p, path, vec, ilen, found, default, default_ilen)
18921895

18931896
end subroutine json_file_get_alloc_string_vec
18941897
!*****************************************************************************************
@@ -1898,7 +1901,7 @@ end subroutine json_file_get_alloc_string_vec
18981901
! Alternate version of [[json_file_get_alloc_string_vec]], where "path" is kind=CDK.
18991902
! This is just a wrapper for [[wrap_json_get_alloc_string_vec_by_path]].
19001903

1901-
subroutine wrap_json_file_get_alloc_string_vec(me, path, vec, ilen, found)
1904+
subroutine wrap_json_file_get_alloc_string_vec(me, path, vec, ilen, found, default, default_ilen)
19021905

19031906
implicit none
19041907

@@ -1909,8 +1912,11 @@ subroutine wrap_json_file_get_alloc_string_vec(me, path, vec, ilen, found)
19091912
!! of each character
19101913
!! string in the array
19111914
logical(LK),intent(out),optional :: found
1915+
character(kind=CK,len=*),dimension(:),intent(in),optional :: default
1916+
integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
1917+
!! length of `default`
19121918

1913-
call me%get(to_unicode(path), vec, ilen, found)
1919+
call me%get(to_unicode(path), vec, ilen, found, default, default_ilen)
19141920

19151921
end subroutine wrap_json_file_get_alloc_string_vec
19161922
!*****************************************************************************************

src/json_get_vec_by_path_alloc.inc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
type(json_value),pointer :: p
2+
3+
if ( json%exception_thrown ) then
4+
if (present(default)) then
5+
vec = default
6+
if (present(default_ilen)) then
7+
ilen = default_ilen
8+
else
9+
allocate(ilen(size(default)))
10+
ilen = len(default)
11+
end if
12+
end if
13+
call flag_not_found(found)
14+
return
15+
end if
16+
17+
nullify(p)
18+
call json%get(me=me, path=path, p=p)
19+
20+
if (.not. associated(p)) then
21+
call json%throw_exception('Error in '//routine//':'//&
22+
' Unable to resolve path: '// trim(path),found)
23+
else
24+
call json%get(p,vec,ilen)
25+
end if
26+
27+
if ( json%exception_thrown ) then
28+
if ( present(found) .or. present(default)) then
29+
call flag_not_found(found)
30+
if (present(default)) then
31+
vec = default
32+
if (present(default_ilen)) then
33+
ilen = default_ilen
34+
else
35+
allocate(ilen(size(default)))
36+
ilen = len(default)
37+
end if
38+
end if
39+
call json%clear_exceptions()
40+
end if
41+
else
42+
if ( present(found) ) found = .true.
43+
end if

src/json_value_module.F90

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9353,8 +9353,12 @@ end subroutine json_get_alloc_string_vec
93539353
!@note An alternative to using this routine is to use [[json_get_array]] with
93549354
! a callback function that gets the string from each element and populates
93559355
! a user-defined string type.
9356+
!
9357+
!@note If the `default` argument is used, and `default_ilen` is not present,
9358+
! then `ilen` will just be returned as the length of the `default` dummy
9359+
! argument (all elements with the same length).
93569360

9357-
subroutine json_get_alloc_string_vec_by_path(json, me, path, vec, ilen, found)
9361+
subroutine json_get_alloc_string_vec_by_path(json,me,path,vec,ilen,found,default,default_ilen)
93589362

93599363
implicit none
93609364

@@ -9366,23 +9370,13 @@ subroutine json_get_alloc_string_vec_by_path(json, me, path, vec, ilen, found)
93669370
!! of each character
93679371
!! string in the array
93689372
logical(LK),intent(out),optional :: found
9373+
character(kind=CK,len=*),dimension(:),intent(in),optional :: default
9374+
integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
9375+
!! length of `default`
93699376

9370-
type(json_value),pointer :: p
9371-
9372-
call json%get(me, path, p, found)
9377+
character(kind=CK,len=*),parameter :: routine = CK_'json_get_alloc_string_vec_by_path'
93739378

9374-
if (present(found)) then
9375-
if (.not. found) return
9376-
else
9377-
if (json%exception_thrown) return
9378-
end if
9379-
9380-
call json%get(p, vec, ilen)
9381-
9382-
if (present(found) .and. json%exception_thrown) then
9383-
call json%clear_exceptions()
9384-
found = .false.
9385-
end if
9379+
#include "json_get_vec_by_path_alloc.inc"
93869380

93879381
end subroutine json_get_alloc_string_vec_by_path
93889382
!*****************************************************************************************
@@ -9391,7 +9385,7 @@ end subroutine json_get_alloc_string_vec_by_path
93919385
!>
93929386
! Alternate version of [[json_get_alloc_string_vec_by_path]], where "path" is kind=CDK
93939387

9394-
subroutine wrap_json_get_alloc_string_vec_by_path(json,me,path,vec,ilen,found)
9388+
subroutine wrap_json_get_alloc_string_vec_by_path(json,me,path,vec,ilen,found,default,default_ilen)
93959389

93969390
implicit none
93979391

@@ -9403,8 +9397,11 @@ subroutine wrap_json_get_alloc_string_vec_by_path(json,me,path,vec,ilen,found)
94039397
!! of each character
94049398
!! string in the array
94059399
logical(LK),intent(out),optional :: found
9400+
character(kind=CK,len=*),dimension(:),intent(in),optional :: default
9401+
integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
9402+
!! length of `default`
94069403

9407-
call json%get(me,to_unicode(path),vec,ilen,found)
9404+
call json%get(me,to_unicode(path),vec,ilen,found,default,default_ilen)
94089405

94099406
end subroutine wrap_json_get_alloc_string_vec_by_path
94109407
!*****************************************************************************************

src/tests/jf_test_46.F90

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ subroutine test_46(error_cnt)
3434
logical(LK) :: lval
3535
character(kind=CK,len=:),allocatable :: cval
3636
character(kind=CK,len=1),dimension(:),allocatable :: cvec
37+
character(kind=CK,len=:),dimension(:),allocatable :: cvec2
38+
integer(IK),dimension(:),allocatable :: ilen
3739

3840
character(kind=CK,len=1),dimension(1) :: cvec_default = [CK_'1']
41+
integer(IK),dimension(1) :: ilen_default = [1]
3942

4043
write(error_unit,'(A)') ''
4144
write(error_unit,'(A)') '================================='
@@ -89,6 +92,16 @@ subroutine test_46(error_cnt)
8992
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
9093
error_cnt = error_cnt + 1
9194
end if
95+
call json%get(p, CK_'not_there', cvec2, ilen, found, default=cvec_default)
96+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
97+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
98+
error_cnt = error_cnt + 1
99+
end if
100+
call json%get(p, CK_'not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
101+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
102+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
103+
error_cnt = error_cnt + 1
104+
end if
92105

93106
! default:
94107
call json%get(p, 'not_there', ival, found, default=99_IK)
@@ -120,6 +133,16 @@ subroutine test_46(error_cnt)
120133
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
121134
error_cnt = error_cnt + 1
122135
end if
136+
call json%get(p, 'not_there', cvec2, ilen, found, default=cvec_default)
137+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
138+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
139+
error_cnt = error_cnt + 1
140+
end if
141+
call json%get(p, 'not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
142+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
143+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
144+
error_cnt = error_cnt + 1
145+
end if
123146

124147
call json%destroy(p)
125148

@@ -159,6 +182,16 @@ subroutine test_46(error_cnt)
159182
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
160183
error_cnt = error_cnt + 1
161184
end if
185+
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default)
186+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
187+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
188+
error_cnt = error_cnt + 1
189+
end if
190+
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
191+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
192+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
193+
error_cnt = error_cnt + 1
194+
end if
162195

163196
! default:
164197
call json_f%get('not_there', ival, found, default=99_IK)
@@ -190,6 +223,16 @@ subroutine test_46(error_cnt)
190223
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
191224
error_cnt = error_cnt + 1
192225
end if
226+
call json_f%get('not_there', cvec2, ilen, found, default=cvec_default)
227+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
228+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
229+
error_cnt = error_cnt + 1
230+
end if
231+
call json_f%get('not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
232+
if (json%failed() .or. found .or. all(cvec2 /= cvec_default) .or. all(ilen/=1_IK)) then
233+
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
234+
error_cnt = error_cnt + 1
235+
end if
193236

194237
if (error_cnt==0) then
195238
write(error_unit,'(A)') 'Success!'

0 commit comments

Comments
 (0)