3. Optional settings
Not every setting is in every case file. heat gives the missing ones a default, reads strings of any length, and asks which sections and options exist.
program heat
!< Tutorial, chapter 3: optional settings and defaults.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: case_file
integer(I4P) :: error
real(R8P) :: cfl
integer(I4P) :: every
character(len=:), allocatable :: fields, format, where
call case_file%load(filename='heat.ini')
call case_file%get(section_name='time', option_name='cfl', val=cfl, error=error, default=0.9_R8P)
print '(A,F4.2,A,I0)', 'cfl = ', cfl, ', error ', error
call case_file%get(section_name='output', option_name='every', val=every, default=100_I4P)
print '(A,I0)', 'every = ', every
call case_file%get_string(section_name='output', option_name='fields', val=fields)
print '(A)', 'fields = "'//fields//'"'
call case_file%get_string(section_name='output', option_name='compression', val=format, default='none')
print '(A)', 'compression = "'//format//'"'
print '(A,L1)', 'has [restart]: ', case_file%has_section(section_name='restart')
where = repeat(' ', 32)
if (case_file%has_option(option_name='density', section_name=where)) print '(A)', 'density is in ['//trim(where)//']'
endprogram heatDefaults
call case_file%get(section_name='time', option_name='cfl', val=cfl, error=error, default=0.9_R8P)
print '(A,F4.2,A,I0)', 'cfl = ', cfl, ', error ', error
call case_file%get(section_name='output', option_name='every', val=every, default=100_I4P)
print '(A,I0)', 'every = ', everyWhen get cannot read a value (the section or the option is missing, or the value cannot be converted) it sets error and leaves val unchanged, or sets it to default= if one is passed. default can be of any type assignable to val: an integer default for a real val is fine. An array get takes an array default of the same size.
Strings of any length
call case_file%get_string(section_name='output', option_name='fields', val=fields)
print '(A)', 'fields = "'//fields//'"'
call case_file%get_string(section_name='output', option_name='compression', val=format, default='none')
print '(A)', 'compression = "'//format//'"'get into a character(len=32) truncates or pads the value. get_string (re)allocates a deferred-length string instead, to the exact length of the value, so it needs no guess. It takes default= too.
What is there
print '(A,L1)', 'has [restart]: ', case_file%has_section(section_name='restart')
where = repeat(' ', 32)
if (case_file%has_option(option_name='density', section_name=where)) print '(A)', 'density is in ['//trim(where)//']'has_section and has_option answer .true. or .false.. has_option searches every section, and returns in its optional section_name the name of the first section holding the option (the argument must be long enough to receive it).
Running it
$ heat
cfl = 0.90, error 3
every = 500
fields = "temperature flux"
compression = "none"
has [restart]: F
density is in [material]error is 3 (ERR_OPTION): the option is missing. Chapter 8 has all the error codes.
What you learned
default= on get and get_string, get_string for strings of any length, has_section, has_option. Reference: Reading values, Exploring a file.
Next: 4. Walking the file.