Better treatment for attributes, less susceptible to run into trouble

with attributes containing single quotes into double quotes, or vice versa
This commit is contained in:
Paolo Giannozzi 2020-06-05 14:39:08 +00:00
parent 01eb8dc1c4
commit 436a7b6ef7
1 changed files with 32 additions and 40 deletions

View File

@ -118,48 +118,41 @@ CONTAINS
CHARACTER(LEN=*), INTENT(IN) :: attrname CHARACTER(LEN=*), INTENT(IN) :: attrname
CHARACTER(LEN=*), INTENT(OUT) :: attrval_c CHARACTER(LEN=*), INTENT(OUT) :: attrval_c
! !
CHARACTER(LEN=1) :: quote
INTEGER :: j0, j1 INTEGER :: j0, j1
LOGICAL :: found
! !
! search for attrname in attrlist ! search for attribute name in attrlist: attr1="val1" attr2="val2" ...
!
attrval_c = ''
if ( .not. allocated(attrlist) ) return
if ( len_trim(attrlist) < 1 ) return
! !
if ( .not. allocated(attrlist) ) then
attrval_c = ''
return
else if ( len_trim(attrlist) < 1 ) then
attrval_c = ''
return
end if
j0 = 1 j0 = 1
do while ( j0 < len_trim(attrlist) ) do while ( j0 < len_trim(attrlist) )
j1 = index ( attrlist(j0:), ',' ) ! locate = and first quote
if ( j1 == 0 ) then j1 = index ( attrlist(j0:), '=' )
! no more commas: check if found quote = attrlist(j0+j1:j0+j1)
if ( attrname == attrlist(j0:) ) exit ! next line: something is not right
! here if not found if ( quote /= '"' .and. quote /= "'" ) return
attrval_c = ' ' ! check if attribute found: need exact match
found = ( trim(attrname) == adjustl(trim(attrlist(j0:j0+j1-2))) )
! locate next quote
j0 = j0+j1+1
j1 = index ( attrlist(j0:), quote )
if ( found) then
if ( j1 == 1 ) then
! two quotes, one after the other ("")
attrval_c = ' '
else
! get value between two quotes
attrval_c = adjustl(trim(attrlist(j0:j0+j1-2)))
end if
return return
else
! check if found: need exact match between commas
if ( trim(attrname) == attrlist(j0:j0+j1-2) ) exit
end if end if
j0 = j0+j1 j0 = j0+j1
end do end do
! !
! next item between commas is the value
!
j0 = j0+j1
j1 = index ( attrlist(j0:), ',' )
if ( j1 == 0 ) then
! no more commas
attrval_c = attrlist(j0:)
else if ( j1 == 1 ) then
! two commas, one after the other (,,)
attrval_c = ' '
else
! take field between two commas
attrval_c = attrlist(j0:j0+j1-2)
end if
!
END SUBROUTINE get_c_attr END SUBROUTINE get_c_attr
! !
SUBROUTINE add_i_attr ( attrname, attrval_i ) SUBROUTINE add_i_attr ( attrname, attrval_i )
@ -743,29 +736,28 @@ CONTAINS
return return
! !
else if ( line(j:j) == '=' ) then else if ( line(j:j) == '=' ) then
! end of attribute located: save attribute ! end of attribute located: save attribute (with final =)
nattr=nattr+1 nattr=nattr+1
! print *, 'attr=',line(j0:j-1) ! print *, 'attr=',line(j0:j-1)
if ( nattr == 1 ) then if ( nattr == 1 ) then
attrlist = line(j0:j-1) attrlist = line(j0:j)
else else
attrlist = attrlist//','//line(j0:j-1) attrlist = attrlist//' '//line(j0:j)
end if end if
! continue searching for attribute value ! continue searching for attribute value
j = j+1 j = j+1
else if ( line(j:j) == '"' .or. line(j:j) =="'" ) then else if ( line(j:j) == '"' .or. line(j:j) =="'" ) then
! first occurrence of ' or " found, look for next ! first occurrence of ' or " found, look for next
quote = line(j:j) quote = line(j:j)
j=j+1 i = index(line(j+1:),quote)
i = index(line(j:),quote)
if ( i < 1 ) then if ( i < 1 ) then
! print *, 'Error: matching quote not found' ! print *, 'Error: matching quote not found'
go to 10 go to 10
else else
! save attribute value and continue scanning ! save attribute value (with quotes) and continue scanning
! print *, 'attrval=',line(j:j+i-2) ! print *, 'attrval=',line(j:j+i-2)
attrlist = attrlist//','//line(j:j+i-2) attrlist = attrlist//line(j:j+i)
j = j+i j = j+i+1
end if end if
else else
! continue scanning until end of attribute ! continue scanning until end of attribute