udfStrQuote
str udfStrQuote (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrQuote (strString, strLeft, strRight)
If strString == "" Then Return strString
If "" == strLeft
   If "" == strRight
      strQuote = """'`"
      strCleaned = StrClean (strString, strQuote, "", @FALSE, 2)
      If "" == StrClean (strQuote, strCleaned, "", @FALSE, 1)
         strQuote = '"'
         strString = StrReplace (strString, strQuote, strQuote : strQuote)
      Else
         strCleaned = StrClean (strQuote, strCleaned, "", @FALSE, 1)
         strQuote = StrSub (strCleaned, 1, 1)
      EndIf
      strLeft = strQuote
      strRight = strQuote
   EndIf
EndIf
Return strLeft : strString : strRight
;------------------------------------------------------------------------------------------------------------------------------------------
; This UDF "udfStrQuote" takes a source string and two other strings and returns the given string prepended and appended by the other strings.
; The parameters "strLeft" and "strRight" can be single quotation characters but also any other string content.
;
; If parameters "strLeft" and strRight" both are set to empty strings (strLeft="" and strRight=""),
; then the function chooses a winbatch quote delimiter automagically and doubles the quotation char in source string if necessary.
;
; With parameters "strLeft" and strRight" set to strLeft="""" and strRight="""" resp. strLeft='"' and strRight='"'
; the function allows quotation without touching the content of the source string and doubling of quotation characters in the source string.
;
; Examples:
; With strLeft="(* " and strRight=" *)" the function encloses the content of parameter "strString" in pairs of Pascal comments.
; With strLeft="/* " and strRight=" */" the function encloses the content of parameter "strString" in pairs of C comments.
; With strLeft="; " and strRight="." the function makes a source string looking like a WinBatch comment with trailing dot.
;
; (c)Detlev Dalitz.20010722.20020628.20100124.
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; Example 1.
strString1 = "This is a 'test' string."
strLeft1 = """"
strRight1 = """"
strResult1 = udfStrQuote (strString1, strLeft1, strRight1) ; "This is a 'test' string."

strMsgTitle = "Ex. 1"
strMsgText = "In: " : @LF : ">" : strString1 : "<" : @LF : @LF : "Out: " : @LF : ">" : strResult1 : "<"
Message (strMsgTitle, strMsgText)


; Example 2.
strString2 = "This is a 'test' string."
strLeft2 = "/* "
strRight2 = " */"
strResult2 = udfStrQuote (strString2, strLeft2, strRight2) ; /* This is a 'test' string. */

strMsgTitle = "Ex. 2"
strMsgText = "In: " : @LF : ">" : strString2 : "<" : @LF : @LF : "Out: " : @LF : ">" : strResult2 : "<"
Message (strMsgTitle, strMsgText)


; Example 3.
strString3 = 'This is a "test" string.'
strLeft3 = ""
strRight3 = ""
strResult3 = udfStrQuote (strString3, strLeft3, strRight3) ; 'This is a "test" string.'

strMsgTitle = "Ex. 3"
strMsgText = "In: " : @LF : ">" : strString3 : "<" : @LF : @LF : "Out: " : @LF : ">" : strResult3 : "<"
Message (strMsgTitle, strMsgText)


; Example 4.
strString4 = `'This is a "test" string.'`
strLeft4 = ""
strRight4 = ""
strResult4 = udfStrQuote (strString4, strLeft4, strRight4)  ; `'This is a "test" string.'`

strMsgTitle = "Ex. 4"
strMsgText = "In: " : @LF : ">" : strString4 : "<" : @LF : @LF : "Out: " : @LF : ">" : strResult4 : "<"
Message (strMsgTitle, strMsgText)


; Example 5.
strFunc1 = `udfStrQuote (strIn, '', '')`
strFunc2 = `udfStrQuote (strIn, '"', '"')`
strFunc3 = `udfStrQuote (strIn, '(', ')')`
strFunc4 = `udfStrQuote (strIn, '/* ',' */')`
strFunc5 = `udfStrQuote (strIn, '==> ','')`

strClip = ""
For intF = 1 To 5
   strOut = ""             ; The output string.
   strIn = "."" ab'c ""."  ; The source string.
   strFunc = strFunc%intF% ; The function call.
   strTmp = strIn
   For intI = 1 To 4
      intK = intI + intI
      intJ = intK - 1
      strTmp%intJ% = strTmp
      strTmp%intK% = %strFunc%
      strTmp = strTmp%intK%
      strLine = strTmp%intJ% : @TAB : @TAB : "-->" : @TAB : strTmp%intK%
      strOut = strOut : strLine : @LF
      If StrSub (strTmp, 1, 1) == """" Then strTmp = StrReplace (strTmp, """""", """") ; Simulate quote substitution.
      strIn = strTmp
   Next
   strOut = strFunc : @LF : @LF : strOut
   strClip = strClip : @LF : @LF : strOut
   Pause ("Ex. 5." : intF, strOut)
Next
ClipPut (strClip)
Exit

;------------------------------------------------------------------------------------------------------------------------------------------
;
;   udfStrQuote (strIn, '', '')
;
;   ." ab'c ".          -->   `." ab'c ".`
;   `." ab'c ".`        -->   "`."" ab'c "".`"
;   "`." ab'c ".`"      -->   """`."" ab'c "".`"""
;   ""`." ab'c ".`""    -->   """""`."" ab'c "".`"""""
;
;
;   udfStrQuote (strIn, '"', '"')
;
;   ." ab'c ".          -->   "." ab'c "."
;   "." ab'c "."        -->   ""." ab'c ".""
;   "." ab'c "."        -->   ""." ab'c ".""
;   "." ab'c "."        -->   ""." ab'c ".""
;
;
;   udfStrQuote (strIn, '(', ')')
;
;   ." ab'c ".          -->   (." ab'c ".)
;   (." ab'c ".)        -->   ((." ab'c ".))
;   ((." ab'c ".))      -->   (((." ab'c ".)))
;   (((." ab'c ".)))    -->   ((((." ab'c ".))))
;
;
;   udfStrQuote (strIn, '/* ',' */')
;
;   ." ab'c ".                        -->   /* ." ab'c ". */
;   /* ." ab'c ". */                  -->   /* /* ." ab'c ". */ */
;   /* /* ." ab'c ". */ */            -->   /* /* /* ." ab'c ". */ */ */
;   /* /* /* ." ab'c ". */ */ */    -->   /* /* /* /* ." ab'c ". */ */ */ */
;
;
;   udfStrQuote (strIn, '==> ','')
;
;   ." ab'c ".      -->   ==> ." ab'c ".
;   ==> ." ab'c ".      -->   ==> ==> ." ab'c ".
;   ==> ==> ." ab'c ".      -->   ==> ==> ==> ." ab'c ".
;   ==> ==> ==> ." ab'c ".      -->   ==> ==> ==> ==> ." ab'c ".
;
;------------------------------------------------------------------------------------------------------------------------------------------