GetToken


Bruce: The original GetToken in REMLINE worked exactly like the C strtok function. It follows a classic design for iteration functions that have to be called repeatedly until they report no more items to iterate. In this type of function, you call it with one set of arguments the first time, and then you call it with another set of arguments each subsequent time.


GetToken works the same way. The first time, you pass the string to be parsed; after that, you pass an empty string. It goes like this:

sSeparator = “, “ & sTab & sCrLf  
sToken = GetToken(sCommand, sSeparator)
Do While sToken <> sEmpty
‘ Do something with sToken
sToken = GetToken(sEmpty, sSeparator)
Loop

That’s the philosophy. Here’s the code:

Function GetToken1(sTarget As String, sSeps As String) As String

‘ Note that sSave and iStart must be static from call to call
‘ If first call, make copy of string
Static sSave As String, iStart As Integer
If sTarget <> sEmpty Then
iStart = 1
sSave = sTarget
End If

‘ Find start of next token
Dim iNew As Integer
iNew = StrSpan1(Mid$(sSave, iStart, Len(sSave)), sSeps)
If iNew Then
‘ Set position to start of token
iStart = iNew + iStart - 1
Else
‘ If no new token, return empty string
GetToken1 = sEmpty
Exit Function
End If

‘ Find end of token
iNew = StrBreak1(Mid$(sSave, iStart, Len(sSave)), sSeps)
If iNew Then
‘ Set position to end of token
iNew = iStart + iNew - 1
Else
‘ If no end of token, set to end of string
iNew = Len(sSave) + 1
End If
‘ Cut token out of sTarget string
GetToken1 = Mid$(sSave, iStart, iNew - iStart)
‘ Set new starting position
iStart = iNew

End Function