#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    SplitStr, strArgs()


    Im having a problem with a few pieces of code, apparently they work in VB6 but it won't work in Python because they don't exist. Here is 3 VB6 functions which I've gotten help on to convert them into Python.
    Code:
    Public Sub ParseData(strData)
        'Parse a data string which has been received from Battle.net.
        'This should be called whenever a Winsock_DataArrival event
        'occurs, where strData is the received data string.
        
        Dim strStrings() As String, i
        
        'Sometimes Battle.net puts several strings into one line,
        'separated by CrLf's. Here we split and parse them.
        
        SplitStr strData, strStrings, vbCrLf
        For i = 1 To UBound(strStrings)
            Parse2 strStrings(i)
        Next i
    End Sub
    
    Private Sub Parse2(ByVal strData As String)
    On Error Resume Next
        'This subroutine does the actual parsing. It gets called
        'by the ParseData subroutine. There is no need to call it
        'from outside.
        
        Dim strData1, strData2, i
        Dim strArgs() As String
        
        'Sometimes, Battle.net puts a LineFeed character at the
        'beginning of strings. We need to trim it.
        If Left(strData, 1) = Chr(10) Then
            strData = Right(strData, Len(strData) - 1)
        End If
        
        'Check whether this is normal text, or an event
        If Val(Left(strData, 4)) < 1001 Or Val(Left(strData, 4)) > 3000 Then
            Event_Unknown strData
            Exit Sub
        End If
        
        'If this is an event, then we separate the arguments, and quoted text
        i = InStr(1, strData, Chr(34), vbBinaryCompare)
        If i <> 0 Then
            strData1 = Left(strData, i - 2)
            strData2 = Mid(strData, i + 1, Len(strData) - i - 1)
        Else
            strData1 = strData
            strData2 = ""
        End If
        SplitStr strData1, strArgs(), " "
        
        'Call Appropriate Event based on ID
        Select Case Val(strArgs(1))
            Case 1001: Event_User strArgs(3), strArgs(4), strArgs(5)
            Case 1002: Event_Join strArgs(3), strArgs(4), strArgs(5)
            Case 1003: Event_Leave strArgs(3), strArgs(4)
            Case 1004: Event_RecvWhisper strArgs(3), strArgs(4), strData2
            Case 1005: Event_Talk strArgs(3), strArgs(4), strData2
            Case 1006: Event_Broadcast strData2
            Case 1007: Event_Channel strData2
            Case 1009: Event_Flags strArgs(3), strArgs(4)
            Case 1010: Event_SendWhisper strArgs(3), strArgs(4), strData2
            Case 1013: Event_ChannelFull strData2
            Case 1014: Event_ChannelNotExist strData2
            Case 1015: Event_ChannelRestricted strData2
            Case 1016: Event_Info strData2
            Case 1018: Event_Info strData2
            Case 1019: Event_Error strData2
            Case 1023: Event_Emote strArgs(3), strArgs(4), strData2
            Case 2010: Event_Name strArgs(3)
            Case 3000: Event_Info strData2
        End Select
    End Sub
    
    Public Function SplitStr(ByVal OriginalString As String, ByRef ReturnArray() As String, ByVal Delimiter As String) As Long
        'Used to split a string by delimiters, into a dynamic array, and returns
        'the ubound of the array, i use this since vb5 doesnt have the split() function
        
        Dim sItem, lArrCnt
        Dim lLen, lPos
        
        lArrCnt = 0
        lLen = Len(OriginalString)
    
        Do
            lPos = InStr(1, OriginalString, Delimiter, vbTextCompare)
            If lPos <> 0 Then
                sItem = Left$(OriginalString, lPos - 1)
                OriginalString = Mid$(OriginalString, lPos + 1)
                If sItem <> "" Then
                    lArrCnt = lArrCnt + 1
                    If lArrCnt = 1 Then
                        ReDim ReturnArray(1 To lArrCnt) As String
                    Else
                        ReDim Preserve ReturnArray(1 To lArrCnt) As String
                    End If
                    ReturnArray(lArrCnt) = sItem
                End If
            End If
        Loop While lPos <> 0
    
        If OriginalString <> "" Then
            lArrCnt = lArrCnt + 1
            ReDim Preserve ReturnArray(1 To lArrCnt) As String
            ReturnArray(lArrCnt) = OriginalString
        End If
        
        SplitStr = lArrCnt
    End Function
    In Python, I have:

    Code:
    def Parse(strData):
        if strData.startswith('\n'):
            strData = strData[1:]
        if int(len(strData[:4])) < 1001 or int(len(strData[:4])) > 3000:
            print "["+mytime+"]An unknown error has occured."
        if '"' in strData:
            strData1, strData2 = strData.split('"', 1)
        else:
            strData1 = strData
            strData2 = ""
        SplitStr(strData1, strArgs(), " ")
        case = int(strArgs(1))
        if case == 1001:
            InChannel(strArgs(3), strArgs(4), strArgs(5))
        elif case == 1002:
            UserJoin(strArgs(3), strArgs(4), strArgs(5))
        elif case == 1003:
            UserLeave(strArgs(3), strArgs(4))
        elif case == 1004:
            RecvWhisper(strArgs(3), strArgs(4), strData2)
        elif case == 1005:
            UserTalk(strArgs(3), strArgs(4), strData2)
        elif case == 1006:
            Broadcast(strData2)
        elif case == 1007:
            Channel(strData2)
        elif case == 1010:
            SendWhisper(strArgs(3), strArgs(4), strData2)
        elif case == 1013:
            ChannelFull(strData2)
        elif case == 1014:
            ChannelNotExist(strData2)
        elif case == 1015:
            ChannelRestricted(strData2)
        elif case == 1016:
            Info(strData2)
        elif case == 1018:
            Info(strData2)
        elif case == 1019:
            BnetError(strData2)
        elif case == 1023:
            UserEmote(strArgs(3), strArgs(4), strData2)
        elif case == 2010:
            LoggedIn(strArgs(3))
        elif case == 3000:
            Info(strData2)
    def ParseData(strData):
        SplitStr(strData, strStrings, '\r\n')
        for item in strStrings:
            Parse(item)
    def SplitStr(OriginalString, Delimiter):
        array = OriginalString.split(Delimiter) + [OriginalString]
        return len(array), array
    I have no idea where strStrings comes from or strArgs comes from. The VB6 code I gave is the only place strStrings and strArgs are used, can anyone show me where they are getting created?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    You may want to read the python tutorial at the python website. It's an excellent start for learning python.
    In the meantime you may want to try this:
    Code:
    def Parse(strData):
        # remove whitespace from start and end of string
        strData = strData.strip()
    
        # check the error number
        if int(strData[:4]) < 1001 or int(strData[:4]) > 3000:
            print "["+mytime+"]An unknown error has occured."
            return
    
        # separate arguments and quoted text
        if '"' in strData:
            strData1, strData2 = strData.split('"', 1)
        else:
            strData1 = strData
            strData2 = ""
    
        # split string into arguments, first argument has index 0
        strArgs = strData1.split()
        
        # evaluate arguments
        case = int(strArgs[0])
        if case == 1001:
            InChannel(strArgs[2], strArgs[3], strArgs[4])
        elif case == 1002:
            UserJoin(strArgs[2], strArgs[3], strArgs[4])
        elif ...
            ...
    
    
    def ParseData(strData):
        # split data into lines
        strStrings = strData.splitlines()
    
        # loop over lines
        for item in strStrings:
            Parse(item)
    
    # we don't need a SplitStr function here
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I've read the main tutorial 3-4 times. It's not enough to learn something like this, someone from battle.net who's very good with Java/Python gave me the proper code for this. I'm going to use his and see how it works out.
    Update: I just got the proper code for this topic on another forum (mostly for battle.net).

IMN logo majestic logo threadwatch logo seochat tools logo