Skip to content

Commit

Permalink
feat: add paramcard_output
Browse files Browse the repository at this point in the history
  • Loading branch information
tueda committed Jan 8, 2022
1 parent b7f6994 commit bae2725
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 4 deletions.
59 changes: 59 additions & 0 deletions src/paramcard.f90
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ module paramcard
public :: paramcard_parse
public :: paramcard_summary
public :: paramcard_format
public :: paramcard_output

interface paramcard_get
!! Retrieve a parameter.
Expand Down Expand Up @@ -970,6 +971,51 @@ function paramcard_format(fmt) result(res)
res = result
end function paramcard_format

subroutine paramcard_output(file_param, format_param)
!! Write output to a file with a format, which are specified by parameters.

character(len=*), intent(in), optional :: file_param
!! The parameter to specify the output file.
character(len=*), intent(in), optional :: format_param
!! The parameter to specify the output format.

character(len=:), allocatable :: file_param_, format_param_, file, format, output
integer :: lun, ios

if (present(file_param)) then
file_param_ = file_param
else
file_param_ = 'output_file'
end if

if (present(format_param)) then
format_param_ = format_param
else
format_param_ = 'output_format'
end if

if (file_param_ == '' .or. format_param_ == '') then
return
end if

call paramcard_get(file_param_, file, '')
call paramcard_get(format_param_, format, '')

if (file == '' .or. format == '') then
return
end if

output = paramcard_format(format)

open (newunit=lun, file=file, status='replace', iostat=ios)
if (ios /= 0) then
write (error_unit, '(a)') '[ERROR] paramcard: failed to open: '//file
error stop
end if
write (lun, '(a)'), output
close (lun)
end subroutine paramcard_output

subroutine get_param_str_impl(name, variable, canon_name)
!! Retrieve a string parameter.

Expand Down Expand Up @@ -1351,3 +1397,16 @@ subroutine paramcard_summary

call paramcard_summary_f90
end subroutine paramcard_summary

subroutine paramcard_output(file_param, format_param)
!! Write output to a file with a format, which are specified as parameters.
use paramcard, only: paramcard_output_f90 => paramcard_output
implicit none

character(len=*), intent(in) :: file_param
!! The parameter to specify the output file.
character(len=*), intent(in) :: format_param
!! The parameter to specify the output format.

call paramcard_output_f90(file_param, format_param)
end subroutine paramcard_output
59 changes: 59 additions & 0 deletions src/paramcard.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ module paramcard
public :: paramcard_parse
public :: paramcard_summary
public :: paramcard_format
public :: paramcard_output

interface paramcard_get
!! Retrieve a parameter.
Expand Down Expand Up @@ -577,6 +578,51 @@ contains
res = result
end function paramcard_format

subroutine paramcard_output(file_param, format_param)
!! Write output to a file with a format, which are specified by parameters.

character(len=*), intent(in), optional :: file_param
!! The parameter to specify the output file.
character(len=*), intent(in), optional :: format_param
!! The parameter to specify the output format.

character(len=:), allocatable :: file_param_, format_param_, file, format, output
integer :: lun, ios

if (present(file_param)) then
file_param_ = file_param
else
file_param_ = 'output_file'
end if

if (present(format_param)) then
format_param_ = format_param
else
format_param_ = 'output_format'
end if

if (file_param_ == '' .or. format_param_ == '') then
return
end if

call paramcard_get(file_param_, file, '')
call paramcard_get(format_param_, format, '')

if (file == '' .or. format == '') then
return
end if

output = paramcard_format(format)

open (newunit=lun, file=file, status='replace', iostat=ios)
if (ios /= 0) then
write (error_unit, '(a)') '[ERROR] paramcard: failed to open: '//file
error stop
end if
write (lun, '(a)'), output
close (lun)
end subroutine paramcard_output

subroutine get_param_str_impl(name, variable, canon_name)
!! Retrieve a string parameter.

Expand Down Expand Up @@ -898,3 +944,16 @@ subroutine paramcard_summary

call paramcard_summary_f90
end subroutine paramcard_summary

subroutine paramcard_output(file_param, format_param)
!! Write output to a file with a format, which are specified as parameters.
use paramcard, only: paramcard_output_f90 => paramcard_output
implicit none

character(len=*), intent(in) :: file_param
!! The parameter to specify the output file.
character(len=*), intent(in) :: format_param
!! The parameter to specify the output format.

call paramcard_output_f90(file_param, format_param)
end subroutine paramcard_output
43 changes: 42 additions & 1 deletion test/test-paramcard.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ subroutine collect_test_paramcard(testsuite)
new_unittest('paramcard_set_real64', test_paramcard_set_real64), &
new_unittest('paramcard_load', test_paramcard_load), &
new_unittest('paramcard_parse', test_paramcard_parse), &
new_unittest('paramcard_format', test_paramcard_format) &
new_unittest('paramcard_format', test_paramcard_format), &
new_unittest('paramcard_output', test_paramcard_output) &
]
end subroutine collect_test_paramcard

Expand Down Expand Up @@ -661,8 +662,48 @@ subroutine test_paramcard_format(error)
call paramcard_set('x', 42)
s = paramcard_format('{x}')
call check(error, s, '42')
if (allocated(error)) return
end subroutine test_paramcard_format

subroutine test_paramcard_output(error)
type(error_type), allocatable, intent(out) :: error

integer :: n

! Output.
call paramcard_parse('paramcard command: clear')
call paramcard_set('output_file', 'test_output_1.txt')
call paramcard_set('output_format', '{a} = {b}')
call paramcard_set('a', 'n')
call paramcard_set('b', 123)
call paramcard_output

! Check the output.
call paramcard_parse('paramcard command: clear')
call paramcard_parse('paramcard command: load, test_output_1.txt')
call paramcard_get('n', n)
call check(error, n, 123)
if (allocated(error)) return

! Output, with specifying file_param and format_param
call paramcard_parse('paramcard command: clear')
call paramcard_set('outfile', 'test_output_2.txt')
call paramcard_set('outfmt', '{a} = {b}')
call paramcard_set('a', 'n')
call paramcard_set('b', 123)
call paramcard_output('outfile', 'outfmt')

! Check the output.
call paramcard_parse('paramcard command: clear')
call paramcard_parse('paramcard command: load, test_output_2.txt')
call paramcard_get('n', n)
call check(error, n, 123)
if (allocated(error)) return

! Teardown.
call delete_file('test_output_1.txt')
call delete_file('test_output_2.txt')
end subroutine test_paramcard_output
end module test_paramcard

program tester
Expand Down
43 changes: 42 additions & 1 deletion test/test-paramcard.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ contains
#:endfor
new_unittest('paramcard_load', test_paramcard_load), &
new_unittest('paramcard_parse', test_paramcard_parse), &
new_unittest('paramcard_format', test_paramcard_format) &
new_unittest('paramcard_format', test_paramcard_format), &
new_unittest('paramcard_output', test_paramcard_output) &
]
end subroutine collect_test_paramcard

Expand Down Expand Up @@ -245,8 +246,48 @@ contains
call paramcard_set('x', 42)
s = paramcard_format('{x}')
call check(error, s, '42')
if (allocated(error)) return
end subroutine test_paramcard_format

subroutine test_paramcard_output(error)
type(error_type), allocatable, intent(out) :: error

integer :: n

! Output.
call paramcard_parse('paramcard command: clear')
call paramcard_set('output_file', 'test_output_1.txt')
call paramcard_set('output_format', '{a} = {b}')
call paramcard_set('a', 'n')
call paramcard_set('b', 123)
call paramcard_output

! Check the output.
call paramcard_parse('paramcard command: clear')
call paramcard_parse('paramcard command: load, test_output_1.txt')
call paramcard_get('n', n)
call check(error, n, 123)
if (allocated(error)) return

! Output, with specifying file_param and format_param
call paramcard_parse('paramcard command: clear')
call paramcard_set('outfile', 'test_output_2.txt')
call paramcard_set('outfmt', '{a} = {b}')
call paramcard_set('a', 'n')
call paramcard_set('b', 123)
call paramcard_output('outfile', 'outfmt')

! Check the output.
call paramcard_parse('paramcard command: clear')
call paramcard_parse('paramcard command: load, test_output_2.txt')
call paramcard_get('n', n)
call check(error, n, 123)
if (allocated(error)) return

! Teardown.
call delete_file('test_output_1.txt')
call delete_file('test_output_2.txt')
end subroutine test_paramcard_output
end module test_paramcard

program tester
Expand Down
2 changes: 1 addition & 1 deletion test/test-paramcard_77.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ subroutine collect_test_paramcard_77(testsuite)
new_unittest('paramcard_set_s', test_paramcard_set_s), &
new_unittest('paramcard_set_i', test_paramcard_set_i), &
new_unittest('paramcard_set_r', test_paramcard_set_r), &
new_unittest('paramcard_set_s', test_paramcard_set_d) &
new_unittest('paramcard_set_d', test_paramcard_set_d) &
]
end subroutine collect_test_paramcard_77

Expand Down
2 changes: 1 addition & 1 deletion test/test-paramcard_77.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contains
#:for k in ['s', 'i', 'r']
new_unittest('paramcard_set_${k}$', test_paramcard_set_${k}$), &
#:endfor
new_unittest('paramcard_set_s', test_paramcard_set_d) &
new_unittest('paramcard_set_d', test_paramcard_set_d) &
]
end subroutine collect_test_paramcard_77

Expand Down

0 comments on commit bae2725

Please sign in to comment.