Sub CSV2List(strFolder As String, _
strFile As String, _
bFileHasFieldNames As Boolean, _
btSeparatorByte As Byte, _
btEncloserByte As Byte, _
bLookForEncloserByte As Boolean, _
btEndOfLineByte As Byte, _
lRows As Long) As List
Dim i As Long
Dim c As Long
Dim arrBytes() As Byte
Dim lBytes As Long
Dim lColumns As Long
Dim lRow As Long
Dim lUB As Long
Dim lSeparatorPos As Long
Dim lEncloserPos As Long
Dim lChr10Pos As Long
Dim arrFields() As Object
Dim lstRows As List
Dim lFirstDataByte As Long
Dim bExit As Boolean
Dim bCSVHasEncloserBytes As Boolean
'this will be a zero-based UTF8 byte array
'-----------------------------------------
RAF.Initialize(strFolder, strFile, True)
lBytes = RAF.Size
'Log("CSV2List, lBytes: " & lBytes)
Dim arrBytes(lBytes) As Byte
RAF.ReadBytes(arrBytes, 0, lBytes, 0)
lUB = arrBytes.Length - 1
'General.StartSW(5)
arrFields = GetCSVHeaders(arrBytes, btSeparatorByte, 34, 10)
lColumns = arrFields.Length
Dim arrData(lColumns) As String '<<<<<<<<<< was as Object!
If bFileHasFieldNames Then
lstRows.Initialize
lstRows.Add(arrFields) 'adding the column names
lRow = lRow + 1
'as we are skipping the the column names
'---------------------------------------
lFirstDataByte = GetCSVFirstDataByte(arrBytes, 10)
End If
'so we ignore enclosers in the fields row!
'-----------------------------------------
If bLookForEncloserByte Then
'if iBytesToCheckForEncloserByte = 0 then check all bytes, till one is found
bCSVHasEncloserBytes = CSVHasEncloserBytes(arrBytes, lFirstDataByte, iBytesToCheckForEncloserByte, btEncloserByte)
Else
If btEncloserByte = -1 Then
bCSVHasEncloserBytes = False
Else
bCSVHasEncloserBytes = True
End If
End If
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = lFirstDataByte - 1 '<<<<<<!!
'deal with the first byte, taking this out speeds up the next loop a bit
'-----------------------------------------------------------------------
If bCSVHasEncloserBytes Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If lColumns > 1 Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
''''''''''''''''''''''''''
'SEPARATORS AND ENCLOSERS'
''''''''''''''''''''''''''
Select Case arrBytes(lFirstDataByte)
Case btSeparatorByte
arrData(c) = "" '<<<<<<<<< was Null!
Case btEncloserByte
lEncloserPos = lFirstDataByte
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btSeparatorByte
If c < lColumns Then
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If arrBytes(i - 1) <> btEncloserByte Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8")
c = c + 1
lChr10Pos = -1
End If 'If arrBytes(i - 1) <> btEncloserByte
Else 'If lSeparatorPos = -1
If i - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else 'If (i - 1) - (lSeparatorPos + 1) > 1
arrData(c) = "" '<<<<<<<<< was Null!
End If 'If (i - 1) - (lSeparatorPos + 1) > 1
c = c + 1
lChr10Pos = -1
End If 'If lSeparatorPos = -1
End If 'If lEncloserPos = -1
End If 'If c < lColumns
lSeparatorPos = i
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If c < lColumns Then
If i - lEncloserPos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
c = c + 1
Else 'If i - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8")
c = c + 1
End If 'If i - lEncloserPos = 1
End If 'If c < lColumns
lEncloserPos = -1
End If
lSeparatorPos = -1
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - 2) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - 1) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
End If 'If arrBytes(i - 1) = 13
End If
lstRows.Add(arrData)
lRow = lRow + 1
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As String
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btSeparatorByte
If c < lColumns Then
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If lUB - lChr10Pos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
Else 'If lUB - lChr10Pos = 1
If arrBytes(lUB - 1) <> btEncloserByte Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
End If 'If lUB - lChr10Pos = 1
End If 'If lSeparatorPos = -1
Else 'If lEncloserPos = -1
If lUB - lSeparatorPos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
End If
End If 'If lEncloserPos = -1
End If 'If collStrings.Count < lColumns
Case btEncloserByte
If lEncloserPos > -1 Then
If c < lColumns Then
If lUB - lEncloserPos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
Else 'If lUB - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (lUB - 1) - lEncloserPos, "UTF-8")
End If 'If lUB - lEncloserPos = 1
End If 'If c < lColumns
End If 'If lEncloserPos > -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If lUB - lSeparatorPos > 2 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else 'If arrBytes(lUB - 1) = 13
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
End If 'If arrBytes(lUB - 1) = 13
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 2) <> btEncloserByte Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If arrBytes(lUB - 2) <> btEncloserByte
End If 'If lChr10Pos > -1
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, lUB - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
End Select
End If
Else 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'''''''''''''''''''''''''''''
'NO SEPARATORS BUT ENCLOSERS'
'''''''''''''''''''''''''''''
Select Case arrBytes(lFirstDataByte)
Case btEncloserByte
lEncloserPos = lFirstDataByte
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If c < lColumns Then
If i - lEncloserPos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
c = c + 1
Else 'If i - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8")
c = c + 1
End If 'If i - lEncloserPos = 1
End If 'If c < lColumns
lEncloserPos = -1
End If
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If c < lColumns
lstRows.Add(arrData)
lRow = lRow + 1
lEncloserPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As String
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btEncloserByte
If lEncloserPos > -1 Then
If c < lColumns Then
If lUB - lEncloserPos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
Else 'If lUB - lEncloserPos = 1
arrData(c) = BytesToString(arrBytes, lEncloserPos + 1, (lUB - 1) - lEncloserPos, "UTF-8")
End If 'If lUB - lEncloserPos = 1
End If 'If c < lColumns
End If 'If lEncloserPos > -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If (lUB - 2) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else
If (lUB - 1) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 2) <> btEncloserByte Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If arrBytes(lUB - 2) <> btEncloserByte
End If 'If lChr10Pos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If lEncloserPos = -1 Then
If c < lColumns Then
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If c < lColumns
End If 'If lEncloserPos = -1
End Select
End If
End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Else 'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'''''''''''''''''''''''''''''
'SEPARATORS BUT NO ENCLOSERS'
'''''''''''''''''''''''''''''
If lColumns > 1 Then '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Select Case arrBytes(lFirstDataByte)
Case btSeparatorByte
arrData(c) = "" '<<<<<<<<< was Null!
End Select
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btSeparatorByte
If c < lColumns Then
If lSeparatorPos = -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8")
c = c + 1
lChr10Pos = -1
Else 'If lSeparatorPos = -1
If i - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else 'If (i - 1) - (lSeparatorPos + 1) > 1
arrData(c) = "" '<<<<<<<<< was Null!
End If 'If (i - 1) - (lSeparatorPos + 1) > 1
c = c + 1
lChr10Pos = -1
End If 'If lSeparatorPos = -1
End If 'If c < lColumns
lSeparatorPos = i
Case btEndOfLineByte
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(i - 1) = 13 Then
If (i - 2) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - 1) - lSeparatorPos > 0 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
End If 'If arrBytes(i - 1) = 13
Else 'If lSeparatorPos > -1
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If lSeparatorPos > -1
lstRows.Add(arrData)
lRow = lRow + 1
' If lRow = 91 Then
' Log("91")
' End If
lSeparatorPos = -1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As String
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End If 'If lEncloserPos = -1
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btSeparatorByte
If c < lColumns Then
If lSeparatorPos = -1 Then
If lUB - lChr10Pos = 1 Then
arrData(c) = "" '<<<<<<<<< was Null!
Else 'If lUB - lChr10Pos = 1
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If 'If lUB - lChr10Pos = 1
End If 'If lSeparatorPos = -1
End If 'If collStrings.Count < lColumns
Case btEndOfLineByte
If lEncloserPos = -1 Then
If c < lColumns Then
If lSeparatorPos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If lUB - lSeparatorPos > 2 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 1) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else 'If arrBytes(lUB - 1) = 13
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, (lUB - 2) - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
End If 'If arrBytes(lUB - 1) = 13
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
If (lUB - 2) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else
If (lUB - 1) - lChr10Pos > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If lChr10Pos > -1
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End If 'If lEncloserPos = -1
Case Else
If c < lColumns Then
If lSeparatorPos > -1 Then
If lUB - lSeparatorPos > 1 Then
arrData(c) = BytesToString(arrBytes, lSeparatorPos + 1, lUB - lSeparatorPos, "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If lSeparatorPos > -1
End If 'If c < lColumns
End Select
End If
Else 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
''''''''''''''''''''''''''''''''
'NO SEPARATORS AND NO ENCLOSERS'
''''''''''''''''''''''''''''''''
For i = lFirstDataByte + 1 To lUB - 1
Select Case arrBytes(i)
Case btEndOfLineByte
If c < lColumns Then
If arrBytes(i - 1) = 13 Then
If (i - lChr10Pos) -1 > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) -1 , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
Else 'If arrBytes(i - 1) = 13
If (i - lChr10Pos) > 0 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (i - lChr10Pos) , "UTF-8")
Else
arrData(c) = "" '<<<<<<<<< was Null!
End If
c = c + 1
End If 'If i - lSeparatorPos > 1
End If 'If c < lColumns
lstRows.Add(arrData)
lRow = lRow + 1
lChr10Pos = i
c = 0
Dim arrData(lColumns) As String
If lRows > -1 Then
If lRow > lRows Then
bExit = True
End If
End If
End Select
If bExit Then Exit
Next
'deal with the last byte, this is needed for if there is no final linebreak
'--------------------------------------------------------------------------
If lRows = -1 Then
Select Case arrBytes(lUB)
Case btEndOfLineByte
If c < lColumns Then
If lChr10Pos > -1 Then
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 2) - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, (lUB - 1) - lChr10Pos, "UTF-8")
End If
Else 'If lChr10Pos > -1
If arrBytes(lUB - 1) = 13 Then
arrData(c) = BytesToString(arrBytes, 0, lUB - 2, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, 0, lUB - 1, "UTF-8")
End If
End If 'If lChr10Pos > -1
End If 'If c < lColumns
Case Else
If c < lColumns Then
If lChr10Pos > -1 Then
arrData(c) = BytesToString(arrBytes, lChr10Pos + 1, lUB - lChr10Pos, "UTF-8")
Else
arrData(c) = BytesToString(arrBytes, lFirstDataByte, (lUB - lFirstDataByte) + 1, "UTF-8")
End If
End If 'If c < lColumns
End Select
End If
End If 'If lColumns > 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End If 'If bCSVHasEncloserBytes<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'add final data array
lstRows.Add(arrData)
Return lstRows
End Sub
Sub GetCSVHeaders(arrBytes() As Byte, _
btSeparatorByte As Byte, _
btEncloserByte As Byte, _
btEndOfLineByte As Byte) As String()
Dim i As Long
Dim c As Long
Dim lUB As Long
Dim lSeparatorPos As Long
Dim lEncloserPos As Long
Dim lChr10Pos As Long
Dim lstFields As List
Dim bNoEnclosers As Boolean
Dim bExitLoop As Boolean
lUB = arrBytes.Length - 1
bNoEnclosers = btEncloserByte = -1
lEncloserPos = -1
lSeparatorPos = -1
lChr10Pos = -1
lstFields.Initialize
If bNoEnclosers Then 'no enclosers
For i = 0 To lUB
Select Case arrBytes(i)
Case btSeparatorByte
If lSeparatorPos = -1 Then
If i - lChr10Pos = 1 Then
bExitLoop = True
Else
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
c = c + 1
lChr10Pos = -1
End If
Else
If i - lSeparatorPos > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
Else
bExitLoop = True
End If
c = c + 1
lChr10Pos = -1
End If
lSeparatorPos = i
Case btEndOfLineByte
If lSeparatorPos > -1 Then
If arrBytes(i - 1) = 13 Then
If i - lSeparatorPos > 2 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
Else
If i - lSeparatorPos > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 2) - lChr10Pos, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
End If
c = c + 1
Else
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, 0, i - 1, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, 0, i, "UTF-8"))
End If
c = c + 1
End If
End If 'If lSeparatorPos > -1
bExitLoop = True
End Select
If bExitLoop Then Exit
Next
Else 'If bNoEnclosers
For i = 0 To lUB
Select Case arrBytes(i)
Case btSeparatorByte
If lEncloserPos = -1 Then
If lSeparatorPos = -1 Then
If i - lChr10Pos = 1 Then
bExitLoop = True
Else
If arrBytes(i - 1) <> btEncloserByte Then
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
c = c + 1
lChr10Pos = -1
End If
End If
Else
If i - lSeparatorPos > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
Else
bExitLoop = True
End If
c = c + 1
lChr10Pos = -1
End If
Else
If i - lSeparatorPos = 1 Then
bExitLoop = True
End If
End If
lSeparatorPos = i
Case btEncloserByte
If lEncloserPos = -1 Then
lEncloserPos = i
Else
If i - lEncloserPos = 1 Then
bExitLoop = True
Else
lstFields.Add(BytesToString(arrBytes, lEncloserPos + 1, (i - 1) - lEncloserPos, "UTF-8"))
c = c + 1
End If
lEncloserPos = -1
End If
lSeparatorPos = -1
lChr10Pos = -1
Case btEndOfLineByte
If lEncloserPos = -1 Then
If lSeparatorPos > -1 Then
If arrBytes(i - 1) = 13 Then
If i - lSeparatorPos > 2 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 2) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
Else
If i - lSeparatorPos > 1 Then
lstFields.Add(BytesToString(arrBytes, lSeparatorPos + 1, (i - 1) - lSeparatorPos, "UTF-8"))
c = c + 1
Else
bExitLoop = True
End If
End If
Else 'If lSeparatorPos > -1
If lChr10Pos > -1 Then
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 2) - lChr10Pos, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, lChr10Pos + 1, (i - 1) - lChr10Pos, "UTF-8"))
End If
c = c + 1
Else
If arrBytes(i - 2) <> btEncloserByte Then
If arrBytes(i - 1) <> btEncloserByte Then
If arrBytes(i - 1) = 13 Then
lstFields.Add(BytesToString(arrBytes, 0, i - 1, "UTF-8"))
Else
lstFields.Add(BytesToString(arrBytes, 0, i, "UTF-8"))
End If
c = c + 1
End If
End If
End If
End If 'If lSeparatorPos > -1
bExitLoop = True
End If 'If lEncloserPos = -1
End Select
If bExitLoop Then Exit
Next
End If 'If bNoEnclosers
Dim arrFields(lstFields.Size) As String
For i = 0 To lstFields.Size - 1
arrFields(i) = lstFields.Get(i)
Next
Return arrFields
End Sub
Sub GetCSVFirstDataByte(arrBytes() As Byte, btEndOfLineByte As Byte) As Long
Dim i As Long
For i = 0 To arrBytes.Length - 1
If arrBytes(i) = btEndOfLineByte Then
Return i + 1
End If
Next
Return -1
End Sub
Sub CSVHasEncloserBytes(arrBytes() As Byte, lFirstByte As Long, lBytesToCheck As Long, btEncloserByte As Byte) As Boolean
Dim i As Long
Dim lUB As Long
If lBytesToCheck = 0 Then
lUB = arrBytes.Length - 1
Else
lUB = lBytesToCheck - 1
If lUB > arrBytes.Length -1 Then
lUB = arrBytes.Length -1
End If
End If
For i = lFirstByte To lUB
If arrBytes(i) = btEncloserByte Then
Return True
End If
Next
Return False
End Sub