subroutine ESMF_ConfigSetString(config, value, &
keywordEnforcer, label, rc)
! !ARGUMENTS:
type(ESMF_Config), intent(inout) :: config
character(*), intent(in) :: value
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
character(len=*), intent(in), optional :: label
integer, intent(out), optional :: rc
!
! !DESCRIPTION:
! Sets an integer {\tt value} in the {\tt config} object.
!
! The arguments are:
! \begin{description}
! \item [config]
! Already created {\tt ESMF\_Config} object.
! \item [value]
! String to set.
! \item [{[label]}]
! Identifying attribute label.
! \item [{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOPI -------------------------------------------------------------------
!
integer :: localrc
character(len=ESMF_MAXSTR) :: logmsg
character(len=NBUF_MAX) :: curVal, newVal
integer :: i, j, k, m, nchar, ninsert, ndelete, lenThisLine
! Initialize return code; assume routine not implemented
localrc = ESMF_RC_NOT_IMPL
if (present(rc)) rc = ESMF_RC_NOT_IMPL
!check variables
ESMF_INIT_CHECK_DEEP(ESMF_ConfigGetInit,config,rc)
! Set config buffer at desired attribute
if ( present (label) ) then
call ESMF_ConfigGetString( config, curVal, label=label, rc=localrc)
else
call ESMF_ConfigGetString( config, curVal, rc = localrc )
endif
if ( localrc /= ESMF_SUCCESS ) then
if ( localrc == ESMF_RC_NOT_FOUND ) then
! set config buffer at end for appending
i = config%cptr%nbuf
else
if ( present( rc ) ) then
rc = localrc
endif
return
endif
else ! attribute found
! set config buffer for overwriting/inserting
i = config%cptr%value_begin
endif
! for appending, create new attribute string with label and value
if ( i .eq. config%cptr%nbuf ) then
if ( present(label) ) then
write(newVal, *) trim(label), BLK, value
else
write(newVal, *) "__MISSING__:", BLK, value
endif
newVal = trim(adjustl(newVal)) // EOL
nchar = len_trim(newVal)
j = i + nchar
! check if enough space left in config buffer
if (j .ge. NBUF_MAX) then ! room for EOB if necessary
write(logmsg, *) ", attribute label & value require ", j-i+1, &
" characters (including EOL & EOB), only ", NBUF_MAX-i, &
" characters left in config buffer"
if (ESMF_LogFoundError(ESMC_RC_LONG_STR, msg=logmsg, &
ESMF_CONTEXT, rcToReturn=rc)) return
endif
endif
! overwrite, with possible insertion or deletion of extra characters
if (i .eq. config%cptr%value_begin) then
write(newVal, *) value
newVal = BLK // trim(adjustl(newVal)) // EOL
nchar = len_trim(newVal)
j = i + nchar - 1
! check if we need more space to insert new characters;
! shift buffer down (linked-list redesign would be better!)
lenThisLine = index(config%cptr%buffer(i:config%cptr%next_line),EOL)
if (lenThisLine .eq. 0) lenThisLine = config%cptr%nbuf - i
if ( nchar .gt. lenThisLine) then
ninsert = nchar - lenThisLine
! check if enough space left in config buffer to extend line
! leave room for EOB with .ge.
if (config%cptr%nbuf+ninsert .ge. NBUF_MAX) then
m = index(config%cptr%buffer(1:i), EOL, back=.true.)
if (m .lt. 1) m = 1
write(logmsg, *) ", attribute label & value require ", j-m+1, &
" characters (including EOL & EOB), only ", NBUF_MAX-i, &
" characters left in config buffer"
if (ESMF_LogFoundError(ESMC_RC_LONG_STR, msg=logmsg, &
ESMF_CONTEXT, rcToReturn=rc)) return
endif
config%cptr%nbuf = config%cptr%nbuf + ninsert
config%cptr%buffer(i+lenThisLine+ninsert:config%cptr%nbuf+ninsert) = &
config%cptr%buffer(i+lenThisLine:config%cptr%nbuf)
! or if we need less space and remove characters;
! shift buffer up
elseif ( nchar .lt. lenThisLine ) then
ndelete = lenThisLine - nchar
config%cptr%buffer(i+lenThisLine-ndelete:config%cptr%nbuf-ndelete) = &
config%cptr%buffer(i+lenThisLine:config%cptr%nbuf)
config%cptr%nbuf = config%cptr%nbuf - ndelete
endif
endif
! write new attribute value into config
config%cptr%buffer(i:j) = newVal(1:nchar)
config%cptr%next_line = j + 1
config%cptr%next_item = config%cptr%value_begin
! if appended, reset EOB marker and nbuf
if (i .eq. config%cptr%nbuf) then
config%cptr%buffer(j+1:j+1) = EOB
config%cptr%nbuf = j
endif
if( present( rc )) then
rc = ESMF_SUCCESS
endif
return
end subroutine ESMF_ConfigSetString