Skip to content

7. Other INI dialects ​

INI has no single standard. heat must also read legacy.cfg, written by an older tool with : as separator, other comment styles, dotted section names and an explicit empty header.

ini
# legacy.cfg: written by an older tool
! both comment styles are skipped
solver: heat
version: 3

[mesh.grid]
cells: 64
[mesh.refinement]
levels: 2    ; refine twice
[]
author: the lab
f90
program heat
!< Tutorial, chapter 7: other INI dialects.
use finer
use penf, only : I4P
implicit none
type(file_ini)                :: legacy, dos
integer(I4P)                  :: levels, cells, unit, s
character(len=:), allocatable :: author

call legacy%load(filename='legacy.cfg', separator=':')
call legacy%get(section_name='mesh.refinement', option_name='levels', val=levels)
call legacy%get_string(section_name='', option_name='author', val=author)
print '(A,I0,A)', 'levels = ', levels, ', author = '//author
do s=1, legacy%sections_number()
  print '(A)', 'section "'//legacy%section(s)//'"'
enddo
call legacy%print(unit=6, retain_comments=.true.)
open(newunit=unit, file='dos.ini', access='stream', form='unformatted', status='replace')
write(unit) '[grid]'//achar(13)//achar(10)//'cells = 64'//achar(13)//achar(10)
close(unit)
call dos%load(filename='dos.ini')
call dos%get(section_name='grid', option_name='cells', val=cells)
print '(A,I0)', 'cells from a CRLF file: ', cells
endprogram heat

Another separator ​

f90
call legacy%load(filename='legacy.cfg', separator=':')
call legacy%get(section_name='mesh.refinement', option_name='levels', val=levels)
call legacy%get_string(section_name='', option_name='author', val=author)
print '(A,I0,A)', 'levels = ', levels, ', author = '//author

load(separator=':') splits names and values at :. The separator is one character, and only load uses it: print and save always write name = value.

Sections ​

f90
do s=1, legacy%sections_number()
  print '(A)', 'section "'//legacy%section(s)//'"'
enddo
  • A header [] with nothing inside does not start a section: its options join the global section, after the options that come before the first header.
  • A section name can hold any character but ]: [mesh.grid] is a section named mesh.grid. FiNeR has no nesting, so dotted names are the usual way to write subsections (see the cookbook).

Comments ​

f90
call legacy%print(unit=6, retain_comments=.true.)

A line whose first non-blank character is !, ; or # is a comment and is skipped. After a value, ; starts an inline comment, which FiNeR keeps apart from the value: print and save write it back with retain_comments=.true..

Windows line endings ​

f90
open(newunit=unit, file='dos.ini', access='stream', form='unformatted', status='replace')
write(unit) '[grid]'//achar(13)//achar(10)//'cells = 64'//achar(13)//achar(10)
close(unit)
call dos%load(filename='dos.ini')
call dos%get(section_name='grid', option_name='cells', val=cells)
print '(A,I0)', 'cells from a CRLF file: ', cells

A file with CR+LF line endings reads as with LF endings, on every compiler: load drops every carriage return.

Running it ​

$ heat
levels = 2, author = the lab
section ""
section "mesh.grid"
section "mesh.refinement"
  solver = heat
  version = 3
  author = the lab
[mesh.grid]
  cells = 64
[mesh.refinement]
  levels = 2 ; refine twice
cells from a CRLF file: 64

What you learned

separator=, the three comment characters, inline comments, [], dotted names, CRLF files. Reference: The INI syntax.

Next: 8. Robust input.