home *** CD-ROM | disk | FTP | other *** search
- Option Explicit
-
- Global Separators$ ' Separators between tokens
-
- ' State for parsing
- ' 0 = Normal
- ' 1 = In quote
- Dim ParseState%
-
- ' Location of special characters in string
- Const idComment% = 4 ' ' character
- Const idSpace% = 1 ' Space character
- Const IdTab% = 6 ' Tab character
- Const idQuote% = 7 ' " character
-
- '
- ' Returns the position of the next separator.
- ' Valid separator is any separator unless ParseState% is 1, in which
- ' case only a quote is valid.
- ' TokenId% is the location of the token in the separator string
- '
- Function FindSeparator% (InputString$, TokenId%)
- Dim InputLength%
- Dim c%
-
- If ParseState% = 1 Then ' Find next quote only
- c% = InStr(InputString$, Chr$(34))
- If c% > 0 Then
- TokenId% = InStr(Separators$, Chr$(34))
- End If
- FindSeparator = c%
- Exit Function
- End If
-
- InputLength% = Len(InputString$)
- For c% = 1 To InputLength%
- TokenId% = InStr(Separators$, Mid$(InputString$, c%, 1))
- Select Case TokenId%
- Case 0 ' Ignore - it's not a separator
-
- Case Else
- ' This character is a separator
- FindSeparator = c%
- Exit Function
- End Select
-
- Next c%
-
- FindSeparator = 0
- TokenId% = 0
-
- End Function
-
- '
- ' Retrieves the left token from the input string, modifies
- ' the input string to remove the token. Ignores white space
- ' result% = 0 - on success, 1 - Missing close quote
- '
- Function GetToken1$ (instring$, result%)
- Dim maxlen%
- Dim Separatorloc%, Quote2%
- Dim TokenId%
- Dim Quote2Loc%
- result% = 0
- maxlen% = Len(instring$)
- If maxlen% = 0 Then ' Empty string
- GetToken1$ = instring$
- result% = 0
- End If
-
- Separatorloc% = FindSeparator(instring$, TokenId%)
- Select Case Separatorloc%
- Case 0 ' Just retrieve through the rest of the line
- GetToken1 = instring$
- instring$ = ""
-
- Case 1 ' First char is a separator
- ' Special cases are comment and quote
- Select Case TokenId%
- Case idQuote% ' Return quoted string as a single token
- ParseState% = 1
- Quote2% = FindSeparator(Mid$(instring$, 2), TokenId%) + 1
- ParseState% = 0
- If Quote2% <= 1 Then
- result% = 1
- Exit Function
- Else
- GetToken1$ = Left$(instring$, Quote2%)
- If maxlen% > Quote2% Then instring$ = Mid$(instring$, Quote2% + 1) Else instring$ = ""
- Exit Function
- End If
- Case idComment% ' Return comment as a single token
- GetToken1$ = instring$
- instring$ = ""
- Case idSpace%, IdTab% ' It's whitespace
- If Left$(instring$, 1) = Chr$(9) Then
- ' Strip off the tab
- If Len(instring$) > 1 Then instring$ = Mid$(instring$, 2) Else instring$ = ""
- End If
- instring$ = LTrim$(instring$) ' And any extra spaces
- If Len(instring$) > 0 Then
- ' Recursive call on rest of string
- GetToken1 = GetToken1(instring$, TokenId%)
- Else
- GetToken1$ = ""
- instring$ = ""
- End If
- Case Else
- ' All other separators are tokens
- GetToken1$ = Left$(instring$, 1)
- If maxlen% > 1 Then
- instring$ = Mid$(instring$, 2)
- Else
- instring$ = ""
- End If
-
- End Select
-
-
- Case Else ' First char is not a separator
- ' Return the string up to the separator
- GetToken1$ = UCase$(Left$(instring$, Separatorloc% - 1))
- instring$ = Mid$(instring$, Separatorloc%)
- End Select
-
-
-
-
- End Function
-
- '
- ' Initialize the constants
- '
- Sub Initialize ()
- ' Characters that separate the tokens
- Separators$ = " ()'," & Chr$(9) & Chr$(34) & "%#$!@&=/:"
- ' Note: chr$(9) is the tab and chr$(34) is a "
-
- End Sub
-
-