Skip to content

Commit

Permalink
Fixes #192 ESMF_ConfigSetAttribute
Browse files Browse the repository at this point in the history
* fix inserting and deleting to config buffer
  • Loading branch information
danrosen25 committed Jan 11, 2024
1 parent 609c811 commit cdf51e6
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Infrastructure/Config/src/ESMF_Config.F90
Original file line number Diff line number Diff line change
Expand Up @@ -3685,7 +3685,6 @@ subroutine ESMF_ConfigSetString(config, value, &
else ! attribute found
! set config buffer for overwriting/inserting
i = config%cptr%value_begin
curVal = BLK // trim(curVal) // BLK // EOL ! like config%cptr%this_line
endif

! for appending, create new attribute string with label and value
Expand Down Expand Up @@ -3721,15 +3720,13 @@ subroutine ESMF_ConfigSetString(config, value, &
! check if we need more space to insert new characters;
! shift buffer down (linked-list redesign would be better!)
nchar = j-i+1
lenThisLine = len_trim(curVal) - 1
lenThisLine = index(config%cptr%buffer(i:config%cptr%nbuf), EOL)
if (lenThisLine .lt. 1) lenThisLine = config%cptr%nbuf - 1
if ( nchar .gt. lenThisLine) then

! check to ensure length of extended line doesn't exceed LSZ
do m = i, 1, -1
if (config%cptr%buffer(m:m) .eq. EOL) then
exit
endif
enddo
m = index(config%cptr%buffer(1:i), EOL, back=.true.)
if (m .lt. 1) m = 1
if (j-m+1 .gt. LSZ) then
write(logmsg, *) ", attribute label, value & EOL are ", j-m+1, &
" characters long, only ", LSZ, " characters allowed per line"
Expand All @@ -3747,7 +3744,7 @@ subroutine ESMF_ConfigSetString(config, value, &
endif

ninsert = nchar - lenThisLine
do k = config%cptr%nbuf, j, -1
do k = config%cptr%nbuf, i+lenThisLine, -1
config%cptr%buffer(k+ninsert:k+ninsert) = config%cptr%buffer(k:k)
enddo
config%cptr%nbuf = config%cptr%nbuf + ninsert
Expand All @@ -3757,14 +3754,14 @@ subroutine ESMF_ConfigSetString(config, value, &
elseif ( nchar .lt. lenThisLine ) then
ndelete = lenThisLine - nchar
do k = j+1, config%cptr%nbuf
config%cptr%buffer(k-ndelete:k-ndelete) = config%cptr%buffer(k:k)
config%cptr%buffer(k:k) = config%cptr%buffer(k+ndelete:k+ndelete)
enddo
config%cptr%nbuf = config%cptr%nbuf - ndelete
endif
endif

! write new attribute value into config
config%cptr%buffer(i:j) = newVal(1:len_trim(newVal))
config%cptr%buffer(i:j) = newVal(1:nchar)

! if appended, reset EOB marker and nbuf
if (i .eq. config%cptr%nbuf) then
Expand Down

0 comments on commit cdf51e6

Please sign in to comment.