QB64 Phoenix Edition
Split String to Array Using Strtok (Attempt #2) - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Split String to Array Using Strtok (Attempt #2) (/showthread.php?tid=3295)



Split String to Array Using Strtok (Attempt #2) - SpriggsySpriggs - 12-17-2024

Updating to v4.0 broke my code so I had to edit it to match the new file structure of the headers and to take into account a difference in tcslen in this compiler version versus the old one. tcslen is reporting as 1 character shorter than the old version. But I've remedied that. For now.

Code: (Select All)
ReDim As String myArray(0)
tokenize "This;is;an;example;string", ";", myArray()
Dim As Integer x
For x = LBound(myArray) To UBound(myArray)
    Print myArray(x)
Next


Sub tokenize (toTokenize As String, delimiters As String, StorageArray() As String)
    Declare CustomType Library
        Function strtok%& (ByVal str As _Offset, delimiters As String)
    End Declare
    Dim As _Offset tokenized
    Dim As String tokCopy: tokCopy = toTokenize + Chr$(0)
    Dim As String delCopy: delCopy = delimiters + Chr$(0)
    Dim As _Unsigned Long lowerbound: lowerbound = LBound(StorageArray)
    Dim As _Unsigned Long i: i = lowerbound
    tokenized = strtok(_Offset(tokCopy), delCopy)
    While tokenized <> 0
        ReDim _Preserve StorageArray(lowerbound To UBound(StorageArray) + 1)
        StorageArray(i) = PointerToString(tokenized)
        tokenized = strtok(0, delCopy)
        i = i + 1
    Wend
    ReDim _Preserve StorageArray(UBound(StorageArray) - 1)
End Sub

$If PTRTOSTR = UNDEFINED Then
    $Let PTRTROSTR = DEFINED
    Function PointerToWideString$ (pointer As _Offset)
        $If WCSLEN = UNDEFINED Then
            $Let WCSLEN = DEFINED
            Declare CustomType Library
                Function wcslen%& (ByVal str As _Offset)
            End Declare
        $End If
        Declare CustomType Library
            Sub StringCchCopyW (ByVal pszDest As _Offset, ByVal cchDest As _Offset, ByVal pszSrc As _Offset)
        End Declare
        Dim As _Offset length: length = wcslen(pointer) * 2 'The length does not account for the 2-byte nature of Unicode so we multiply by 2
        Dim As String __dest: __dest = Space$(length)
        StringCchCopyW _Offset(__dest), Len(__dest), pointer
        PointerToWideString = __dest
    End Function

    Function PointerToString$ (pointer As _Offset)
        Declare CustomType Library ".\internal\c\c_compiler\include\tchar"
            Function tcslen%& Alias "_tcslen" (ByVal str As _Offset)
        End Declare
        Declare Library ".\internal\c\c_compiler\include\strsafe"
        End Declare
        Declare CustomType Library
            Sub StringCchCopyA (ByVal pszDest As _Offset, ByVal cchDest As _Offset, ByVal pszSrc As _Offset)
        End Declare
        Dim As _Offset length: length = tcslen(pointer) + 1
        Dim As String __dest: __dest = Space$(length)
        StringCchCopyA _Offset(__dest), Len(__dest), pointer
        PointerToString = __dest
    End Function
$End If