Parse file either from the self source data or from a source string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(file_ini), | intent(inout) | :: | self | File data. |
||
type(string), | intent(in) | :: | source | String source. |
||
integer(kind=I4P), | intent(out), | optional | :: | error | Error code. |
subroutine parse(self, source, error)
!< Parse file either from the self source data or from a source string.
class(file_ini), intent(inout) :: self !< File data.
type(string), intent(in) :: source !< String source.
integer(I4P), optional, intent(out) :: error !< Error code.
integer(I4P) :: errd !< Error code.
type(string), allocatable :: tokens(:) !< Options strings tokenized.
type(string) :: dummy !< Dummy string for parsing sections.
integer(I4P) :: Ns !< Counter.
integer(I4P) :: s !< Counter.
integer(I4P) :: ss !< Counter.
errd = err_source_missing
call source%split(tokens=tokens, sep=new_line('a'))
Ns = 0
s = 0
do while (s+1<=size(tokens, dim=1))
s = s + 1
if (scan(adjustl(tokens(s)), comments) == 1) cycle
if (index(trim(adjustl(tokens(s))), "[") == 1) then
Ns = Ns + 1
dummy = trim(adjustl(tokens(s)))//new_line('a')
ss = s
do while (ss+1<=size(tokens, dim=1))
ss = ss + 1
if (index(trim(adjustl(tokens(ss))), "[") == 1) then
! new section... go back
exit
else
! continuation of current section
dummy = trim(adjustl(dummy))//new_line('a')//trim(adjustl(tokens(ss)))
tokens(ss) = comments ! forcing skip this in the following scan
endif
enddo
tokens(s) = trim(adjustl(dummy))
endif
enddo
if (Ns>0) then
if (allocated(self%sections)) deallocate(self%sections) ; allocate(self%sections(1:Ns))
s = 0
ss = 0
do while (s+1<=size(tokens, dim=1))
s = s + 1
if (scan(adjustl(tokens(s)), comments) == 1) cycle
if (index(trim(adjustl(tokens(s))), "[") == 1) then
ss = ss + 1
call self%sections(ss)%parse(sep=self%opt_sep, source=tokens(s), error=errd)
endif
enddo
endif
self%Ns = Ns
if (present(error)) error = errd
endsubroutine parse