VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsATCScriptMid"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
'Copyright 2002 by AQUA TERRA Consultants
'##MODULE_DESCRIPTION The MID token returns a substring of a given string starting _
 at a specified character and extending a specified length. _
 The first argument is the string. The second argument is the starting _
 character. The third argument, which is optional, is the length of the returned _
 string. If the third argument is omitted or greater than the length of the _
 incoming string, the entire remaining portion of the string is returned. _
 See clsATCScript for documentation of procedures common to all classes implementing clsATCScript
'##MODULE_REMARKS <P>Examples:</P><P>(Mid "word" 2) = "ord"</P><P>(Mid _
 "word" 2, 6) = "ord"</P>

'##SUMMARY collection of arguments belonging to command token
Private pArgs As FastCollection
Private Const pName = "Mid"

'##INTERFACE_ALIAS clsATCScript ATCscriptBase~clsATCScript
Implements clsATCScript

Private pSerial As Long

'Serial number assigned in order of creation
'Guaranteed to be unique at runtime but will be different from one run to another
Private Property Get clsATCScript_Serial() As Long
  clsATCScript_Serial = pSerial
End Property

Private Sub Class_Initialize()
  pSerial = GlobalScriptManager.NextSerial
  Set pArgs = New FastCollection
End Sub

Private Property Get clsATCScript_Aliases() As FColl.FastCollection
  Set clsATCScript_Aliases = New FastCollection
  clsATCScript_Aliases.Add pName
End Property

Private Property Get clsATCScript_Args() As FastCollection
  Set clsATCScript_Args = pArgs
End Property

Private Function clsATCScript_AsText(Optional indent As String = "", Optional indentIncrement As String = "") As String
  clsATCScript_AsText = GlobalScriptManager.AsText(Me, 0, , indent, indentIncrement)
End Function

Private Property Get clsATCScript_Documentation() As String
  clsATCScript_Documentation = "(Set name (Mid name 2))" & vbCr & _
                               "removes the first character of name" & vbCr & _
                               "(Set Initial (Mid name 1 1))" & vbCr & _
                               "sets Initial to be the first character of name" & vbCr & _
                               "(Mid Name Start Length)" & vbCr & _
                               "Returns a substring of Name starting at character " & _
                               "Start (1 is the first character) continuing for " & _
                               "Length characters, or until the end of Name if " & _
                               "Length is omitted or is too large."
End Property

Private Function clsATCScript_Evaluate() As Variant
  Dim value As String
  Dim startpos As Variant
  Dim LenMid As Variant
  value = ""
  If pArgs.Count < 2 Then
    MsgBox "Mid needs at least a source string and start position" & vbCr & clsATCScript_AsText, vbOKOnly, "Script Evaluation"
  Else
    value = GlobalScriptManager.Evaluate(pArgs.ItemByIndex(1))
    startpos = GlobalScriptManager.Evaluate(pArgs.ItemByIndex(2))
    If Not IsNumeric(startpos) Then
      MsgBox "Mid needs numeric start position" & vbCr & clsATCScript_AsText, vbOKOnly, "Script Evaluation"
    Else
      If pArgs.Count > 2 Then
        LenMid = GlobalScriptManager.Evaluate(pArgs.ItemByIndex(3))
        If Not IsNumeric(LenMid) Then
          MsgBox "Mid needs numeric length" & vbCr & clsATCScript_AsText, vbOKOnly, "Script Evaluation"
        Else
          If startpos < 0 Then
            LenMid = LenMid + startpos - 1
            startpos = 1
          End If
          If LenMid > 0 Then value = Mid(value, startpos, LenMid)
        End If
      Else
        value = Mid(value, startpos)
      End If
    End If
  End If
  clsATCScript_Evaluate = value
End Function

Private Property Get clsATCScript_Name() As String
  clsATCScript_Name = pName
End Property

Private Sub clsATCScript_Parse(ByVal buf As String, ByVal ArgsStart As Long)
  GlobalScriptManager.ParseArgs pArgs, buf, ArgsStart
  If Not (pArgs.Count = 2 Or pArgs.Count = 3) Then GlobalScriptManager.Abort "Expected 2 arguments, but found " & pArgs.Count, Me
End Sub


