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"
list="#variableCFScriptTextTmp#"
delimiters="#chr(13)##chr(10)#">
<cfif NOT (left(trim(variableCFScriptCmd),2) eq "//")>

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

<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,1,variableCFcommentSTART-2)
& Mid(variableCFScriptTextTmp,variableCFcommentEND+2,len(variableCFScriptTextTmp) )/>
</cfif>
<cfset variableCFcommentSTART=find("/*",variableCFScriptTextTmp,variableCFcommentSTART+2)>
</cfloop>

<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">
</cfif>

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

</cfif>
</cfloop>


<cfset currentPositionVariableFind = arguments.positionToStart />

4 comments:

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 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