home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code2 / tag_env / strtok.bas < prev    next >
BASIC Source File  |  1992-09-23  |  3KB  |  108 lines

  1.  
  2. '   STRTOK.BAS
  3.  
  4. '   Implementation of the C runtime library function StrTok(),
  5. '   a string tokenizer.
  6. '
  7. '   When you call the routine the first time for a string, you
  8. '   pass the text string as the first argument, and in subsequent
  9. '   calls for the other tokens in the string you signal that you
  10. '   want more tokens from the same string by passing a null
  11. '   string as the first argument. When you do this, StrTok
  12. '   continues to work on the copy that it saved the first time
  13. '   you called it.
  14. '
  15. '   For all calls, the second argument is a string consisting
  16. '   of the delimiter characters.  They don't have to be the same
  17. '   for all calls, but there must be at least one delimiter char
  18. '   in the string every time you call.  The basic technique is:
  19. '
  20. '   text$ = "some text string, with embedded delimiters"
  21. '   delims$ = " ,"   'in this case, one blank character
  22. '                    'and a comma
  23. '
  24. '   token$ = StrTok$(text$, delims$)
  25. '   Do While token$ <> ""
  26. '       'do something with the token...
  27. '       token$ = StrTok$("", delims$)
  28. '   Loop
  29. '
  30. '   In this case, each of the words would be returned, one at
  31. '   a time, until the string was exhausted, and StrTok$ would
  32. '   return a null string to show that there was no more.
  33. '
  34. '   StrTok$ is not reentrant; so you can't nest StrTok loops
  35. '   inside one another; when you pass a non=null string as the
  36. '   first argument, the function forgets about any previous
  37. '   string.  Likewise, if in the first call the string you pass
  38. '   happens to be a null string, the behavior is unpredictable,
  39. '   and depends on whatever use you had previously made of the
  40. '   function. If there was an unfinished string in its static
  41. '   storage, you'd probably get something off that string.
  42. '
  43. '   Also, this routine will do you no good if you have nested
  44. '   delimiters, such as ((a + b) * c); you need a better parser
  45. '   with some look-ahead capability to parse strings like this.
  46. '   But for simple cases, it works just fine.
  47. '
  48.     
  49.     DefInt A-Z
  50.  
  51. Function StrTok$ (Srce$, Delim$)
  52. '
  53. '   Visual Basic implementation of the C function Strtok().
  54. '
  55. '   Input:
  56. '       Srce$  - text string to be parsed (first time)
  57. '                a null string (subsequent calls)
  58. '       Delim$ - string of delimiter characters
  59. '
  60. '   Function return:    next token, or null string if no more
  61. '
  62. '   Restrictions:       not reentrant
  63. '
  64.  
  65.     Static Start%, SaveStr$
  66.  
  67.     ' If first call, make a copy of the string.
  68.  
  69.     If Srce$ <> "" Then
  70.         Start% = 1
  71.         SaveStr$ = Srce$
  72.     End If
  73.  
  74.     BegPos% = Start%
  75.     Ln% = Len(SaveStr$)
  76.  
  77.     ' Look for start of a token (character that isn't delimiter).
  78.  
  79.     Do While (BegPos% <= Ln%) And (InStr(Delim$, Mid$(SaveStr$, BegPos%, 1)) <> 0)
  80.         BegPos% = BegPos% + 1
  81.     Loop
  82.  
  83.     ' Test for token start found.
  84.  
  85.     If BegPos% > Ln% Then
  86.  
  87.         StrTok$ = ""                     'no more string to search
  88.  
  89.     Else
  90.  
  91.         ' Find the end of the token.
  92.  
  93.         EndPos% = BegPos%
  94.         While EndPos% <= Ln% And InStr(Delim$, Mid$(SaveStr$, EndPos%, 1)) = 0
  95.             EndPos% = EndPos% + 1
  96.         Wend
  97.  
  98.         StrTok$ = Mid$(SaveStr$, BegPos%, EndPos% - BegPos%)
  99.  
  100.         ' Set starting point for search for next token.
  101.  
  102.         Start% = EndPos%
  103.  
  104.     End If
  105.  
  106. End Function
  107.  
  108.