type(ESMF_Config) function ESMF_ConfigCreateFromSection(config, &
openlabel, closelabel, keywordEnforcer, rc)
! !ARGUMENTS:
type(ESMF_Config) :: config
character(len=*), intent(in) :: openlabel, closelabel
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
integer,intent(out), optional :: rc
!
!
! !DESCRIPTION:
! Instantiates an {\tt ESMF\_Config} object from a section of an existing
! {\tt ESMF\_Config} object delimited by {\tt openlabel} and {\tt closelabel}.
! An error is returned if neither of the input labels is found in input
! {\tt config}.
!
! Note that a section is intended as the content of a given {\tt ESMF\_Config}
! object delimited by two distinct labels. Such content, as well as each of the
! surrounding labels, are still within the scope of the parent {\tt ESMF\_Config}
! object. Therefore, including in a section labels used outside that
! section should be done carefully to prevent parsing conflicts.
!
! The arguments are:
! \begin{description}
! \item[config]
! The input {\tt ESMF\_Config} object.
! \item[openlabel]
! Label marking the beginning of a section in {\tt config}.
! \item[closelabel]
! Label marking the end of a section in {\tt config}.
! \item [{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if a section is found
! and a new {\tt ESMF\_Config} object returned.
! \end{description}
!
!EOP -------------------------------------------------------------------
integer :: localrc
integer :: ptr, section_open, section_close
logical, parameter :: unique = .false.
! Initialize return code; assume routine not implemented
if (present(rc)) rc = ESMF_RC_NOT_IMPL
! Look for opening section label in whole input Config object
call ESMF_ConfigFindLabel(config, openlabel, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, &
msg="Opening section label not found", &
ESMF_CONTEXT, rcToReturn=rc)) return
section_open = config % cptr % value_begin
! Look closing section label after opening label
call ESMF_ConfigFindNextLabel(config, closelabel, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, &
msg="Closing section label not found", &
ESMF_CONTEXT, rcToReturn=rc)) return
section_close = config % cptr % value_begin - len(closelabel) - 1
if (section_close < section_open) then
call ESMF_LogSetError(ESMF_RC_NOT_FOUND, &
msg="Closing section label precedes opening section label", &
ESMF_CONTEXT, rcToReturn=rc)
return
end if
! Create and populate new Config object from parent object's section
ESMF_ConfigCreateFromSection = ESMF_ConfigCreate(rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, &
msg="Instantiating new config object", &
ESMF_CONTEXT, rcToReturn=rc)) return
ptr = section_close - section_open + 1
if (config % cptr % buffer(section_open:section_open) == EOL) then
ESMF_ConfigCreateFromSection % cptr % buffer(1:ptr) = &
config % cptr % buffer(section_open:section_close)
else
ptr = ptr + 1
ESMF_ConfigCreateFromSection % cptr % buffer(1:1) = EOL
ESMF_ConfigCreateFromSection % cptr % buffer(2:ptr) = &
config % cptr % buffer(section_open:section_close)
end if
ptr = ptr + 1
ESMF_ConfigCreateFromSection % cptr % nbuf = ptr
ESMF_ConfigCreateFromSection % cptr % buffer(ptr:ptr) = EOB
ESMF_ConfigCreateFromSection % cptr % this_line = ' '
ESMF_ConfigCreateFromSection % cptr % next_line = 1
ESMF_ConfigCreateFromSection % cptr % value_begin = 1
call ESMF_ConfigParseAttributes(ESMF_ConfigCreateFromSection, &
unique=unique, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
ESMF_ConfigCreateFromSection%cptr%hconfig_owner = .true.
ESMF_ConfigCreateFromSection%cptr%hconfig = ESMF_HConfigCreate(rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if (present(rc)) rc = ESMF_SUCCESS
ESMF_INIT_SET_CREATED(ESMF_ConfigCreateFromSection)
return
end function ESMF_ConfigCreateFromSection