Android Question B4XPages and ResumableSub

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Just starting with B4XPages and noticed that ResumableSub is not behaving the same as in a non B4XPages project.
In an activity based project I can run this from Main with no problem:

B4X:
    Dim rs As ResumableSub = CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)
    Wait For (rs) Complete (lstCSV As List)
    
    Log(lstCSV.Size)

and I can use the produced list fine.

In a B4XPages project though I get this error:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.BA$ResumableSub

If I run this not as a ResumableSub:

B4X:
    Dim lstCSV As List
    
    lstCSV = CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)

    Log(lstCSV.Size)

There is no problem.
Is this a known limitation of B4XPages or am I doing something wrong?

RBS
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
have your tried

B4X:
wait for (CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)) Complete(lstCSV As List)

Log(lstCSV.Size)
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
have your tried

B4X:
wait for (CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)) Complete(lstCSV As List)

Log(lstCSV.Size)
Tried that but that fails with:

b4xmainpage$ResumableSub_B4XPage_Createdresume (java line: 267)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at b4a.exampleljjll.b4xmainpage$ResumableSub_B4XPage_Created.resume(b4xmainpage.java:267)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)

It works actually fine without the Wait For, but just wonder why it this behaves different in B4XPages.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
what about the code in CSV2List? Can you show it?
It is a lot of code as I made this dealing with misformed and other problematic .csv files, but this it is:

B4X:
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

RBS
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
A Resumable Sub MUST end with
B4X:
as ResumableSub
to return a value.
Then it can be everything, and you must match it correctly with the receiver variable.
Change it and then the code from post #2 should work.
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
A Resumable Sub MUST end with
as ResumableSub
to return a value.
Then it can be everything, and you must match it correctly with the receiver variable.
Change it and then the code from post #2 should work.
Ah, yes, didn't notice that! Will check in a bit what is happening in the non B4XPages project.
Thanks for spotting that.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
A Resumable Sub MUST end with
B4X:
as ResumableSub
to return a value.
Then it can be everything, and you must match it correctly with the receiver variable.
Change it and then the code from post #2 should work.
Just checked and the Sub CSV2List as tested was with As ResumableSub.
I just posted the altered code (with As List) with As List as that worked fine.
So, it remains as in the first post, fine with As ResumableSub in the non-B4XPages project
but not working in the B4XPages project. The Sub just doesn't produce the list, although
it runs fine and adds the right amount of rows.

There was a bug in the posted code, but that was irrelevant as the file I tested with has column
names in the first row.

should be:

B4X:
    lstRows.Initialize
    
    If bFileHasFieldNames Then
        lstRows.Add(arrFields) 'adding the column names
        lRow = lRow + 1
        'as we are skipping the the column names
        '---------------------------------------
        lFirstDataByte = GetCSVFirstDataByte(arrBytes, 10)
    End If

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Just checked and the Sub CSV2List as tested was with As ResumableSub.
I just posted the altered code (with As List) with As List as that worked fine.
So, it remains as in the first post, fine with As ResumableSub in the non-B4XPages project
but not working in the B4XPages project. The Sub just doesn't produce the list, although
it runs fine and adds the right amount of rows.

There was a bug in the posted code, but that was irrelevant as the file I tested with has column
names in the first row.

should be:

B4X:
    lstRows.Initialize
   
    If bFileHasFieldNames Then
        lstRows.Add(arrFields) 'adding the column names
        lRow = lRow + 1
        'as we are skipping the the column names
        '---------------------------------------
        lFirstDataByte = GetCSVFirstDataByte(arrBytes, 10)
    End If

RBS
Attached the zipped project.

RBS
 

Attachments

  • B4AProject.zip
    64.1 KB · Views: 71
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
    File.Copy(File.DirAssets, "menu_props2.csv", File.DirInternal, "menu_props2.csv")
'    Dim rs As ResumableSub = CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)
    Dim rs As ResumableSub = CSV2List(File.DirInternal, "menu_props2.csv", True, 44, 34, True, 10, -1)
    Wait For (rs) Complete (lstCSV As List)
    
    Log(lstCSV.Size)
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
B4X:
    File.Copy(File.DirAssets, "menu_props2.csv", File.DirInternal, "menu_props2.csv")
'    Dim rs As ResumableSub = CSV2List(File.DirRootExternal & "/PhonePats", "menu_props2.csv", True, 44, 34, True, 10, -1)
    Dim rs As ResumableSub = CSV2List(File.DirInternal, "menu_props2.csv", True, 44, 34, True, 10, -1)
    Wait For (rs) Complete (lstCSV As List)
   
    Log(lstCSV.Size)
There was no need to copy the file in my case as I had already put the file in File.DirRootExternal & "/PhonePats" via Windows explorer.
So, that is not the problem.
I suppose you thought it was?

RBS
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
There was no need to copy the file in my case as I had already put the file in File.DirRootExternal & "/PhonePats" via Windows explorer.
So, that is not the problem.
I suppose you thought it was?
No, I had to do it because I didn't have your file in that folder.
However, having tried it that way, it works (and that it works is obvious, because surely the problem is not in the type of project, B4XPages, but in something you do wrong).
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
No, I had to do it because I didn't have your file in that folder.
However, having tried it that way, it works (and that it works is obvious, because surely the problem is not in the type of project, B4XPages, but in something you do wrong).
This is puzzling as I thought you had exactly the same project.
Are you using the same .csv as the one I posted?
Will have another look at it again.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes, I have downloaded your project and your file.
No idea why it didn't work with me but worked fine with you.
I have fixed this now by making the Sub MakeMenuArray also a ResumableSub.
With that it is all fine, but if I run MakeMenuArray as a normal Sub I get the same problem.
It probably has to do with us having a different device. I am compiling with the latest B4A 12.0.
Attached the project as I currently have it.

RBS
 

Attachments

  • B4AProject.zip
    64.8 KB · Views: 71
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Are you exporting the project using Export as ZIP?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
To export a B4XPages project correctly you should see this line
B4X:
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
at the beginning of B4XMainPage.
Rename the ending Project.zip to something different related to your project and then click this line while keeping pressed CTRL on keyboard.
There are missing files in your project because of this.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
To export a B4XPages project correctly you should see this line
B4X:
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
at the beginning of B4XMainPage.
Rename the ending Project.zip to something different related to your project and then click this line while keeping pressed CTRL on keyboard.
There are missing files in your project because of this.
OK, wasn't aware of that and attached a new file, which is indeed a bit larger.

RBS
 

Attachments

  • MenuTest.zip
    163.4 KB · Views: 64
Upvote 0
Top