Source code ch.01 code example: ndx & find in VBScript

Source Code & Software Patents: A Guide to Software & Internet Patent Litigation for Attorneys & Experts
by Andrew Schulman (http://www.SoftwareLitigationConsulting.com)
Code example for Chapter 1: What’s so special (or not so special) about source code?

Code example: mkndx & find in VisualBasic Script (VBS)

mkndx creates an index of lines of source code; find searches in indices created by mkndx

[TODO: show how to use, what output looks like, what index files look like]

========

Mkndx.vbs:

‘ mkndx.vbs

set fs = CreateObject(“Scripting.FileSystemObject”)
set arr = CreateObject(“Scripting.Dictionary”)
set arr2 = CreateObject(“Scripting.Dictionary”)
set tmparr = CreateObject(“Scripting.Dictionary”)
set shell = CreateObject(“WScript.Shell”)
set files = CreateObject(“Scripting.Dictionary”)

set regex = New RegExp
regex.Pattern = “[ \t]+”
regex.Global = True ‘ important!

set fdir = fs.CreateTextFile(“dirfile.db”, True)
set fstr = fs.CreateTextFile(“strfile.db”, True)

function ReadFileSave (fnum, fname)

set f = fs.GetFile(fname)
set ts = f.OpenAsTextStream
dim s

Wscript.StdErr.WriteLine fnum & ” ” & fname
fdir.WriteLine fnum & Chr(9) & fname

while ts.AtEndofStream = False
s = ts.ReadLine()
if Len(s) > 1 then
tmparr.item(regex.Replace(s, ” “)) = 1
end if
wend

ts.Close()

for each s in tmparr
arr.item(s) = arr.item(s) + 1
if arr.item(s) < 50 then
arr2.item(s) = arr2.item(s) & “;” & fnum
end if
next

tmparr.RemoveAll

end function

function GetFileNames (s)
set cmd = shell.Exec(“cmd.exe /c dir /s/b /a-d ” & s)
while cmd.StdOut.AtEndOfStream = false
‘ WScript.Echo cmd.StdOut.ReadLine
files(cmd.StdOut.ReadLine) = 1
wend
‘ TODO: need to close cmd?
end function

for each arg in WScript.Arguments
GetFileNames arg
next

cnt = 1
for each f in files
ReadFileSave cnt, f
cnt=cnt+1
next

cnt = 0
for each str in arr
fstr.WriteLine arr.item(str) & Chr(9) & str & Chr(9) & arr2.item(str)
cnt=cnt+1
next

WScript.Echo cnt & ” unique lines of code”

fstr.Close
fdir.Close

‘ find.vbs
‘ queries dirfile.db and strfile.db created by filetest.vbs

set fs = CreateObject(“Scripting.FileSystemObject”)
set files = CreateObject(“Scripting.Dictionary”)
set filestr = CreateObject(“Scripting.Dictionary”)
set dirfile = fs.GetFile(“dirfile.db”)
set strfile = fs.GetFile(“strfile.db”)
set fdir = dirfile.OpenAsTextStream
set fstr = strfile.OpenAsTextStream
set regex = New RegExp
regex.Pattern = Wscript.Arguments(0)
regex.IgnoreCase = True
TAB = Chr(9)
NL = Chr(13) & Chr(10)

‘ get filenum/pathname association
while fdir.AtEndOfStream = False
s = fdir.ReadLine
arr = Split(s, TAB)
files.item(arr(0)) = arr(1)
wend

‘ look for matches to string on cmdline
while fstr.AtEndOfStream = False
s = fstr.ReadLine
if regex.Test(s) then
arr = Split(s, TAB) ‘ TAB won’t be in db
cnt = arr(0)
str = arr(1)
if (cnt >= 50) then
str = str & ” ### COMMON STRING IN ” & cnt & ” FILES”
end if
arr2 = Split(arr(2), “;”)
for each fnum in arr2
if Len(fnum) > 0 then
filestr.item(fnum) = filestr.item(fnum) & TAB & str
end if
next
end if
wend

‘ for each file with 1+ match, output pathname & all strings
for each fnum in filestr
WScript.Echo NL & files.item(fnum)
arr = Split(filestr.item(fnum), TAB)
for each str in arr
WScript.Echo TAB & str
next
next

========

Print Friendly, PDF & Email