aot_path_toString Subroutine

public subroutine aot_path_toString(path, pathAsString)

Dumps the complete path into a string.

This routine transforms a given path into a special notation. Each element is added to the string, separated by a . char. If the resulting string is to long for the provided buffer /ref pathAsString, the buffer will stay empty to not have the caller proceed with incomplete results.

Arguments

Type IntentOptional Attributes Name
type(aot_path_type), intent(in) :: path

The path which information should be printed

character(len=*), intent(out) :: pathAsString

The path represented as string


Source Code

  subroutine aot_path_toString( path, pathAsString )
    !> The path which information should be printed
    type(aot_path_type), intent(in) :: path
    !> The path represented as string
    character(len=*), intent(out) :: pathAsString

    type(aot_path_node_type), pointer :: current
    integer :: pathLength
    integer :: stringLength
    character(len=10) :: posstr

    stringLength = len(pathAsString)
    pathLength = 0

    ! First we measure the size of the result
    if (associated(path%globalNode)) then
      current => path%globalNode
      do while(associated(current))
        if (associated(current,path%globalNode)) then
          ! Add the length of the first node
          pathLength = len_trim(adjustl(current%key))
        else
          if (trim(current%ID_kind) == 'key') then
            ! Add the length of a following node and the delimiter char
            pathLength = pathLength + len_trim(adjustl(current%key)) + 1
          else
            ! Length of the position number and 2 places for brackets.
            write(posstr,'(i0)') current%pos
            pathLength = pathLength + len_trim(posstr) + 2
          end if
        end if
        current => current%child
      end do
    end if

    ! If the result fits into the buffer, we create it
    if (pathLength <= stringLength .and. pathLength > 0) then
      current => path%globalNode
      do while(associated(current))
        if (associated(current,path%globalNode)) then
          pathAsString = trim(adjustl(current%key))
        else
          if (trim(current%ID_kind) == 'key') then
            pathAsString = trim(pathAsString) // '.' &
              &            // trim(adjustl(current%key))
          else
            write(posstr,'(i0)') current%pos
            pathAsString = trim(pathAsString) // '[' &
              &            // trim(posstr) // ']'
          end if
        end if
        current => current%child
      end do
    else
      ! Either the result is empty or too long, thus we clear the buffer
      pathAsString = ''
    end if

  end subroutine aot_path_toString