Sub Class_Globals
Private mstrCharSet As String
Private mbtEndOfLineByte As Byte
Private bFirstArrayDone As Boolean
Private lFilePos As Long
Private lArrayPos As Long
Private bNoEndOfLineFound As Boolean
Private bFinalArrayDone As Boolean
Private bFinalLineDone As Boolean
Private miMaxBytes As Int
Private RAF As RandomAccessFile
Private iBytes As Int
Private arrBytes() As Byte
Private strLine As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(tFF As tFolderAndFile, strCharSet As String, btEndOfLineByte As Byte, lMaxBytes As Long)
mstrCharSet = strCharSet
mbtEndOfLineByte = btEndOfLineByte
miMaxBytes = lMaxBytes
RAF.Initialize(tFF.strFolder, tFF.strFile, True)
lFilePos = 0
lArrayPos = 0
bNoEndOfLineFound = False
bFinalArrayDone = False
bFinalLineDone = False
iBytes = Min(RAF.Size, miMaxBytes)
End Sub
Public Sub Close
RAF.Close
Dim arrBytes() As Byte 'clear some memory?
End Sub
Public Sub ReadLine As String 'ignore
Dim i As Long
Dim bRowReturned As Boolean
Do While bRowReturned = False 'to avoid returning an empty string
bRowReturned = False
If bFinalArrayDone = False Then
If bFirstArrayDone = False Then
'first byte array to process
Dim arrBytes(iBytes) As Byte
iBytes = RAF.ReadBytes(arrBytes, 0, iBytes, 0)
bNoEndOfLineFound = False
bFirstArrayDone = True
Else
If bNoEndOfLineFound Then
Dim arrBytes(iBytes) As Byte
iBytes = RAF.ReadBytes(arrBytes, 0, iBytes, lFilePos)
lArrayPos = 0
bNoEndOfLineFound = False
End If
End If
End If
For i = lArrayPos To iBytes - 1
If arrBytes(i) = mbtEndOfLineByte Then
If i > 0 Then
If arrBytes(i - 1) = 13 Then
strLine = BytesToString(arrBytes, lArrayPos, (i - lArrayPos) - 1, mstrCharSet)
Else
strLine = BytesToString(arrBytes, lArrayPos, i - lArrayPos, mstrCharSet)
End If
lArrayPos = i + 1
bRowReturned = True
Return strLine
End If
End If
Next
If bFinalLineDone Then
bRowReturned = True
Return "" 'can we return Null here as in TextReader?
End If
bNoEndOfLineFound = True
If RAF.Size - lFilePos < miMaxBytes Then
bFinalArrayDone = True
'Added this to avoid missing a final line
If lFilePos < RAF.Size - 1 Then
strLine = BytesToString(arrBytes, lArrayPos, iBytes - lArrayPos, mstrCharSet)
Log("final line: " & strLine)
bFinalLineDone = True
bRowReturned = True
Return strLine
End If
Else
lFilePos = lFilePos + lArrayPos
End If
Loop
End Sub