Skip to content

Cookbook ​

Short answers to "how do I ...?". Each recipe is a complete program with its real output; the reference has the details. The files they read are in docs/examples/files: heat.ini of the tutorial, rod.ini, settings.ini, colon.cfg.

Reading ​

Parse a string ​

f90
program parse_string
!< Cookbook: parse a string.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: ini
integer(I4P)   :: cells

call ini%load(source='[grid]'//new_line('a')//'cells = 64')
call ini%get(section_name='grid', option_name='cells', val=cells)
print '(A,I0)', 'cells = ', cells
endprogram parse_string
$ parse_string
cells = 64

Lines are separated by new_line('a'). The same file_ini can load again: the new sections replace the old ones.

Parse a file, and stop on errors ​

f90
program parse_file
!< Cookbook: parse a file, and stop on errors.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: ini
integer(I4P)   :: error

call ini%load(filename='heat.ini', error=error)
if (error /= 0) stop 'cannot load heat.ini'
print '(A,I0,A)', 'heat.ini: ', ini%sections_number(), ' sections'
call ini%load(filename='missing.ini', error=error)
if (error /= 0) print '(A)', 'missing.ini: cannot load it'
endprogram parse_file
$ parse_file
heat.ini: 6 sections
missing.ini: cannot load it

error is the I/O status of the read, not zero when the file cannot be read; compare it with 0.

Read an integer or a real of any kind ​

f90
program read_number
!< Cookbook: read an integer or a real of any kind.
use finer
use penf, only : I1P, I2P, I4P, I8P, R4P, R8P
implicit none
type(file_ini) :: ini
integer(I1P)   :: i1
integer(I8P)   :: i8
real(R4P)      :: r4
real(R8P)      :: r8

call ini%load(filename='heat.ini')
call ini%get(section_name='grid', option_name='cells', val=i1)
call ini%get(section_name='time', option_name='steps', val=i8)
call ini%get(section_name='time', option_name='dt', val=r4)
call ini%get(section_name='time', option_name='dt', val=r8)
print '(A,I0,A,I0)', 'cells (I1P) = ', i1, ', steps (I8P) = ', i8
print '(A,ES14.7,A,ES22.15)', 'dt (R4P) = ', r4, ', dt (R8P) = ', r8
endprogram read_number
$ read_number
cells (I1P) = 64, steps (I8P) = 2000
dt (R4P) =  9.9999997E-05, dt (R8P) =  1.000000000000000E-04

The kind of val decides the conversion; an integer that does not fit its kind is an error (ERR_OPTION_VALS), never a wrapped value. 1.0e-4 is not exact in R4P.

Read a logical ​

f90
program read_logical
!< Cookbook: read a logical.
use finer
use penf, only : I4P
implicit none
type(file_ini)  :: ini
logical         :: flag
integer(I4P)    :: error
character(len=1) :: name
integer         :: i

call ini%load(filename='settings.ini')
do i=1, 7
  name = achar(iachar('a') + i - 1)
  flag = .false.
  call ini%get(section_name='flags', option_name=name, val=flag, error=error)
  if (error == 0) then
    print '(A,L1)', name//' = ', flag
  else
    print '(A,I0)', name//': not a logical, error ', error
  endif
enddo
endprogram read_logical
$ read_logical
a = T
b = T
c = T
d = F
e = F
f: not a logical, error 2
g: not a logical, error 2

Logicals follow Fortran: a first letter T or F (any case), optionally after a point. yes and 1 are not logicals.

Read a list of numbers of unknown length ​

f90
program read_list
!< Cookbook: read a list of numbers of unknown length.
use finer
use penf, only : R8P
implicit none
type(file_ini)         :: ini
real(R8P), allocatable :: probes(:)

call ini%load(filename='heat.ini')
allocate(probes(ini%count_values(section_name='boundaries', option_name='probes')))
call ini%get(section_name='boundaries', option_name='probes', val=probes)
print '(I0,A,*(F6.2))', size(probes), ' probes:', probes
endprogram read_list
$ read_list
3 probes:  0.10  0.25  0.40

count_values first, then an array of that size. The list can span several lines.

Read a list with another delimiter ​

f90
program read_list_delimiter
!< Cookbook: read a list with another delimiter.
use finer
use penf, only : I4P
implicit none
type(file_ini)            :: ini
integer(I4P), allocatable :: nodes(:)
character(len=8)          :: labels(3)
integer                   :: i

call ini%load(filename='settings.ini')
allocate(nodes(ini%count_values(section_name='lists', option_name='nodes', delimiter=',')))
call ini%get(section_name='lists', option_name='nodes', val=nodes, delimiter=',')
print '(A,*(1X,I0))', 'nodes:', nodes
call ini%get(section_name='lists', option_name='labels', val=labels)
print '(A,*(1X,A))', 'labels:', (trim(labels(i)), i=1, size(labels))
endprogram read_list_delimiter
$ read_list_delimiter
nodes: 1 2 3 5 8
labels: inlet outlet wall

Pass the same delimiter to count_values and to get. Blanks around numbers are ignored. A character array receives one item per element.

Read complex numbers ​

f90
program read_complex
!< Cookbook: read complex numbers.
use finer
use penf, only : R8P
implicit none
type(file_ini)            :: ini
complex(R8P), allocatable :: modes(:)

call ini%load(filename='settings.ini')
allocate(modes(ini%count_values(section_name='lists', option_name='modes')))
call ini%get(section_name='lists', option_name='modes', val=modes)
print '(I0,A)', size(modes), ' modes:'
print '(2F6.2)', modes
endprogram read_complex
$ read_complex
3 modes:
  1.00  0.50
  2.00 -1.50
  0.00  1.00

Complex values are written (re,im); in a list, each value in parentheses is one item, whatever separates them.

Read a string of any length ​

f90
program read_string
!< Cookbook: read a string of any length.
use finer
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: title
character(len=8)              :: short

call ini%load(filename='heat.ini')
call ini%get_string(section_name='', option_name='title', val=title)
print '(A,I0,A)', 'title (', len(title), ' characters): '//title
call ini%get(section_name='', option_name='title', val=short)
print '(A)', 'into a character(len=8): "'//short//'"'
endprogram read_string
$ read_string
title (23 characters): cooling of a copper rod
into a character(len=8): "cooling "

get_string allocates val to the length of the value; get into a fixed-length string truncates or pads it.

Give a setting a default ​

f90
program default_value
!< Cookbook: give a setting a default.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini)                :: ini
real(R8P)                     :: cfl, weights(3)
integer(I4P)                  :: error
character(len=:), allocatable :: scheme

call ini%load(filename='heat.ini')
call ini%get(section_name='time', option_name='cfl', val=cfl, default=0.9_R8P, error=error)
print '(A,F4.2,A,I0)', 'cfl = ', cfl, ' (default), error ', error
call ini%get(section_name='time', option_name='weights', val=weights, default=[0.25_R8P, 0.5_R8P, 0.25_R8P])
print '(A,3F5.2)', 'weights =', weights
call ini%get_string(section_name='time', option_name='scheme', val=scheme, default='euler')
print '(A)', 'scheme = '//scheme
endprogram default_value
$ default_value
cfl = 0.90 (default), error 3
weights = 0.25 0.50 0.25
scheme = euler

default is used only when get fails, and error still says why. An array get takes an array default of the same size.

Quadruple precision reals ​

f90
program quad_precision
!< Cookbook: quadruple precision reals.
use finer
use penf, only : R16P
implicit none
type(file_ini) :: ini
real(R16P)     :: third, back

third = 1.0_R16P / 3.0_R16P
call ini%add(section_name='q', option_name='third', val=third)
call ini%print(unit=6)
call ini%get(section_name='q', option_name='third', val=back)
print '(A,L1)', 'reads back exactly: ', back == third
endprogram quad_precision
$ quad_precision
[q]
  third = 0.3333333333333333333333333333333333
reads back exactly: T

R16P is quadruple precision when PENF is built with PENF_R16P (FoBiS always, CMake when the compiler supports it, fpm never).

Exploring ​

Check that a section or an option exists, and find its section ​

f90
program check_exists
!< Cookbook: check that a section or an option exists, and find its section.
use finer
implicit none
type(file_ini)    :: ini
character(len=32) :: where

call ini%load(filename='heat.ini')
print '(A,L1)', 'section [time]: ', ini%has_section(section_name='time')
print '(A,L1)', 'section [restart]: ', ini%has_section(section_name='restart')
print '(A,L1)', 'option steps: ', ini%has_option(option_name='steps')
if (ini%has_option(option_name='every', section_name=where)) print '(A)', 'every is in ['//trim(where)//']'
print '(A,L1)', 'steps in [time]: ', ini%index(section_name='time', option_name='steps') > 0
endprogram check_exists
$ check_exists
section [time]: T
section [restart]: F
option steps: T
every is in [output]
steps in [time]: T

has_option searches every section; its optional section_name receives the first that holds the option. index checks one section.

Repeated options, the first one wins ​

f90
program repeated_options
!< Cookbook: repeated options, the first one wins.
use finer
use penf, only : I4P
implicit none
type(file_ini)                :: ini
integer(I4P)                  :: level
character(len=:), allocatable :: pair(:)

call ini%load(filename='settings.ini')
call ini%get(section_name='repeated', option_name='level', val=level)
print '(A,I0)', 'get: level = ', level
print '(A,I0,A,I0)', 'index: first ', ini%index(section_name='repeated', option_name='level'), &
                     ', last ', ini%index(section_name='repeated', option_name='level', back=.true.)
do while (ini%loop(section_name='repeated', option_pairs=pair))
  print '(A)', '  '//trim(pair(1))//' = '//trim(pair(2))
enddo
endprogram repeated_options
$ repeated_options
get: level = 1
index: first 1, last 3
  level = 1
  level = 2
  level = 3

An option written twice is kept twice: get reads the first, index(back=.true.) finds the last, loop returns all of them.

List the sections ​

f90
program list_sections
!< Cookbook: list the sections.
use finer
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: names(:)
integer                       :: s

call ini%load(filename='heat.ini')
call ini%get_sections_list(names)
do s=1, size(names)
  if (len_trim(names(s)) == 0) then
    print '(I0,A)', s, ': (the global section)'
  else
    print '(I0,A)', s, ': ['//trim(names(s))//']'
  endif
enddo
endprogram list_sections
$ list_sections
1: (the global section)
2: [grid]
3: [time]
4: [material]
5: [boundaries]
6: [output]

All the names have the length of the longest; the global section is the empty name.

Loop over the options of a section, or of the whole file ​

f90
program loop_options
!< Cookbook: loop over the options of a section, or of the whole file.
use finer
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: pair(:)
integer                       :: n

call ini%load(filename='heat.ini')
do while (ini%loop(section_name='time', option_pairs=pair))
  print '(A)', trim(pair(1))//' = '//trim(pair(2))
enddo
n = 0
do while (ini%loop(option_pairs=pair))
  n = n + 1
enddo
print '(A,I0,A)', 'the file has ', n, ' options'
endprogram loop_options
$ loop_options
dt = 1.0e-4
steps = 2000
adaptive = .true.
the file has 16 options

A loop keeps its position in the file_ini: let it run to its end before starting another over the same options.

Get every option name and value at once ​

f90
program get_items
!< Cookbook: get every option name and value at once.
use finer
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: items(:,:)
integer                       :: i

call ini%load(filename='rod.ini')
call ini%get_items(items)
do i=1, size(items, dim=1)
  print '(A)', trim(items(i,1))//' = '//trim(items(i,2))
enddo
endprogram get_items
$ get_items
cells = 64
length = 0.5
dt = 1.0e-4
steps = 2000

items(i, 1) is a name, items(i, 2) its value, for every option of every section in order.

Writing ​

Build a file in code and save it ​

f90
program build_file
!< Cookbook: build a file in code and save it.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: ini
integer(I4P)   :: iostat

call ini%add(section_name='grid', option_name='cells', val=128_I4P)
call ini%add(section_name='grid', option_name='length', val=1.0_R8P)
call ini%add(section_name='grid', option_name='periodic', val=.false.)
call ini%add(section_name='output', option_name='format', val='vtk')
call ini%save(filename='built.ini', iostat=iostat)
if (iostat /= 0) stop 'cannot save built.ini'
endprogram build_file
$ cat built.ini
[grid]
cells = 128
length = 1.0
periodic = F
[output]
format = vtk

add creates the sections as needed, in the order of the calls.

Change a value and save the file ​

f90
program update_value
!< Cookbook: change a value and save the file.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: ini

call ini%load(filename='rod.ini')
call ini%add(section_name='time', option_name='dt', val=5.0e-5_R8P)
call ini%add(section_name='time', option_name='steps', val=4000_I4P)
call ini%save(filename='rod-fine.ini')
endprogram update_value
$ cat rod-fine.ini
[grid]
cells = 64
length = 0.5
[time]
dt = 0.00005
steps = 4000

An add of an existing option updates it in place, keeping its position and its inline comment.

Write arrays and complex numbers ​

f90
program write_arrays
!< Cookbook: write arrays and complex numbers.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: ini

call ini%add(section_name='arrays', option_name='ids', val=[1_I4P, 2_I4P, 3_I4P])
call ini%add(section_name='arrays', option_name='weights', val=[0.25_R8P, 0.5_R8P, 0.25_R8P])
call ini%add(section_name='arrays', option_name='mask', val=[.true., .false., .true.])
call ini%add(section_name='arrays', option_name='names', val=['inlet ', 'outlet'])
call ini%add(section_name='arrays', option_name='z', val=(1.0_R8P, -2.0_R8P))
call ini%add(section_name='arrays', option_name='zs', val=[(1.0_R8P, 0.5_R8P), (0.0_R8P, 1.0_R8P)])
call ini%print(unit=6)
endprogram write_arrays
$ write_arrays
[arrays]
  ids = 1 2 3
  weights = 0.25 0.5 0.25
  mask = T F T
  names = inlet outlet
  z = (1.0,-2.0)
  zs = (1.0,0.5) (0.0,1.0)

Arrays are written with one blank between items; strings in an array are trimmed.

How reals are written ​

f90
program real_format
!< Cookbook: how reals are written.
use finer
use penf, only : R4P, R8P
implicit none
type(file_ini) :: ini
real(R8P)      :: back, x(6)
integer        :: i
character(len=1) :: name

x = [0.1_R8P, 1.0_R8P/3.0_R8P, 2.5e-5_R8P, 6.02214076e23_R8P, -1.0e-12_R8P, huge(1.0_R8P)]
do i=1, size(x)
  name = achar(iachar('a') + i - 1)
  call ini%add(section_name='reals', option_name=name, val=x(i))
enddo
call ini%add(section_name='reals', option_name='r4', val=0.1_R4P)
call ini%print(unit=6)
do i=1, size(x)
  name = achar(iachar('a') + i - 1)
  call ini%get(section_name='reals', option_name=name, val=back)
  if (back /= x(i)) print '(A)', name//' does not read back exactly'
enddo
print '(A)', 'every R8P value reads back exactly'
endprogram real_format
$ real_format
[reals]
  a = 0.1
  b = 0.3333333333333333
  c = 0.000025
  d = 6.02214076E+23
  e = -1.0E-12
  f = 1.7976931348623157E+308
  r4 = 0.1
every R8P value reads back exactly

The shortest text that reads back to the same value: plain decimals for exponents from -5 to 15, scientific notation beyond, huge included.

Options outside any section ​

f90
program global_section
!< Cookbook: options outside any section.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: ini
integer(I4P)   :: version

call ini%load(source='version = 2'//new_line('a')//'[grid]'//new_line('a')//'cells = 64')
call ini%get(section_name='', option_name='version', val=version)
print '(A,I0,A,A,A)', 'version = ', version, ', section 1 is "', ini%section(1), '"'
call ini%add(section_name='', option_name='author', val='the lab')
call ini%print(unit=6)
endprogram global_section
$ global_section
version = 2, section 1 is ""
  version = 2
  author = the lab
[grid]
  cells = 64

The global section is the empty name, always the first section, saved without a header.

Delete options and sections, or empty them ​

f90
program delete_options
!< Cookbook: delete options and sections, or empty them.
use finer
implicit none
type(file_ini) :: ini

call ini%load(filename='heat.ini')
call ini%del(section_name='time', option_name='adaptive')   ! one option
call ini%del(section_name='output')                         ! a section and its options
call ini%free_options(section_name='boundaries')            ! the options of a section, the section stays
call ini%free_options(section_name='grid', option_name='origin')
print '(A,I0,A,L1)', 'sections: ', ini%sections_number(), ', [boundaries] still there: ', &
                     ini%has_section(section_name='boundaries')
call ini%print(unit=6)
call ini%free_options()                                     ! every option, the sections stay
print '(A,I0,A,L1)', 'after free_options(): ', ini%sections_number(), ' sections, any option: ', &
                     ini%has_option(option_name='cells')
call ini%free()                                             ! everything
print '(A,I0)', 'after free(): ', ini%sections_number()
endprogram delete_options
$ delete_options
sections: 5, [boundaries] still there: T
  title = cooling of a copper rod
[grid]
  cells = 64
  length = 0.5
[time]
  dt = 1.0e-4
  steps = 2000
[material]
  name = copper
  conductivity = 401.0
  density = 8960.0
[boundaries]
after free_options(): 5 sections, any option: F
after free(): 0

del removes, free_options empties and keeps the sections, free resets everything. Deleting what does not exist does nothing.

Keep the inline comments when saving ​

f90
program keep_comments
!< Cookbook: keep the inline comments when saving.
use finer
implicit none
type(file_ini) :: ini

call ini%load(filename='heat.ini')
call ini%del(section_name='time')
call ini%del(section_name='boundaries')
call ini%del(section_name='output')
call ini%save(filename='plain.ini')
call ini%save(filename='commented.ini', retain_comments=.true.)
endprogram keep_comments
$ cat plain.ini commented.ini
title = cooling of a copper rod
[grid]
cells = 64
length = 0.5
origin = 0.0 0.0 0.0
[material]
name = copper
conductivity = 401.0
density = 8960.0
title = cooling of a copper rod
[grid]
cells = 64
length = 0.5 ; metres
origin = 0.0 0.0 0.0
[material]
name = copper
conductivity = 401.0 ; W/(m K)
density = 8960.0

Only inline comments (after ;) are kept, and only with retain_comments=.true.; comment lines are not written back.

f90
program print_prefix
!< Cookbook: print a file into a log, with a prefix.
use finer
implicit none
type(file_ini) :: ini
integer        :: iostat

call ini%load(filename='rod.ini')
print '(A)', 'run started with:'
call ini%print(unit=6, pref='[log] ', iostat=iostat)
endprogram print_prefix
$ print_prefix
run started with:
[log] [grid]
[log]   cells = 64
[log]   length = 0.5
[log] [time]
[log]   dt = 1.0e-4
[log]   steps = 2000

print takes any open unit; pref starts every line.

Copy a file_ini, then change the copy only ​

f90
program copy_file
!< Cookbook: copy a file_ini, then change the copy only.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: base, variant
integer(I4P)   :: a, b

call base%load(filename='rod.ini')
variant = base
call variant%add(section_name='grid', option_name='cells', val=256_I4P)
call base%get(section_name='grid', option_name='cells', val=a)
call variant%get(section_name='grid', option_name='cells', val=b)
print '(A,I0,A,I0)', 'base: ', a, ', variant: ', b
endprogram copy_file
$ copy_file
base: 64, variant: 256

Assignment makes an independent copy.

Remember the file name, save and load without passing it ​

f90
program reload_file
!< Cookbook: remember the file name, save and load without passing it.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: ini
integer(I4P)   :: steps, error

call ini%initialize(filename='counter.ini')
call ini%add(section_name='run', option_name='steps', val=10_I4P)
call ini%save()                                  ! into ini%filename
call ini%free()
ini%filename = 'counter.ini'
call ini%load(error=error)                       ! from ini%filename
call ini%get(section_name='run', option_name='steps', val=steps)
print '(A,A,A,I0,A,I0)', 'file ', ini%filename, ': steps = ', steps, ', error ', error
endprogram reload_file
$ reload_file
file counter.ini: steps = 10, error 0

initialize, load and save store the file name in filename, a public member; save() and load() without arguments use it.

Dialects ​

Read a file that uses another separator ​

f90
program custom_separator
!< Cookbook: read a file that uses another separator.
use finer
use penf, only : I4P
implicit none
type(file_ini) :: ini
integer(I4P)   :: port

call ini%load(filename='colon.cfg', separator=':')
call ini%get(section_name='server', option_name='port', val=port)
print '(A,I0)', 'port = ', port
call ini%print(unit=6)
endprogram custom_separator
$ custom_separator
port = 8080
[server]
  host = localhost
  port = 8080

The separator is used to read only: print and save always write name = value.

Values on several lines ​

f90
program multi_line
!< Cookbook: values on several lines.
use finer
use penf, only : R8P
implicit none
type(file_ini)                :: ini
real(R8P), allocatable        :: table(:)
character(len=:), allocatable :: text

call ini%load(source='[data]'//new_line('a')// &
                     'table = 1.0 2.0'//new_line('a')// &
                     '        3.0 4.0'//new_line('a')// &
                     '; a comment line does not break the value'//new_line('a')// &
                     '        5.0'//new_line('a')// &
                     'note  = first line'//new_line('a')// &
                     '        second line')
allocate(table(ini%count_values(section_name='data', option_name='table')))
call ini%get(section_name='data', option_name='table', val=table)
print '(I0,A,*(F4.1))', size(table), ' values:', table
call ini%get_string(section_name='data', option_name='note', val=text)
print '(A)', 'note = "'//text//'"'
endprogram multi_line
$ multi_line
5 values: 1.0 2.0 3.0 4.0 5.0
note = "first line second line"

A line without the separator continues the option above it, joined with one blank; comment lines in between are skipped.

Subsections as dotted names ​

f90
program subsections
!< Cookbook: subsections as dotted names.
use finer
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: names(:)
integer                       :: s

call ini%load(source='[solver]'//new_line('a')//'name = heat'//new_line('a')// &
                     '[solver.linear]'//new_line('a')//'tolerance = 1e-10'//new_line('a')// &
                     '[solver.linear.preconditioner]'//new_line('a')//'type = ilu')
call ini%get_sections_list(names)
do s=1, size(names)
  if (index(names(s), 'solver.linear') == 1) print '(A)', 'under solver.linear: '//trim(names(s))
enddo
endprogram subsections
$ subsections
under solver.linear: solver.linear
under solver.linear: solver.linear.preconditioner

Sections are flat: [solver.linear] is one name. A prefix test finds the subsections.

Errors ​

Check every setting and report all the bad ones ​

f90
program report_bad_values
!< Cookbook: check every setting and report all the bad ones.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: ini
integer(I4P)   :: error, cells, bad
real(R8P)      :: dt, length

call ini%load(source='[grid]'//new_line('a')//'cells = 64.5'//new_line('a')//'length = 1m'//new_line('a')// &
                     '[time]'//new_line('a')//'dt = 1e-4')
bad = 0
call ini%get(section_name='grid', option_name='cells', val=cells, error=error)
call report('grid', 'cells')
call ini%get(section_name='grid', option_name='length', val=length, error=error)
call report('grid', 'length')
call ini%get(section_name='time', option_name='dt', val=dt, error=error)
call report('time', 'dt')
call ini%get(section_name='time', option_name='steps', val=cells, error=error)
call report('time', 'steps')
print '(I0,A)', bad, ' bad settings'
contains
  subroutine report(section, option)
  character(*), intent(in) :: section, option
  if (error == err_option_vals) then
    bad = bad + 1 ; print '(A)', '['//section//'] '//option//': not a value of that type'
  elseif (error /= 0) then
    bad = bad + 1 ; print '(A)', '['//section//'] '//option//': missing'
  endif
  endsubroutine report
endprogram report_bad_values
$ report_bad_values
[grid] cells: not a value of that type
[grid] length: not a value of that type
[time] steps: missing
3 bad settings

ERR_OPTION_VALS means the value is there but is not of the type asked; any other non-zero error means it is missing.

Find the options nobody read ​

f90
program find_typos
!< Cookbook: after reading the settings, list the options nobody asked for (a typo, a removed key).
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini)                :: ini
character(len=:), allocatable :: unused(:,:)
integer(I4P)                  :: error, cells, u
real(R8P)                     :: cfl

call ini%load(source='[grid]'//new_line('a')//'cells = 64'//new_line('a')// &
                     '[time]'//new_line('a')//'CLF = 0.5'//new_line('a')//'cfl = 0.8')
call ini%get(section_name='grid', option_name='cells', val=cells, error=error)
call ini%get(section_name='time', option_name='cfl', val=cfl, error=error)
call ini%unused_options(items=unused)
do u=1, size(unused, dim=1)
  print '(A)', '['//trim(unused(u, 1))//'] '//trim(unused(u, 2))//': never read'
enddo
print '(I0,A)', size(unused, dim=1), ' unused options'
endprogram find_typos
$ find_typos
[time] CLF: never read
1 unused options

A misspelled key is not an error for get: the program asks for cfl, finds it, and never sees CLF. After the settings are read, unused_options lists every option that no get (or get_string) touched since the file was loaded.

Check that a file was saved ​

f90
program save_errors
!< Cookbook: check that a file was saved.
use finer
use penf, only : I4P
implicit none
type(file_ini)     :: ini
integer(I4P)       :: iostat
character(len=128) :: iomsg

call ini%add(section_name='run', option_name='steps', val=10_I4P)
call ini%save(filename='out/run.ini', iostat=iostat, iomsg=iomsg)
if (iostat /= 0) print '(A)', 'out/run.ini not saved (the directory does not exist)'
call ini%save(filename='run.ini', iostat=iostat)
if (iostat == 0) print '(A)', 'run.ini saved'
endprogram save_errors
$ save_errors
out/run.ini not saved (the directory does not exist)
run.ini saved

iostat is the status of the first operation that failed; iomsg holds the message of the compiler.