get_array Subroutine

private subroutine get_array(this, path, array_callback)

Arguments

Type IntentOptional AttributesName
type(fson_value), pointer:: this
character(len=*), optional :: path
public subroutine array_callback(element, index, count)
Arguments
Type IntentOptional AttributesName
type(fson_value), pointer:: element
integer :: index
integer :: count

Contents

Source Code


Source Code

  subroutine get_array(this, path, array_callback)
    use fson_value_m, only: type_array, fson_value_get, fson_value_count, &
      fson_value
    implicit none

    type(fson_value), pointer :: this, p, element
    character(len=*), optional :: path
    integer :: index, count

    ! ELEMENT CALLBACK  (PJK: Added example comments)
    interface
       subroutine array_callback(element, index, count)
         use fson_value_m, only: fson_value
         implicit none

         !  In the actual routine add a second 'use' line as follows:
         !use shared_data  !  contains declarations for the array(s) to be populated

         type(fson_value), pointer :: element
         integer :: index, count

         !  Example usage in the actual routine:
         !  call fson_get(element, "element_name", array_name(index))

       end subroutine array_callback
    end interface

    nullify(p)

    ! resolve the path to the value
    if (present(path)) then
       call get_by_path(this=this, path=path, p=p)
    else
       p => this
    end if

    if (.not.associated(p)) then
       print *, "Unable to resolve path: ", path
       call exit(1)
    end if

    if (p % value_type == TYPE_ARRAY) then
       count = fson_value_count(p)
       do index = 1, count
          element => fson_value_get(p, index)
          call array_callback(element, index, count)
       end do
    else
       print *, "Resolved value is not an array. ", path
       call exit(1)
    end if

  end subroutine get_array