home *** CD-ROM | disk | FTP | other *** search
- // TOKENS.SCP - Functions to convert a string to a list and a list to a string
- // Copyright (c) 2000, Vector Networks Limited
- // All Rights Reserved
- //
- // Revision History:
- // 5.3 20-Jul-00 AB - Created.
- // 03-Aug-00 DB - Finish off StrngList function.
- // 22-Aug-00 DB - Correct TokenList handling of a string with no delimiter
- // 6.0 09-Oct-00 DB - Add debug code to TokenList which fails when used
- // from HOSTNAME.SCP.
- // 18-Oct-00 DB - Change Do While loop in TokenList to Do Until.
- // - Comment out most debugging code.
- // 7.0 21-Mar-02 DB - Change Do Until loop start test in TokenList.
- // - Convert Do Until back to Do While
- // - Comment out most debugging code.
-
- // GetToken looks for the nth token in a string where the tokens
- // are separated by a caller-provided Delimiter (string).
-
- Function GetToken (Strng as String, Delimiter as String, TokenNumber as Integer) as String
- Dim Num, Pos, TempStrng
-
- Token = ""
-
- // Go though the string looking for the Delimiter. If it is not found,
- // there is only one Token in the string.
-
- For Num = 1 to TokenNumber
- Pos = InStr (Strng, Delimiter)
- If Pos > 0 then
- Token = Left (Strng, Pos - 1)
- Strng = Right (Strng, Len (Strng) - Pos + Len (Delimiter) - 1)
- Else
-
- // There are no more Delimiters in the string. If we have reached
- // the target Token, return the remaining string. Otherwise, the
- // requested Token is not present. Return an empty string.
-
- If Num < TokenNumber Then
- Token = ""
- Else
- Token = Strng
- Endif
- Exit For
- Endif
- Next
-
- // Return the selected string
-
- GetToken = Token
- End Function
-
- // This function takes a string and converts it into a list, splitting the
- // string by looking for a predefined Delimiter (string)
-
- $INCLUDE "LINSTR.SCP"
-
- Function TokenList (Strng as String, Delimiter as String) as List
- Dim Pos, TempList, TempStrng, Tokens
-
- // If the Delimiter is found in the string, the string up to the
- // Delimiter is put into the list, and then it and the Delimiter are
- // cut from the string. This continues until there are no more
- // Delimiters, after which the final part of the string is added.
-
- Print "Tokenising string: <", Strng, ">, Delimiter: <", Delimiter, ">"
- Tokens = 1
-
- // Separate the position calculation from the Do start test as
- // doing them both together provokes an access violation.
-
- Pos = LInStr (Strng, Delimiter)
-
- // If (Pos > 0) Then
- Do While Pos > 0
- // Do Until Pos = 0
- // Print "Extracting token ", Tokens, " Delimiter <", Delimiter, ">"
- TempStrng = Left (Strng, Pos - 1)
- Strng = Right (Strng, Len (Strng) - Pos)
- Print "Token ", Tokens,": <", TempStrng, "> Remainder: <", Strng, ">"
- AddItem (TempList, TempStrng)
- Tokens = Tokens + 1
- Pos = LInStr (Strng, Delimiter)
- Loop
- // Endif
-
- Print "Token ", Tokens,": <", Strng, ">"
- AddItem (TempList, Strng)
- Print Tokens, " Tokens found"
-
- // The temporary list is assigned to the return variable.
-
- TokenList = TempList
- End Function
-
- // StringList returns a string containing all of the items in a
- // List, with each separated by the string Delimiter
-
- Function StrngList (Lst as List, Delimiter as String) as String
- Dim Item, TempStr
-
- TempStr = ""
-
- For Each Item in Lst
- If TempStr = "" Then
- TempStr = Item
- Else
- TempStr = TempStr + Delimiter + Item
- Endif
- Next
-
- StrngList = TempStr
- End Function
-