Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

String constant questions

gelarimer
Beginner
1,407 Views

1)Iuse the string "Example 34' boat.txt" as the command lineargument in VS 2003 (MyprogProperty PageDebuggerCommand Line Argument). When I runMyprog.exe and put a break point in WinMain() I see that
the szCmdFile string looks like this: '''Example 34' boat.txt ".Why is there adbl quote anda single quoteat the beginning of the string (''')?

I was hoping to use the command line arg to simulate a dbl click on an associated file for debugging purposes, but the dbl quote and single quote prevent this.

2) If Iselect the file "C:Example 34' boat.txt" with theWin32 API fn. GetOpenFileName(OFN)it is shown in the Fortran character variable I assignthe path to as 'C:Example 34' boat.txt'. Note that there are 3 single quotes in the string, but this does not cause an error. Why no error?

0 Kudos
8 Replies
Jugoslav_Dujic
Valued Contributor II
1,407 Views
It's a bit difficult to follow your post, as it's not always clear which quotes exist in your computer's memory, and which are used by you as text delimiters smiley [:-)]. However, a couple of hints might be helpful:

* VS debugger always displays strings enclosed in single quotes. Thus, those are "phony" ones. (You might not see the closing quote for long strings). To unconfuse yourselfsmiley [:-)] in cases with too much quotes, use "Memory" debug window (Alt+6)
* File names might or might not be passed with double quotes as command arguments, i.e. into WinMain->lpszCmdLine (that is, GETARG() ) with double quotes around. Double quotes are practically required for correct handling when the file path contains spaces. I'm not sure about exact rules who (Explorer? Cmd.exe? Yourapp.exe) should eventually strip those double quotes, but your application should be prepared to handle both quoted and unquoted arguments, along the following lines:

!XUnQuote removes leading and trailing quote from a string, also
!removing all the text before and after. If the string does not
!contain two quotes, does nothing.
FUNCTION XUnQuote(sString) RESULT(s)

CHARACTER(*), INTENT(IN):: sString
CHARACTER(LEN=PXUnquotedLen(sString)):: s

INTEGER:: iLen1, iLen2

iLen1 = INDEX(sString, '"')
iLen2 = INDEX(sString, '"', .TRUE.)
IF (iLen1.NE.iLen2) THEN
 s = sString(iLen1+1 : iLen2-1)
ELSE
 s = sString
END IF

END FUNCTION XUnQuote

0 Kudos
gelarimer
Beginner
1,407 Views
Thanks for the information andexample code.Info about VS is very helpful.
0 Kudos
gelarimer
Beginner
1,407 Views

JugoslavDujic, what is the most reliable way to determine if a command line arg exists forWinMain(...lpszCmdLine...)?

The method I have been usingsimply checks for the position of a nul character in the string pointed to by lpszCmdLine. If the position is .GT. 1 then I assume that a stringPath exists to a file that was dbl clicked by the user.This appears to work, but I am concerned about the reliability. Code snippet follows:

INTEGER :: iPos
CHARACTER(MAX_PATH+1):: szCmdFile
POINTER(pszCmdFile, szCmdFile)

! assign pointer, where lpszCmdLine is WinMain() dummy arg
pszCmdFile = lpszCmdLine

iPos = INDEX( szCmdFile, ACHAR(0) )

! if iPos ,GT, 1, assume that a fully qualified Path exists
if( iPos .GT. 1 ) then

!Path exists, remove quotes here, if any

end if

Or is it better to use NARGS() or IARGC() to check for a command line arg (Path to a dbl clicked data file)?

0 Kudos
Steven_L_Intel1
Employee
1,407 Views
Intel Fortran supports the Fortran 2003 intrinsics GET_COMMAND, GET_COMMAND_ARGUMENT and COMMAND_ARGUMENT_COUNT. I would recommend using these.
0 Kudos
gelarimer
Beginner
1,407 Views
Thanks Steve.
0 Kudos
gelarimer
Beginner
1,407 Views

Based on the info provide above, I created
two snippets of code for WinMain() that can
retrieve the path of a file clicked on in
Windows Explorer, if a file association in
the Registry exists.

These are preliminary codesnippets that worked
a couple of times on an XP home machine.

I modified JugoslavDujic's code slightly by
removing garbage dbl quotes, if any, after
the nul char before INDEX()ing for dbl Quotes.
I did have a problem with a garbage dbl quote.

*** code snippet 1 ****

INTEGER iPos1, iPos2

CHARACTER(MAX_PATH+1) gszFileName

CHARACTER(Max_Path+1) :: szCmdFile
POINTER(pszCmdFile, szCmdFile)

! lpszCmdLine is from WinMain( args )
pszCmdFile = lpszCmdLine

iPos1 = Index(szCmdFile, ACHAR(0))
if(iPos1 .GT. 1) then

iPos1 = INDEX(szCmdFile, ACHAR(0))
if(iPos1 > 0) then

! pad with blanks after nul char in case a
! garbage dbl quote (") is present
szCmdFile = szCmdFile(1 : iPos1)

end if

! strip dbl quotes
iPos1 = INDEX(szCmdFile, '"')
iPos2 = INDEX(szCmdFile, '"', BACK = .TRUE.)

if(iPos1 .NE. iPos2) then

gszFileName = szCmdFile(iPos1+1 : iPos2-1)//ACHAR(0)

else

gszFileName = ADJUSTL(szCmdFile)

end if

end if

***** code snippet 2, another method *****

INTEGER iPos1, iPos2
INTEGER icnt, iStatus

CHARACTER(MAX_PATH+1) gszFileName


! get number of command line args, 0 is the command,
icnt = COMMAND_ARGUMENT_COUNT()

! prog has 1 Cmd Ln arg, which is path to data file
if(icnt == 1) then

CALL GET_COMMAND_ARGUMENT(1, gszFileName, iStatus)
if(iStatus > 0) then

iPos1 = INDEX(gszFileName, ACHAR(0))
if(iPos1 .GT. 0) then

! pad with blanks after nul char in case a
! garbage dbl quote (") is present
gszFileName = gszFileName(1:iPos1)

end if

! strip dbl quotes
iPos1 = INDEX(gszFileName, '"')
iPos2 = INDEX(gszFileName, '"', BACK = .TRUE.)

if(iPos1 .NE. iPos2) then

gszFileName = gszFileName(iPos1+1 : iPos2-1)//ACHAR(0)

else

! leading space will cause error so ADJUSTL()
gszFileName = ADJUSTL(TRIM(gszFileName)//ACHAR(0))

end if

end if

end i f

******

In code snippet 2 GET_COMMAND_ARGUMENT() appears to do
the following to the valuegszFileName returned:
1. removes the nul char
2. pads the nul char position and beyond
with blanks
3. removes dlg quotes (").

If GET_COMMAND_ARGUMENT() does these things consistantly
then snippet 2 can be simplified. However, not sure
that this is the case.

Thanks for any additional commentsinfo about
GET_COMMAND_ARGUMENT().

0 Kudos
Steven_L_Intel1
Employee
1,407 Views
Items 1 and 2 are required for Fortran functionality. I am fairly certain item 3 is by design as well.
0 Kudos
gelarimer
Beginner
1,407 Views

Steve, appreciate thethe reply for items 1, 2, 1nd 3. Thanks.

An added note:code snippet 1 above isalso based oncode provided by JugoslavDujic in a post on 12/01/2005 in this Forum. The subj. of that post was "Win32/IVF Question".

Error correction in code snippet 2 above:

CALL GET_COMMAND_ARGUMENT(1, gszFileName, iStatus)
if(iStatus > 0) then

should actually be

CALL GET_COMMAND_ARGUMENT(1, gszFileName, iLen, iStatus)
if(iStatus== 0) then

needed to include all three optional args before iStatus returns 0 or success.

0 Kudos
Reply