On Feb 16, 2007, at 3:54 PM, Tim Jones wrote:

On Feb 16, 2007, at 1:36 PM, Kevin Windham wrote:

On Feb 16, 2007, at 2:29 PM, Tim Jones wrote:

Hi Folks,

I'm trying to track down the best way to parse a binary file, searching for specific sequences of bytes. I assume that reading the file into a MemoryBlock (the files tend to be a few 100K at worst) will be the best solution, but what would then be used to walk though the memoryblock to locate the sequence of bytes (sort of like InStrB)?

I would just use InStrB on a string. As long as you don't do "bad stuff" then it should be reasonably quick. Of course I don't know what kind of files they are or what you're using them for, but I've parsed decent sized files before using InStrB and it didn't take an unreasonable amount of time.

I was not doing random searches though. I was parsing the file from beginning to end. So it was something like, look for first marker then from there look for second marker and stuff all those bytes into a variable. Then continue from where I left off and repeat until the EOF.

Thanks Kevin and Charles. It never dawned on me to use a String. I'm so used to CStrings and the NULL that the thought of all the NULL's written into the file sent me searching elsewhere.

So here's my pseudo code:

        Dim i, keyOffset As Integer
        Dim f As FolderItem
        Dim ft As TextInputStream

        f = GetFolderItem(filePath, FolderItem.PathTypeShell)
        // check for bad f instance
        fs = f.OpenAsTextFile
        theFileString = fs.ReadAll
        keyOffset = 0
        Do
                keyOffset = InStr(theFileString, keyOffset + 1)
                // Process located data
        Loop Until keyOffset >= LenB(theFileString

Any holes?


InStrB returns 0 if no match is found, so this loop will never terminate. Instead --
        

        Dim f As FolderItem = GetFolderItem(filePath, FolderItem.PathTypeShell)
        // check for bad f instance
        Dim fs As TextInputStream = f.OpenAsTextFile
        // check for nil fs
        theFileString = fs.ReadAll
        dim matchStringLength as Integer = LenB(matchString)
        Dim keyOffset As Integer = 1
        Do
                keyOffset = InStrB(keyOffset, theFileString, matchString )
                if keyOffset > 0 then
                  // Process located data
                  keyOffset = keyOffset + matchStringLength
                else
                  exit
                end if
        Loop


Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to