Optimize This!
Todd Geist
tg.lists at geistinteractive.com
Mon May 1 11:22:47 EDT 2006
Thanks Alex and Mark!
I have it in place now and it is working great. Here is the final
version I settled on. NOTE in my scenario, I open and close the file
further up the chain because I call this function 4 times in a row.
FUNCTION GetPositionInBinaryFile pPath, pString, pStart, pOccurrence
put the number of chars in pString into tStringLength
put 100000 into tSize
IF tStringLength = 0 THEN return 0
IF tStringLength > tSize THEN return 0
IF pStart = "" THEN PUT 1 into pStart
IF pOccurrence = "" THEN PUT 1 into pOccurrence
put 0 into tFoundCount
put 0 into tPos
-- file is already open so we don't need this
--open file pPath for binary read
REPEAT
read from file pPath at pStart for tSize
put it into tData
IF the number of chars in tData < tStringLength THEN return 0
put 0 into tCharsToSkip
REPEAT
put offset(pString, tData, tCharsToSkip) into n
IF n > 0 THEN
add 1 to tFoundCount
IF tFoundCount = pOccurrence THEN
put pStart + tCharsToSkip + n into tPos
-- close file pPath
Return tPos
ELSE
add n + tStringLength to tCharsToSkip -- what
about overlapping sub-strings ??!!
END IF
ELSE
-- no more to be found in this block
EXIT REPEAT
END IF
END REPEAT
-- we get here when n = 0, i.e. no more occurrences in this
block
put pStart + tSize - tStringLength into pStart
END REPEAT
END GetPositionInBinaryFile
--
Todd Geist
______________________________________
g e i s t i n t e r a c t i v e
More information about the use-livecode
mailing list