PDA

View Full Version : Finding Functions, Quotes, Comments, and Dim statement in source code



Gary
28-08-2023, 20:35
'---Script created on 08-28-2023 13:17:25 by
'---Script created on 08-27-2023 11:59:24 by
uses "VBREGEXP"
Uses "Console"
uses "File"






'FindAllComments_InSourceCode()
'FindFunctions()
'Find_Dims_InSourceCode()
Find_Quotes_InSourceCode()




function Find_Quotes_InSourceCode()
dim TheFile as string
dim FileData as string
dim mask as string
dim startvar as long
dim posVar as long
dim lenvar as long
dim retFromReg as string
TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
FileData = File2Text(TheFile)
dim comments as string




mask = CHR$(34) + "(.*)" + CHR$(34) + "|[\q](.*)[\q]"

vbregex(mask, fileData)
Console_WaitKey
end function




function Find_Dims_InSourceCode()
dim TheFile as string
dim FileData as string
dim mask as string
dim startvar as long
dim posVar as long
dim lenvar as long
dim retFromReg as string
TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
FileData = File2Text(TheFile)
dim comments as string
Mask = "Dim[ ][a-z0-9]+[ ]+[as]+[ ]+[a-z0-9]+([ ]*[,]*[ ]*[a-z0-9_]*[ ]*[as]*[ ]*[a-z0-9]*)*"


vbregex(mask, fileData)
Console_WaitKey
end function






function FindAllComments_InSourceCode()
dim TheFile as string
dim FileData as string
dim mask as string
dim startvar as long
dim posVar as long
dim lenvar as long
dim retFromReg as string
TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
FileData = File2Text(TheFile)
dim comments as string
Mask = "'[\x20]*[A-Za-z0-9 ]*"


vbregex(mask, fileData)
Console_WaitKey
end function


function FindFunctions()
dim TheFile as string
dim FileData as string
dim mask as string
dim startvar as long
dim posVar as long
dim lenvar as long
dim retFromReg as string
TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
FileData = File2Text(TheFile)
dim comments as string
Mask = "[\x20]*FUNCTION[\x20]+[a-z-0-9]+[\x20]*\([\x20]*[a-z-0-9_]*[\x20]*[as]*[\x20]*[a-z-0-9_]*[ ]*([,][ ]*[a-z0-9_]*[ ]*[as]*[ ]*[a-z0-9_]*)*[ ]*\)[ ]*[as]*[ ]*[a-z0-9]*"


vbregex(mask, fileData)
Console_WaitKey
end function








function vbregReplace(pattern as string,txt as string,replaceTxt as string )


dim lpRegExp as dword
'dim txt as string value "The quick brown fox jumped over the lazy dog."
dim strRetVal as string


'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New


'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
print "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
stop
end if


'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1



'---Replace example 1
'---Replace 'fox' with 'cat'.
VBREGEXP_SetPattern lpRegExp, pattern
strRetVal = VbRegExp_Replace(lpRegExp, txt, replaceTxt )

print strRetVal+$crlf



'---Replace example 2
'---In addition, the Replace method can replace subexpressions in the pattern.
'---The following swaps the first pair of words in the original string:
'VBREGEXP_SetPattern lpRegExp, "(\S+)(\s+)(\S+)"
'strRetVal = VbRegExp_Replace(lpRegExp, txt, "$3$2$1")

'print strRetVal+$crlf


'---Deallocate regular expression resource
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)


end function






Function File2Text(sfn As String) As String


Dim st As String
Dim n As Long
Dim FileHandle As DWORD,s as string
FileHandle= FILE_OPEN(sfn, "INPUT")
while not FILE_EOF(FileHandle)
s=FILE_LineInput(FileHandle)
st=st+s+$crlf
wend

s = FILE_Close(FileHandle)
Function=st
End Function






function vbregex(pattern as string, txt as string)





dim lpRegExp as dword
dim lpMatches as dword
dim lpMatch as dword
dim strValue as string




'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New


'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
Print "Unable to create an instance of the RegExp object." & $crlf & "Script terminated" + $crlf
stop
end if

'---Set pattern
VBREGEXP_SetPattern lpRegExp, pattern '"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"

'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1


'---Execute search '"Please send a mail to eros.olmi@thinbasic.com or to <support@thinbasic.com>. Thanks!"
lpMatches = VBREGEXP_Execute(lpRegExp, txt)
IF ISFALSE lpMatches THEN
Print "1. No match found"
else

dim nCount as long value VBMatchCollection_GetCount(lpMatches)
IF nCount = 0 THEN
Print "2. No match found" + $crlf
else


'---Iterate the Matches collection
dim I as long


strValue += "Total matches found: " & nCount & $CRLF & string$(50, "-") & $crlf
FOR i = 1 TO nCount


lpMatch = VBMatchCollection_GetItem(lpMatches, i)


IF ISFALSE lpMatch THEN EXIT FOR

strValue += "Match number " & i & " found at position: " & VBMatch_GetFirstIndex(lpMatch) & " length: " & VBMatch_Getlength(lpMatch) & $CRLF
strValue += "Value is: " & VBMatch_GetValue(lpMatch) & $CRLF
strValue += "--------------" & $CRLF

VBREGEXP_Release lpMatch

NEXT

Print strValue + $crlf


END IF

END IF

IF istrue lpMatches THEN VBREGEXP_Release(lpMatches)
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)



end function