Saturday, September 29, 2007

Varscoper.cfc CFSCRIPT support

UPDATE: Updated code posted here

Is your code Thread Safe? How do you know?

that inspired me to do a bit of hacking as i use a lot of CFSCRIPT which the original doesn't support yet, it's a friday afternoon job, but it worked for me

add this code into the top of the findCFsetVariables function in varscoper.cfc

it will find any var scoped cfscript statements to reduce the false positives

<cfset var variableCFScriptDelim=" #chr(13)##chr(9)##chr(8)#">
<cfset var variableCFScriptStart = ""/>
<cfset var variableCFScriptEND = ""/>
<cfset var variableCFScriptText = ""/>

<cfset var variableCFScriptTextTmp = ""/>
<cfset var variableCFScriptCmd = ""/>
<cfset var variableCFcommentSTART = "" />
<cfset var variableCFcommentEND =""/>

<!--- Now start looping over all cfscript statements to identify variables that are being set --->

<cfset currentPositionVariableFind = 1 />
<cfset variableCFScriptStart = FindNoCase("<CFS"&"CRIPT>",functionInnerText,currentPositionVariableFind)/>

<cfloop condition="variableCFScriptStart NEQ 0">
<!--- Identify the cfscript statement --->

<cfset variableCFScriptEND = FindNoCase("</CFS"&"CRIPT>",functionInnerText, variableCFScriptStart+1)/>

<cfif variableCFScriptEND neq 0>
<cfset variableCFScriptText=""/>

<cfset variableCFScriptTextTmp =mid(functionInnerText, variableCFScriptSTART + len('<cfs'&'cript>'),
variableCFScriptEND-variableCFScriptSTART- len('<cfsc'&'ript>') )/>

<!--- strip line // comments as they don't end in a ; --->
<cfloop index="variableCFScriptCmd"
<cfif NOT (left(trim(variableCFScriptCmd),2) eq "//")>

<cfset variableCFScriptText=variableCFScriptText&"#chr(13)##chr(10)#"&variableCFScriptCmd/>

<cfset variableCFScriptTextTmp=variableCFScriptText>

<!--- strip out /* type comments */ --->

<cfset variableCFcommentSTART=find("/*",variableCFScriptTextTmp,1)>

<cfloop condition="variableCFcommentSTART neq 0">
<cfset variableCFcommentEND=find("*/",variableCFScriptTextTmp,variableCFcommentSTART+2)>
<cfif variableCFcommentEND gt 0>

<cfset variableCFScriptTextTmp=
& Mid(variableCFScriptTextTmp,variableCFcommentEND+2,len(variableCFScriptTextTmp) )/>
<cfset variableCFcommentSTART=find("/*",variableCFScriptTextTmp,variableCFcommentSTART+2)>

<cfloop index="variableCFScriptCmd" list="#variableCFScriptTextTmp#" delimiters=";">

<cfset variableCFScriptCmd=ListFirst(ListChangeDelims(variableCFScriptCmd," ",variableCFScriptDelim),"=")/>
<cfset variableCFScriptCmd=trim(variableCFScriptCmd)>
<cfif ListFirst(trim(variableCFScriptCmd), " ") eq "var">
<cfset VariableNameCfset=mid(variableCFScriptCmd,3,len(variableCFScriptCmd))/>

<cfset VariableNameCfset=trim(ListGetAt(variableCFScriptCmd,2, " "))/>
<!--- Update the var-ed struct to note that we are using this var --->
<cfset tempVaredStruct["#VariableNameCfset#"] = "not used">

<!--- Update the current parsing position to start from the end of the cfset statement --->
<cfset currentPositionVariableFind = variableCFScriptEND>
<cfset variableCFScriptStart = FindNoCase("<CFS"&"CRIPT>",functionInnerText,currentPositionVariableFind)>
<cfset variableCFScriptStart=variableCFScriptStart+len("<CFS"&"CRIPT>")>


<cfset currentPositionVariableFind = arguments.positionToStart />


Anonymous said...

thanks for this, though there seems to be some code missing from your copy and paste , the loop and if statement is missing after this line

--- Update the current parsing position to start from the end of the cfset statement ---

well I'm guessing it would go around there somewhere :-)

Zac Spitzer / Fastmovingtarget said...

I can see the closing cfloop and cfif statement on the blog entry... the code there is the same as my local code

Anonymous said...

found out what I was doing wrong, pasting it in the wrong place, should have pasted your code under the last cfset :-) Thanks

Anonymous said...

I can see what I was doing wrong, should have pasted it after the last cfset :-) thanks