Skip to content

4. Walking the file ​

A generic tool cannot know the names in advance: it walks the sections and options it finds.

f90
program heat
!< Tutorial, chapter 4: walking the file.
use finer
use penf, only : I4P
implicit none
type(file_ini)                :: case_file
character(len=:), allocatable :: names(:)
character(len=:), allocatable :: pair(:)
character(len=:), allocatable :: items(:,:)
integer(I4P)                  :: s, i

call case_file%load(filename='heat.ini')
print '(A,I0,A)', 'heat.ini has ', case_file%sections_number(), ' sections:'
do s=1, case_file%sections_number()
  print '(A,I0,A)', '  ', s, ': "'//case_file%section(s)//'"'
enddo
call case_file%get_sections_list(names)
print '(A,I0,A)', 'the list holds ', size(names), ' names'
print '(A)', 'options of [material]:'
do while (case_file%loop(section_name='material', option_pairs=pair))
  print '(A)', '  '//trim(pair(1))//' -> '//trim(pair(2))
enddo
i = 0
do while (case_file%loop(option_pairs=pair))
  i = i + 1
enddo
print '(A,I0,A)', 'the whole file has ', i, ' options'
call case_file%get_items(items)
print '(A,I0,A)', 'get_items: ', size(items, dim=1), ' pairs, the last is '// &
                  trim(items(size(items, dim=1), 1))//' = '//trim(items(size(items, dim=1), 2))
print '(A,I0)', 'index of [time]: ', case_file%index(section_name='time')
print '(A,I0)', 'index of steps in [time]: ', case_file%index(section_name='time', option_name='steps')
print '(A,I0)', 'values of fields: ', case_file%count_values(section_name='output', option_name='fields')
endprogram heat

The sections ​

f90
print '(A,I0,A)', 'heat.ini has ', case_file%sections_number(), ' sections:'
do s=1, case_file%sections_number()
  print '(A,I0,A)', '  ', s, ': "'//case_file%section(s)//'"'
enddo
call case_file%get_sections_list(names)
print '(A,I0,A)', 'the list holds ', size(names), ' names'

Sections are numbered from 1 in the order of the file, the global section first. section(i) is the name of the section i; get_sections_list gives all the names at once, as an array of strings of the same length.

Looping over options ​

f90
print '(A)', 'options of [material]:'
do while (case_file%loop(section_name='material', option_pairs=pair))
  print '(A)', '  '//trim(pair(1))//' -> '//trim(pair(2))
enddo

loop returns .true. and the next pair [name, value] until the options are over, then .false.. The state of the loop is kept in the file_ini itself, so a loop must run to its end before starting another over the same section.

f90
i = 0
do while (case_file%loop(option_pairs=pair))
  i = i + 1
enddo
print '(A,I0,A)', 'the whole file has ', i, ' options'

Without section_name, loop goes over every option of every section.

All pairs at once, and indexes ​

f90
call case_file%get_items(items)
print '(A,I0,A)', 'get_items: ', size(items, dim=1), ' pairs, the last is '// &
                  trim(items(size(items, dim=1), 1))//' = '//trim(items(size(items, dim=1), 2))
f90
print '(A,I0)', 'index of [time]: ', case_file%index(section_name='time')
print '(A,I0)', 'index of steps in [time]: ', case_file%index(section_name='time', option_name='steps')
print '(A,I0)', 'values of fields: ', case_file%count_values(section_name='output', option_name='fields')

get_items returns every pair in an array items(1:n, 1:2). index returns the position of a section, or of an option inside its section, and 0 when it is missing. count_values counts the values of an option.

Running it ​

$ heat
heat.ini has 6 sections:
  1: ""
  2: "grid"
  3: "time"
  4: "material"
  5: "boundaries"
  6: "output"
the list holds 6 names
options of [material]:
  name -> copper
  conductivity -> 401.0
  density -> 8960.0
the whole file has 16 options
get_items: 16 pairs, the last is every = 500
index of [time]: 3
index of steps in [time]: 2
values of fields: 2

What you learned

sections_number, section(i), get_sections_list, loop, get_items, index, count_values. Reference: Exploring a file.

Next: 5. Editing and saving.