Optimize This!

Todd Geist tg.lists at geistinteractive.com
Mon May 1 02:28:27 EDT 2006


Hello Everyone,

I had the need to search large binary files for a string. The files  
could be over a gigabyte in size, so I decided not to load the whole  
file into ram but digest in chunks instead.  This is the routine I  
came up with, it seems to work very quickly but I am wondering if  
some of you might be able to speed it up.


FUNCTION GetPositionInBinaryFile pPath, pString, pStart, pOccurrence
     open file pPath for binary read
     IF pStart = "" THEN
         PUT 1 into pStart
     END IF
     IF pOccurrence = "" THEN
         PUT 1 into pOccurrence
     END IF
     put 20000 into tSize
     put 0 into tFoundCount
     REPEAT
         read from file pPath at pStart for tSize
         put Offset(pString, it) into n
         IF it is "" THEN
             Return "0"
         END IF
         IF n > 0 THEN
             put tFoundCount + 1 into tFoundCount
             IF tFoundCount = pOccurrence THEN
                 put pStart + n into tPos
                 Return tPos
             ELSE
                 put pStart + n + 1 into pStart
             END IF
         ELSE
             put pStart + tSize into pStart
         END IF
     END REPEAT
END GetPositionInBinaryFile
close file pPath

what do you think?  Did I miss some obvious easier way?

Thanks

Todd

-- 

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