Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ParseVar to parse file paths containing spaces. #2558

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/aerodyn/src/AeroDyn_Driver_Subs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ subroutine Dvr_ReadInputFile(fileName, dvr, errStat, errMsg )
call ParseVar(FileInfo_In, CurLine, "analysisType", dvr%analysisType, errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "tMax" , dvr%tMax , errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "dt" , dvr%dt , errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "AeroFile" , dvr%AD_InputFile, errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "AeroFile" , dvr%AD_InputFile, errStat2, errMsg2, unEc, IsPath=.true.); if (Failed()) return

! --- Environmental conditions
call ParseCom(FileInfo_In, CurLine, Line, errStat2, errMsg2, unEc); if (Failed()) return
Expand All @@ -973,7 +973,7 @@ subroutine Dvr_ReadInputFile(fileName, dvr, errStat, errMsg )
! --- Inflow data
call ParseCom(FileInfo_In, CurLine, Line, errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "compInflow", dvr%IW_InitInp%compInflow , errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "InflowFile", dvr%IW_InitInp%InputFile, errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "InflowFile", dvr%IW_InitInp%InputFile, errStat2, errMsg2, unEc, IsPath=.true.); if (Failed()) return
if (dvr%IW_InitInp%compInflow==0) then
call ParseVar(FileInfo_In, CurLine, "HWindSpeed", dvr%IW_InitInp%HWindSpeed , errStat2, errMsg2, unEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "RefHt" , dvr%IW_InitInp%RefHt , errStat2, errMsg2, unEc); if (Failed()) return
Expand Down
6 changes: 3 additions & 3 deletions modules/aerodyn/src/AeroDyn_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ SUBROUTINE ParsePrimaryFileInfo( PriPath, InitInp, InputFile, RootName, NumBlade
call ParseVar( FileInfo_In, CurLine, "CompAA", InputFileData%CompAA, ErrStat2, ErrMsg2, UnEc )
if (Failed()) return
! AA_InputFile - Aeroacoustics input file
call ParseVar( FileInfo_In, CurLine, "AA_InputFile", InputFileData%AA_InputFile, ErrStat2, ErrMsg2, UnEc )
call ParseVar( FileInfo_In, CurLine, "AA_InputFile", InputFileData%AA_InputFile, ErrStat2, ErrMsg2, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%AA_InputFile ) ) InputFileData%AA_InputFile = TRIM(PriPath)//TRIM(InputFileData%AA_InputFile)

Expand Down Expand Up @@ -802,7 +802,7 @@ SUBROUTINE ParsePrimaryFileInfo( PriPath, InitInp, InputFile, RootName, NumBlade
if ( InputFileData%Echo ) WRITE(UnEc, '(A)') FileInfo_In%Lines(CurLine) ! Write section break to echo
CurLine = CurLine + 1
! OLAFInputFileName - Input file for OLAF [used only when WakeMod=3]
call ParseVar( FileInfo_In, CurLine, "OLAFInputFileName", InputFileData%FVWFileName, ErrStat2, ErrMsg2, UnEc )
call ParseVar( FileInfo_In, CurLine, "OLAFInputFileName", InputFileData%FVWFileName, ErrStat2, ErrMsg2, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%FVWFileName ) ) InputFileData%FVWFileName = TRIM(PriPath)//TRIM(InputFileData%FVWFileName)

Expand Down Expand Up @@ -906,7 +906,7 @@ SUBROUTINE ParsePrimaryFileInfo( PriPath, InitInp, InputFile, RootName, NumBlade
! NOTE: being nice with legacy input file. Uncomment in next release
call ParseVar(FileInfo_In, CurLine, "TFinAero", InputFileData%rotors(iR)%TFinAero, ErrStat2, ErrMsg2, UnEc);
if (ErrStat2==ErrID_None) then
call ParseVar(FileInfo_In, CurLine, "TFinFile", InputFileData%rotors(iR)%TFinFile, ErrStat2, ErrMsg2, UnEc); if (Failed()) return
call ParseVar(FileInfo_In, CurLine, "TFinFile", InputFileData%rotors(iR)%TFinFile, ErrStat2, ErrMsg2, UnEc, IsPath=.true.); if (Failed()) return
InputFileData%rotors(iR)%TFinFile = trim(PriPath) // trim(InputFileData%rotors(iR)%TFinFile)
else
call LegacyWarning('Tail Fin section (TFinAero, TFinFile) is missing from input file.')
Expand Down
2 changes: 1 addition & 1 deletion modules/aerodyn/src/AirfoilInfo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ SUBROUTINE ReadAFfile ( InitInp, NumCoefsIn, p, ErrStat, ErrMsg, UnEc )
ENDIF

! Reading Boundary layer file for aeroacoustics
CALL ParseVar ( FileInfo, CurLine, 'BL_file' , p%BL_file , ErrStat2, ErrMsg2, UnEc )
CALL ParseVar ( FileInfo, CurLine, 'BL_file' , p%BL_file , ErrStat2, ErrMsg2, UnEc, IsPath=.true. )
IF (ErrStat2 >= AbortErrLev) p%BL_file = "NOT_SET_IN_AIRFOIL_FILE"
!CALL SetErrStat( ErrStat2, ErrMsg2, ErrStat, ErrMsg, RoutineName )
IF ( PathIsRelative( p%BL_file ) ) p%BL_file=trim(PriPath)//trim(p%BL_file)
Expand Down
2 changes: 1 addition & 1 deletion modules/hydrodyn/src/HydroDyn_Input.f90
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ SUBROUTINE HydroDyn_ParseInput( InputFileName, OutRootName, defWtrDens, defWtrDp
if (Failed()) return;

! WvKinFile
call ParseVar( FileInfo_In, CurLine, 'WvKinFile', InputFileData%Waves%WvKinFile, ErrStat2, ErrMsg2, UnEc )
call ParseVar( FileInfo_In, CurLine, 'WvKinFile', InputFileData%Waves%WvKinFile, ErrStat2, ErrMsg2, UnEc, IsPath=.true. )
if (Failed()) return;

! NWaveElev
Expand Down
12 changes: 6 additions & 6 deletions modules/inflowwind/src/InflowWind_Subs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ SUBROUTINE InflowWind_ParseInputFileInfo( InputFileData, InFileInfo, PriPath, In
!-------------------------------------------------------------------------------------------------

CurLine = CurLine + 1 ! Skip section break
CALL ParseVar( InFileInfo, CurLine, "FileName_Uni", InputFileData%Uniform_FileName, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FileName_Uni", InputFileData%Uniform_FileName, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%Uniform_FileName ) ) InputFileData%Uniform_FileName = TRIM(PriPath)//TRIM(InputFileData%Uniform_FileName)
IF ( FixedWindFileRootName ) THEN ! .TRUE. when FAST.Farm uses multiple instances of InflowWind for ambient wind data
Expand All @@ -339,7 +339,7 @@ SUBROUTINE InflowWind_ParseInputFileInfo( InputFileData, InFileInfo, PriPath, In
!-------------------------------------------------------------------------------------------------

CurLine = CurLine + 1 ! Skip section break
CALL ParseVar( InFileInfo, CurLine, "FileName_BTS", InputFileData%TSFF_FileName, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FileName_BTS", InputFileData%TSFF_FileName, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%TSFF_FileName ) ) InputFileData%TSFF_FileName = TRIM(PriPath)//TRIM(InputFileData%TSFF_FileName)
IF ( FixedWindFileRootName ) THEN ! .TRUE. when FAST.Farm uses multiple instances of InflowWind for ambient wind data
Expand All @@ -355,7 +355,7 @@ SUBROUTINE InflowWind_ParseInputFileInfo( InputFileData, InFileInfo, PriPath, In
!-------------------------------------------------------------------------------------------------

CurLine = CurLine + 1 ! Skip section break
CALL ParseVar( InFileInfo, CurLine, "FilenameRoot", InputFileData%BladedFF_FileName, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FilenameRoot", InputFileData%BladedFF_FileName, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%BladedFF_FileName ) ) InputFileData%BladedFF_FileName = TRIM(PriPath)//TRIM(InputFileData%BladedFF_FileName)

Expand Down Expand Up @@ -383,15 +383,15 @@ SUBROUTINE InflowWind_ParseInputFileInfo( InputFileData, InFileInfo, PriPath, In
!-------------------------------------------------------------------------------------------------

CurLine = CurLine + 1 ! Skip section break
CALL ParseVar( InFileInfo, CurLine, "FileName_u", InputFileData%HAWC_FileName_u, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FileName_u", InputFileData%HAWC_FileName_u, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%HAWC_FileName_u ) ) InputFileData%HAWC_FileName_u = TRIM(PriPath)//TRIM(InputFileData%HAWC_FileName_u)

CALL ParseVar( InFileInfo, CurLine, "FileName_v", InputFileData%HAWC_FileName_v, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FileName_v", InputFileData%HAWC_FileName_v, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%HAWC_FileName_v ) ) InputFileData%HAWC_FileName_v = TRIM(PriPath)//TRIM(InputFileData%HAWC_FileName_v)

CALL ParseVar( InFileInfo, CurLine, "FileName_w", InputFileData%HAWC_FileName_w, TmpErrStat, TmpErrMsg, UnEc )
CALL ParseVar( InFileInfo, CurLine, "FileName_w", InputFileData%HAWC_FileName_w, TmpErrStat, TmpErrMsg, UnEc, IsPath=.true. )
if (Failed()) return
IF ( PathIsRelative( InputFileData%HAWC_FileName_w ) ) InputFileData%HAWC_FileName_w = TRIM(PriPath)//TRIM(InputFileData%HAWC_FileName_w)

Expand Down
148 changes: 96 additions & 52 deletions modules/nwtc-library/src/NWTC_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MODULE NWTC_IO
CHARACTER(99) :: ProgVer = ' ' !< The version (including date) of the calling program. DO NOT USE THIS IN NEW PROGRAMS
CHARACTER(1), PARAMETER :: Tab = CHAR( 9 ) !< The tab character.
CHARACTER(*), PARAMETER :: CommChars = '!#%' !< Comment characters that mark the end of useful input
INTEGER(IntKi), PARAMETER :: NWTC_SizeOfNumWord = 200 !< maximum length of the words containing numeric input (for ParseVar routines)
INTEGER(IntKi), PARAMETER :: NWTC_SizeOfNumWord = 256 !< maximum length of the words containing numeric input (for ParseVar routines)


! Parameters for writing to echo files (in this module only)
Expand Down Expand Up @@ -2061,7 +2061,7 @@ END SUBROUTINE GetTokens
!! It uses spaces, tabs, commas, semicolons, single quotes, and double quotes ("whitespace")
!! as word separators. If there aren't NumWords in the line, the remaining array elements will remain empty.
!! Use CountWords (nwtc_io::countwords) to count the number of words in a line.
SUBROUTINE GetWords ( Line, Words, NumWords, NumFound )
SUBROUTINE GetWords ( Line, Words, NumWords, NumFound, IgnoreQuotes )

! Argument declarations.

Expand All @@ -2070,68 +2070,106 @@ SUBROUTINE GetWords ( Line, Words, NumWords, NumFound )
CHARACTER(*), INTENT(IN) :: Line !< The string to search.
CHARACTER(*), INTENT(OUT) :: Words(NumWords) !< The array of found words.
INTEGER, OPTIONAL, INTENT(OUT) :: NumFound !< The number of words found
LOGICAL, OPTIONAL, INTENT(OUT) :: IgnoreQuotes !< Flag to ignore quotes (process as whitespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be INTENT(IN)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should! Good catch.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed directly on rc-3.5.5 with a62d877


! Local declarations.

INTEGER :: Ch ! Character position within the string.
INTEGER :: IW ! Word index.
INTEGER :: NextWhite ! The location of the next whitespace in the string.
INTEGER :: iWord ! Word index.
INTEGER :: i ! Character index in line.
INTEGER :: iChar ! Character index in word.
CHARACTER(len=1) :: Char ! Current character
LOGICAL :: InQuotes ! Flag indicating text is within quotes
LOGICAL :: IgnoreQuotesLoc ! Local flag to ignore quotes

! If no text on line, return
if (len_trim(Line) == 0) return

! If ignore quotes is present, set local flag, otherwise true
if (present(IgnoreQuotes)) then
IgnoreQuotesLoc = IgnoreQuotes
else
IgnoreQuotesLoc = .true.
end if

! Let's prefill the array with blanks.
! Let's prefill the array with blanks.
do iWord = 1, NumWords
Words(iWord) = ' '
end do

DO IW=1,NumWords
Words(IW) = ' '
END DO ! IW
! Initialize number of words found to zero if present
if (present(NumFound)) NumFound = 0

IW = 0
! If no text on line, return
if (len_trim(Line) == 0) return

! Initialize word index to first word
iWord = 1

! Let's make sure we have text on this line.

IF ( LEN_TRIM( Line ) > 0 ) THEN

! Parse words separated by any combination of spaces, tabs, commas,
! semicolons, single quotes, and double quotes ("whitespace").

Ch = 0

DO

NextWhite = SCAN( Line(Ch+1:) , ' ,;''"'//Tab )

IF ( NextWhite > 1 ) THEN

IW = IW + 1
Words(IW) = Line(Ch+1:Ch+NextWhite-1)
if (NextWhite > len(words(iw)) ) then
call ProgWarn('Error reading field from file. There are too many characters in the input file to store in the field. Value may be truncated.')
end if

IF ( IW == NumWords ) EXIT

Ch = Ch + NextWhite

ELSE IF ( NextWhite == 1 ) THEN
! Initialize index within word
iChar = 0

! Initialize in quotes to false
InQuotes = .false.

! Loop through characters in line
do i = 1, len_trim(line)

! Get current character
Char = Line(i:i)

! Select based on character
select case (Char)
case ('"', "'") ! Double quotes, single quotes
if (IgnoreQuotesLoc .or. InQuotes) then
InQuotes = .false.
if (iChar > 0) then
! If requested number of words found, exit
if (iWord == NumWords) exit
iWord = iWord + 1
iChar = 0
end if
else
InQuotes = .true.
end if
cycle

case (' ', Tab) ! Whitespace
! If between quotes, keep whitespace; otherwise separate words
if (.not. InQuotes) then
if (iChar > 0) then
! If requested number of words found, exit
if (iWord == NumWords) exit
iWord = iWord + 1
iChar = 0
end if
cycle
end if

Ch = Ch + 1
case (',', ';') ! Comma, semicolon
! Always separate words on these characters
if (iChar > 0) then
! If requested number of words found, exit
if (iWord == NumWords) exit
iWord = iWord + 1
iChar = 0
end if
cycle
end select

CYCLE
! Increment character index
iChar = iChar + 1

ELSE

EXIT
! If index is larger than length of word, continue
if (iChar > len(words(iWord))) then
call ProgWarn('Error reading field from file. There are too many characters in the input file to store in the field. Value may be truncated. '//Line)
cycle
end if

END IF
! Add character to word
Words(iWord)(iChar:iChar) = Char

END DO

END IF
end do

IF (PRESENT(NumFound)) NumFound = IW
if (present(NumFound)) NumFound = iWord

RETURN
END SUBROUTINE GetWords
!=======================================================================
!> This routine converts an ASCII array of integers into an equivalent string
Expand Down Expand Up @@ -2884,7 +2922,7 @@ END SUBROUTINE ParseCom
!!
!! WARNING: This routine assumes the "words" containing the variable name and value are <= 20 characters. \n
!! Use ParseVar (nwtc_io::parsevar) instead of directly calling a specific routine in the generic interface.
SUBROUTINE ParseChVar ( FileInfo, LineNum, ExpVarName, Var, ErrStat, ErrMsg, UnEc )
SUBROUTINE ParseChVar ( FileInfo, LineNum, ExpVarName, Var, ErrStat, ErrMsg, UnEc, IsPath )

! Arguments declarations.

Expand All @@ -2893,6 +2931,7 @@ SUBROUTINE ParseChVar ( FileInfo, LineNum, ExpVarName, Var, ErrStat, ErrMsg, UnE
INTEGER(IntKi), INTENT(INOUT) :: LineNum !< The number of the line to parse.

INTEGER, INTENT(IN), OPTIONAL :: UnEc !< I/O unit for echo file. If present and > 0, write to UnEc.
LOGICAL, INTENT(IN), OPTIONAL :: IsPath !< Flag indicating that string is a path.

CHARACTER(*), INTENT(OUT) :: Var !< The variable to receive the input value.
CHARACTER(*), INTENT(OUT) :: ErrMsg !< The error message, if ErrStat /= 0.
Expand All @@ -2905,6 +2944,7 @@ SUBROUTINE ParseChVar ( FileInfo, LineNum, ExpVarName, Var, ErrStat, ErrMsg, UnE

INTEGER(IntKi) :: ErrStatLcl ! Error status local to this routine.
INTEGER(IntKi) :: NameIndx ! The index into the Words array that points to the variable name.
LOGICAL :: IgnoreQuotes

CHARACTER(NWTC_SizeOfNumWord) :: Words (2) ! The two "words" parsed from the line.
CHARACTER(ErrMsgLen) :: ErrMsg2
Expand All @@ -2924,8 +2964,12 @@ SUBROUTINE ParseChVar ( FileInfo, LineNum, ExpVarName, Var, ErrStat, ErrMsg, UnE
RETURN
END IF


CALL GetWords ( FileInfo%Lines(LineNum), Words, 2 ) ! Read the first two words in Line.
if (present(IsPath)) then
IgnoreQuotes = .not. IsPath
else
IgnoreQuotes = .true.
end if
CALL GetWords ( FileInfo%Lines(LineNum), Words, 2, IgnoreQuotes=IgnoreQuotes ) ! Read the first two words in Line.
IF ( Words(2) == '' .and. (LEN_TRIM(ExpVarName) > 0) ) THEN
CALL SetErrStat ( ErrID_Fatal, 'A fatal error occurred when parsing data from "' &
//TRIM( FileInfo%FileList(FileInfo%FileIndx(LineNum)) )//'".'//NewLine// &
Expand Down
6 changes: 3 additions & 3 deletions modules/servodyn/src/ServoDyn_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1332,15 +1332,15 @@ subroutine ParseInputFileInfo( PriPath, InputFile, OutFileRoot, FileInfo_In, Inp
if ( InputFileData%Echo ) WRITE(UnEcho, '(A)') FileInfo_In%Lines(CurLine) ! Write section break to echo
CurLine = CurLine + 1
! DLL_FileName - Name/location of the dynamic library {.dll [Windows] or .so [Linux]} in the Bladed-DLL format (-) [used only with Bladed Interface]
call ParseVar( FileInfo_In, CurLine, 'DLL_FileName', InputFileData%DLL_FileName, ErrStat2, ErrMsg2, UnEcho )
call ParseVar( FileInfo_In, CurLine, 'DLL_FileName', InputFileData%DLL_FileName, ErrStat2, ErrMsg2, UnEcho, IsPath=.true. )
if (Failed()) return;
IF ( PathIsRelative( InputFileData%DLL_FileName ) ) InputFileData%DLL_FileName = TRIM(PriPath)//TRIM(InputFileData%DLL_FileName)
! DLL_InFile - Name of input file sent to the DLL (-) [used only with Bladed Interface]
call ParseVar( FileInfo_In, CurLine, 'DLL_InFile', InputFileData%DLL_InFile, ErrStat2, ErrMsg2, UnEcho )
call ParseVar( FileInfo_In, CurLine, 'DLL_InFile', InputFileData%DLL_InFile, ErrStat2, ErrMsg2, UnEcho, IsPath=.true. )
if (Failed()) return;
IF ( PathIsRelative( InputFileData%DLL_InFile ) ) InputFileData%DLL_InFile = TRIM(PriPath)//TRIM(InputFileData%DLL_InFile)
! DLL_ProcName - Name of procedure in DLL to be called (-) [case sensitive; used only with DLL Interface]
call ParseVar( FileInfo_In, CurLine, 'DLL_ProcName', InputFileData%DLL_ProcName, ErrStat2, ErrMsg2, UnEcho )
call ParseVar( FileInfo_In, CurLine, 'DLL_ProcName', InputFileData%DLL_ProcName, ErrStat2, ErrMsg2, UnEcho, IsPath=.true. )
if (Failed()) return;
! DLL_DT - Communication interval for dynamic library (s) (or "default") [used only with Bladed Interface]
call ParseVarWDefault( FileInfo_In, CurLine, 'DLL_DT', InputFileData%DLL_DT, InputFileData%DT, ErrStat2, ErrMsg2, UnEcho )
Expand Down
Loading