home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Demo / PCDUO / data1.cab / Script_Samples / TOKENS.SCP < prev    next >
Encoding:
Text File  |  2003-11-28  |  3.7 KB  |  114 lines

  1. // TOKENS.SCP - Functions to convert a string to a list and a list to a string
  2. // Copyright (c) 2000, Vector Networks Limited
  3. // All Rights Reserved
  4. //
  5. // Revision History:
  6. // 5.3 20-Jul-00 AB  - Created.
  7. //     03-Aug-00 DB  - Finish off StrngList function.
  8. //     22-Aug-00 DB  - Correct TokenList handling of a string with no delimiter
  9. // 6.0 09-Oct-00 DB  - Add debug code to TokenList which fails when used
  10. //                     from HOSTNAME.SCP.
  11. //     18-Oct-00 DB  - Change Do While loop in TokenList to Do Until.
  12. //                   - Comment out most debugging code.
  13. // 7.0 21-Mar-02 DB  - Change Do Until loop start test in TokenList.
  14. //                   - Convert Do Until back to Do While
  15. //                   - Comment out most debugging code.
  16.  
  17. //  GetToken looks for the nth token in a string where the tokens 
  18. //  are separated by a caller-provided Delimiter (string).
  19.  
  20. Function GetToken (Strng as String, Delimiter as String, TokenNumber as Integer) as String
  21.     Dim Num, Pos, TempStrng
  22.  
  23.     Token = ""
  24.  
  25.     // Go though the string looking for the Delimiter. If it is not found,
  26.     // there is only one Token in the string.
  27.  
  28.     For Num = 1 to TokenNumber
  29.         Pos = InStr (Strng, Delimiter)
  30.         If Pos > 0 then
  31.             Token = Left (Strng, Pos - 1)
  32.             Strng = Right (Strng, Len (Strng) - Pos + Len (Delimiter) - 1)
  33.         Else
  34.  
  35.             // There are no more Delimiters in the string. If we have reached 
  36.             //  the target Token, return the remaining string. Otherwise, the 
  37.             //  requested Token is not present. Return an empty string.
  38.  
  39.             If Num < TokenNumber Then
  40.                 Token = ""
  41.             Else
  42.                 Token = Strng
  43.             Endif
  44.             Exit For
  45.         Endif
  46.     Next
  47.     
  48.     // Return the selected string
  49.  
  50.     GetToken = Token
  51. End Function
  52.  
  53. // This function takes a string and converts it into a list, splitting the 
  54. // string by looking for a predefined Delimiter (string)
  55.  
  56. $INCLUDE "LINSTR.SCP"
  57.  
  58. Function TokenList (Strng as String, Delimiter as String) as List
  59.   Dim Pos, TempList, TempStrng, Tokens
  60.  
  61.   // If the Delimiter is found in the string, the string up to the 
  62.   // Delimiter is put into the list, and then it and the Delimiter are 
  63.   // cut from the string. This continues until there are no more 
  64.   // Delimiters, after which the final part of the string is added.
  65.  
  66.   Print "Tokenising string: <", Strng, ">, Delimiter: <", Delimiter, ">"
  67.   Tokens = 1
  68.  
  69.   //  Separate the position calculation from the Do start test as
  70.   //  doing them both together provokes an access violation.
  71.  
  72.   Pos = LInStr (Strng, Delimiter)
  73.  
  74. //  If (Pos > 0) Then
  75.     Do While Pos > 0
  76. //  Do Until Pos = 0
  77. //   Print "Extracting token ", Tokens, " Delimiter <", Delimiter, ">"
  78.       TempStrng = Left (Strng, Pos - 1)
  79.       Strng = Right (Strng,  Len (Strng) - Pos)
  80.       Print "Token ", Tokens,": <", TempStrng, "> Remainder: <", Strng, ">"
  81.       AddItem (TempList, TempStrng)
  82.       Tokens = Tokens + 1
  83.       Pos = LInStr (Strng, Delimiter)
  84.     Loop
  85. //  Endif
  86.  
  87.   Print "Token ", Tokens,": <", Strng, ">"
  88.   AddItem (TempList, Strng)
  89.   Print Tokens, " Tokens found"
  90.  
  91.   // The temporary list is assigned to the return variable.
  92.  
  93.   TokenList = TempList
  94. End Function
  95.  
  96. //  StringList returns a string containing all of the items in a
  97. //  List, with each separated by the string Delimiter
  98.  
  99. Function StrngList (Lst as List, Delimiter as String) as String
  100.     Dim Item, TempStr
  101.  
  102.     TempStr = ""
  103.  
  104.     For Each Item in Lst
  105.         If TempStr = "" Then
  106.             TempStr = Item
  107.         Else
  108.             TempStr = TempStr + Delimiter + Item
  109.         Endif
  110.     Next
  111.  
  112.     StrngList = TempStr
  113. End Function
  114.