Sub CountFieldsInTextLine(strCSVLine As String, strSeparator As String, strEncloser As String) As Int
Dim c As Int
Dim iEndIndex As Int
Dim bInsideQuotes As Boolean = False
For iEndIndex = 0 To strCSVLine.Length - 1
If strCSVLine.CharAt(iEndIndex) = strEncloser Then
bInsideQuotes = bInsideQuotes = False
Else
If strCSVLine.CharAt(iEndIndex) = strSeparator Then
If bInsideQuotes = False Then
c = c + 1
End If
End If
End If
Next
Return c + 1
End Sub
Sub ParseCSVLine(strCSVLine As String, iCols As Int, strSeparator As String, strEncloser As String) As String()
Dim c As Int
Dim arrValues(iCols) As String
Dim iStartIndex As Int
Dim iEndIndex As Int
Dim bInsideQuotes As Boolean = False
For iEndIndex = 0 To strCSVLine.Length - 1
If strCSVLine.CharAt(iEndIndex) = strEncloser Then
bInsideQuotes = bInsideQuotes = False
Else
If strCSVLine.CharAt(iEndIndex) = strSeparator Then
If bInsideQuotes = False Then
arrValues(c) = strCSVLine.SubString2(iStartIndex, iEndIndex)
iStartIndex = iEndIndex + 1
c = c + 1
End If
End If
End If
Next
arrValues(c) = strCSVLine.SubString2(iStartIndex, iEndIndex )
Return arrValues
End Sub
Sub TestTextReader
Dim c As Int 'ignore
Dim oTR As TextReader
Dim lBytes As Long
Dim strLine As String
Dim iCols As Int
Dim lstCSV As List
Dim rs As ResumableSub = PickFile("Parse a .csv or .txt file", _
"Pick a .csv or .txt file or a folder", _
File.DirRootExternal & "/PhonePats", _
Array As String("csv", "txt"), _
True)
Wait For (rs) Complete (tFF As tFolderAndFile)
StartSW(0)
RAF.Initialize(tFF.strFolder, tFF.strFile, True)
lBytes = 1000 'this is enough at least in case of the SCL.csv file
Dim arrBytes(lBytes) As Byte
RAF.ReadBytes(arrBytes, 0, lBytes, 0)
Dim rs1 As ResumableSub = DetectCharacterEncoding(arrBytes)
Wait For (rs1) Complete (strCharset As String)
Log(strCharset) '>> windows-1252
oTR.Initialize2(File.OpenInput(tFF.strFolder, tFF.strFile), strCharset)
lstCSV.Initialize
'first line will be column names
strLine = oTR.ReadLine
iCols = CountFieldsInTextLine(strLine, "¬", Chr(34))
Dim arrColumnNames() As String = ParseCSVLine(strLine, iCols, "¬", Chr(34)) 'ignore, will be used to create SQLite table
Do While True
strLine = oTR.ReadLine
If strLine = Null Then Exit
Dim arr() As String = ParseCSVLine(strLine, iCols, "¬", Chr(34))
' If arr(0) = "8329479" Then
' For c = 0 To arr.Length - 1
' Log(arr(c))
' Next
' End If
lstCSV.Add(arr) 'will need to move list to DB if gets too big and Dim a new list
Loop
Log("lstCSV.Size: " & lstCSV.Size & ", " & FormatMilliSecs(StopSW(0), False)) 'lstCSV.Size: 28269, 110 milli-seconds
oTR.Close
End Sub