search Subroutine

private elemental subroutine search(self, tag_name, source, tstart, tend)

Arguments

TypeIntentOptionalAttributesName
class(xml_tag), intent(inout) :: self
character, intent(in) :: tag_name
character, intent(in) :: source
integer(kind=I4P), intent(out), optional :: tstart
integer(kind=I4P), intent(out), optional :: tend

Contents

Source Code


Source Code

  elemental subroutine search(self, tag_name, source, tstart, tend)
  !< Search tag named *tag_name* into a string and, in case it is found, store into self.
  !<
  !< @note If *tag_name* is not found, self is returned empty.
  class(xml_tag),         intent(inout) :: self     !< XML tag.
  character(*),           intent(in)    :: tag_name !< Searched tag name.
  character(*),           intent(in)    :: source   !< String containing the input.
  integer(I4P), optional, intent(out)   :: tstart   !< Starting index of tag inside the source.
  integer(I4P), optional, intent(out)   :: tend     !< Ending index of tag inside the source.
  type(xml_tag)                         :: tag      !< Dummy XML tag.
  integer(I4P)                          :: tstart_  !< Starting index of tag inside the source, local variable.
  integer(I4P)                          :: tend_    !< Ending index of tag inside the source, local variable.
  logical                               :: found    !< Flag for inquiring search result.
  integer(I4P)                          :: tstart_c !< Starting index of tag inside the current slice of source.
  integer(I4P)                          :: tend_c   !< Starting index of tag inside the current slice of source.
  integer(I4P)                          :: i

  call self%free
  self%tag_name = tag_name
  tstart_ = 1
  tend_   = 0
  found = .false.
  tstart_c = 0
  tend_c = 0
  Tag_Search: do
    call tag%parse(source=source(tend_ + 1:), tstart=tstart_c, tend=tend_c)
    tstart_ = tstart_ + tend_
    tend_ = tend_ + tend_c
    if (tstart_c==0.and.tend_c==0) then
      exit Tag_Search ! no tag found
    else
      if (tag%tag_name%is_allocated()) then
        if (tag%tag_name==self%tag_name) then
          found = .true.
        endif
      endif
    endif
    if (found) exit Tag_Search
  enddo Tag_Search
  if (found) then
    self = tag
  else
    call self%free
  endif
  if (present(tstart)) tstart = tstart_
  if (present(tend  )) tend   = tend_
  endsubroutine search