Autotest the library functionalities.
subroutine file_ini_autotest()
!< Autotest the library functionalities.
type(file_ini) :: fini !< INI File.
character(len=:), allocatable :: source !< Testing string.
character(len=:), allocatable :: string !< String option.
real(R4P), allocatable :: array(:) !< Array option.
integer(I4P) :: error !< Error code.
character(len=:), allocatable :: items(:,:) !< List of all options name/value pairs.
character(len=:), allocatable :: item(:) !< Option name/value couple.
character(len=:), allocatable :: list(:) !< Sections names list.
integer(I4P) :: i !< Counter.
integer(I4P) :: s !< Counter.
source='[section-1]'//new_line('A')// &
'option-1 = one'//new_line('A')//&
'option-2 = 2.'//new_line('A')// &
' 3. ; this is an inline comment'//new_line('A')// &
'option-3 = bar ; this is an inline comment'//new_line('A')//&
'[section-2]'//new_line('A')// &
'option-1 = foo'
print "(A)", ''
print "(A)", "Testing parsing procedures"
print "(A)", ''
print "(A)", "Source to be parsed:"
print "(A)", source
call fini%load(source=source)
print "(A)", ''
print "(A)", "Result of parsing:"
string = ' '
call fini%get(section_name='section-1', option_name='option-1', val=string, error=error)
if (error==0) print "(A,A)", ' option-1 of section-1 has values: ', string
allocate(array(1:fini%count_values(section_name='section-1', option_name='option-2')))
call fini%get(section_name='section-1', option_name='option-2', val=array, error=error)
if (error==0) print "(A,3(F4.1,1X))", ' option-2 of section-1 has values: ', array
call fini%get(section_name='section-1', option_name='option-3', val=string, error=error)
if (error==0) print "(A,A)", ' option-3 of section-1 has values: ', string
call fini%get(section_name='section-2',option_name='option-1', val=string, error=error)
if (error==0) print "(A,A)", ' option-1 of section-2 has values: ', string
print "(A)", ''
print "(A)", "Parsed data will be saved as (having retained inline comments that are trimmed out by default):"
call fini%print(pref=' ', unit=stdout, retain_comments=.true.)
call fini%save(filename='foo.ini', retain_comments=.true.)
call fini%free
print "(A)", ''
print "(A)", "Testing generating procedures"
call fini%add(section_name='sec-foo')
call fini%add(section_name='sec-foo', option_name='bar', val=-32.1_R8P)
call fini%add(section_name='sec-foo', option_name='baz', val=' hello FiNeR! ')
call fini%add(section_name='sec-foo', option_name='array', val=[1, 2, 3, 4])
call fini%add(section_name='sec-bar')
call fini%add(section_name='sec-bar', option_name='bools', val=[.true.,.false.,.false.])
call fini%add(section_name='sec-bartolomeo')
call fini%add(section_name='sec-bartolomeo', option_name='help', val='I am Bartolomeo')
print "(A)", "The autogenerated INI file will be saved as:"
call fini%print(pref=' ', unit=stdout)
print "(A)", ''
print "(A)", "Testing removing option baz"
call fini%del(section_name='sec-foo', option_name='baz')
call fini%print(pref=' ', unit=stdout)
print "(A)", ''
print "(A)", "Testing removing section sec-bar"
call fini%del(section_name='sec-bar')
call fini%print(pref=' ', unit=stdout)
print "(A)", ''
print "(A)", "Testing introspective methods"
print "(A,L1)", "Is there option bar? ", fini%has_option(option_name='bar')
print "(A,L1)", "Is there option baz? ", fini%has_option(option_name='baz')
print "(A,L1)", "Is there section sec-bar? ", fini%has_section(section_name='sec-bar')
print "(A,L1)", "Is there section sec-foo? ", fini%has_section(section_name='sec-foo')
print "(A)", ''
print "(A)", "What are all options name/values pairs? Can I have a list? Yes, you can:"
call fini%get_items(items=items)
do i=1, size(items, dim=1)
print "(A)", trim(items(i, 1))//' = '//trim(items(i, 2))
enddo
print "(A)", ''
print "(A)", "Testing loop method over options of a section:"
do s=1, fini%Ns
print "(A)", fini%section(s)
do while(fini%loop(section_name=fini%section(s), option_pairs=item))
print "(A)", ' '//trim(item(1))//' = '//trim(item(2))
enddo
enddo
print "(A)", "Testing sections names list inquire:"
call fini%get_sections_list(list)
do s=1, fini%Ns
print "(A)", 'Sec. '//trim(str(s, .true.))//': '//trim(list(s))
enddo
print "(A)", ''
print "(A)", "Testing loop method over all options:"
do while(fini%loop(option_pairs=item))
print "(A)", ' '//trim(item(1))//' = '//trim(item(2))
enddo
print "(A)", ''
print "(A)", "Testing custom separator of option name/value:, use ':' instead of '='"
source='[section-1]'//new_line('A')// &
'option-1 : one'//new_line('A')//&
'option-2 : 2.'//new_line('A')// &
' 3.'//new_line('A')// &
'option-3 : bar'//new_line('A')//&
'[section-2]'//new_line('A')// &
'option-1 : foo'
print "(A)", ''
print "(A)", "Source to be parsed:"
print "(A)", source
call fini%free
call fini%load(separator=':', source=source)
print "(A)", ''
print "(A)", "Result of parsing:"
call fini%print(pref=' ', unit=stdout)
! remove "foo.ini"
open(newunit=i, file='foo.ini') ; close(unit=i, status='DELETE')
endsubroutine file_ini_autotest