Skip to content

8. Robust input ​

A case file is written by hand, so heat must say what is wrong with it instead of running with a bad setting.

f90
program heat
!< Tutorial, chapter 8: robust input.
use finer
use penf, only : I1P, I4P, R8P
implicit none
type :: probe
  real(R8P) :: x
endtype probe
type(file_ini)    :: case_file, empty
integer(I4P)      :: error, iostat
integer(I4P)      :: cells
integer(I1P)      :: small
real(R8P)         :: origin(2)
type(probe)       :: p
character(len=64) :: iomsg

call case_file%load(filename='hat.ini', error=error)
print '(A,L1)', 'load of a missing file: error /= 0 is ', error /= 0
call case_file%load(filename='heat.ini')
cells = -1
call case_file%get(section_name='grids', option_name='cells', val=cells, error=error)
print '(A,I0,A,I0)', 'missing section:     error ', error, ', cells kept: ', cells
call case_file%get(section_name='material', option_name='name', val=cells, error=error)
print '(A,I0,A,I0)', 'not an integer:      error ', error, ', cells kept: ', cells
small = -1_I1P
call case_file%get(section_name='material', option_name='density', val=small, error=error)
print '(A,I0,A,I0)', 'out of range (I1P):  error ', error, ', small kept: ', small
origin = -1._R8P
call case_file%get(section_name='grid', option_name='origin', val=origin, error=error)
print '(A,I0,A,2F5.1)', 'array too small:     error ', error, ', origin kept:', origin
call case_file%get(section_name='grid', option_name='length', val=p, error=error)
print '(A,I0)', 'unsupported type:    error ', error
call case_file%get(section_name='material', option_name='name', val=cells, error=error)
if (error == err_option_vals) print '(A)', 'error is err_option_vals'
call empty%save(iostat=iostat, iomsg=iomsg)
print '(A,I0,A)', 'save without a file name: iostat ', iostat, ', "'//trim(iomsg)//'"'
call case_file%save(filename='no-such-dir/heat.ini', iostat=iostat)
print '(A,L1)', 'save into a missing directory: iostat /= 0 is ', iostat /= 0
endprogram heat

A file that cannot be read ​

f90
call case_file%load(filename='hat.ini', error=error)
print '(A,L1)', 'load of a missing file: error /= 0 is ', error /= 0

Values that cannot be read ​

f90
cells = -1
call case_file%get(section_name='grids', option_name='cells', val=cells, error=error)
print '(A,I0,A,I0)', 'missing section:     error ', error, ', cells kept: ', cells
call case_file%get(section_name='material', option_name='name', val=cells, error=error)
print '(A,I0,A,I0)', 'not an integer:      error ', error, ', cells kept: ', cells
small = -1_I1P
call case_file%get(section_name='material', option_name='density', val=small, error=error)
print '(A,I0,A,I0)', 'out of range (I1P):  error ', error, ', small kept: ', small
origin = -1._R8P
call case_file%get(section_name='grid', option_name='origin', val=origin, error=error)
print '(A,I0,A,2F5.1)', 'array too small:     error ', error, ', origin kept:', origin
call case_file%get(section_name='grid', option_name='length', val=p, error=error)
print '(A,I0)', 'unsupported type:    error ', error

A get that fails sets error and leaves val unchanged (or sets it to default=):

errorConstantWhen
3ERR_OPTIONthe section or the option is missing
2ERR_OPTION_VALSthe option has no value; the value is not of the type of val (copper into an integer, 3.5 into an integer); it is out of the range of the kind (8960 into an I1P); an array val is too small for the list; val is of a type FiNeR does not support

An array is checked whole before it is touched: if one value is bad, none is stored.

The constants come from finer, in lower case like any Fortran name:

f90
call case_file%get(section_name='material', option_name='name', val=cells, error=error)
if (error == err_option_vals) print '(A)', 'error is err_option_vals'

Files that cannot be saved ​

f90
call empty%save(iostat=iostat, iomsg=iomsg)
print '(A,I0,A)', 'save without a file name: iostat ', iostat, ', "'//trim(iomsg)//'"'
call case_file%save(filename='no-such-dir/heat.ini', iostat=iostat)
print '(A,L1)', 'save into a missing directory: iostat /= 0 is ', iostat /= 0

save without a file name (neither passed nor set by load) returns ERR_SOURCE_MISSING (7) with a message; a file that cannot be opened or written returns the I/O status of the compiler, which is not zero.

Running it ​

$ heat
load of a missing file: error /= 0 is T
missing section:     error 3, cells kept: -1
not an integer:      error 2, cells kept: -1
out of range (I1P):  error 2, small kept: -1
array too small:     error 2, origin kept: -1.0 -1.0
unsupported type:    error 2
error is err_option_vals
save without a file name: iostat 7, "file_ini%save: no file name"
save into a missing directory: iostat /= 0 is T

What you learned

Check error after load and get, iostat after save; a failed get never changes val. Reference: Errors.

This is the end of the tutorial. The cookbook has short recipes for everyday tasks.