Results 1 to 11 of 11

Thread: [RESOLVED] Help with Code

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] Help with Code

    I have got this code from a friend:

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class GameInfo
        Private Const NumberOfFields As Integer = 5
    
        ' Use properties in real code; I'm using fields for brevity.
        Public IpAddress As String
        Public Port As Integer
        Public Wins As Integer
        Public UserName As String
        Public TimeStamp As Integer
        Public Points As Integer
    
        Public Shared Function Parse(ByVal logLine As String) As GameInfo
            Dim info As New GameInfo
    
            Dim pattern As String = "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<port>\d+) (?<wins>\d+) (?<userName>.*) (?<timestamp>\d{10}) (?<points>\d+)"
    
            Dim lineMatch As Match = Regex.Match(logLine, pattern)
    
            If Not lineMatch.Success Then
                ' Invalid line, possibly return an error
                Return info
            End If
    
            ' Group 0 is always the entire capture
            If lineMatch.Groups.Count <> NumberOfFields + 1 Then
                ' Invalid line, possibly return an error
                Return info
            End If
    
            info.IpAddress = lineMatch.Groups("IP").Value
            info.Port = CInt(lineMatch.Groups("port").Value)
            info.Wins = CInt(lineMatch.Groups("wins").Value)
            info.UserName = lineMatch.Groups("userName").Value
            info.TimeStamp = CInt (lineMatch.Groups("timestamp").Value
            info.Points = CInt(lineMatch.Groups("points").Value)
    
            Return info
        End Function
    
        Public Overrides Function ToString() As String
            Return String.Format("{0} ({1}:{2}) had {3} wins and {4} points.", _
                                 UserName, IpAddress, Port, Wins, Points)
        End Function
    End Class
    I will use it for my project. What i want it to do is reading a txt file and read line by line and use this function to separate the lines into specific data. And the it should add a new item to listview detailed view for each line.

    The second thing he didn't help me with, but that wouldn't be so hard i think.

    Hope someone will look into this.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    can you post your text file, or an example of it?

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Help with Code

    Of course:

    Code:
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    ip:port wins username timestamp points
    Where the timestamp is always 10 numbers.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    i was thinking more along the lines of an example i could test it with

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Help with Code

    oh, sorry, i'll attach that!
    Attached Files Attached Files

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    try this:
    (i had to edit your text file slightly. i've reattached it)

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim lines() As String = IO.File.ReadAllLines("gungame.txt")
    3.     For x As Integer = 0 To lines.GetUpperBound(0)
    4.         Dim info As New GameInfo
    5.         info = info.Parse(lines(x))
    6.  
    7.         ListView1.Items.Add(New ListViewItem(New String() {info.IpAddress, info.Port.ToString, info.Wins.ToString, info.UserName, info.TimeStamp.ToString, info.Points.ToString}))
    8.     Next
    9. End Sub

    vb Code:
    1. Public Class GameInfo
    2.  
    3.     Public IpAddress As String
    4.     Public Port As Integer
    5.     Public Wins As Integer
    6.     Public UserName As String
    7.     Public TimeStamp As Integer
    8.     Public Points As Integer
    9.  
    10.     Public Function Parse(ByVal logLine As String) As GameInfo
    11.         Dim info As New GameInfo
    12.  
    13.         Dim rx As New Regex("(?<IP>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(\:(?<port>(\d+)))?\t(?<wins>(\d+))\t(?<userName>((.|\s)*))\t(?<timestamp>(\d{10}))\t(?<points>(\d+))")
    14.  
    15.         Dim lineMatch As Match = rx.Match(logLine)
    16.  
    17.         If Not lineMatch.Success Then
    18.             ' Invalid line, possibly return an error
    19.             Return info
    20.         End If
    21.  
    22.         If logLine = "" Then
    23.             ' Invalid line, possibly return an error
    24.             Return info
    25.         End If
    26.  
    27.         info.IpAddress = lineMatch.Groups("IP").Value
    28.         info.Port = CInt(Val(lineMatch.Groups("port").Value))
    29.         info.Wins = CInt(lineMatch.Groups("wins").Value)
    30.         info.UserName = lineMatch.Groups("userName").Value
    31.         info.TimeStamp = CInt(lineMatch.Groups("timestamp").Value)
    32.         info.Points = CInt(lineMatch.Groups("points").Value)
    33.  
    34.         Return info
    35.     End Function
    36.  
    37. End Class
    Attached Files Attached Files

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    looking at it again, i'd recommend changing Port + TimeStamp to strings, to avoid zero value Ports or truncated TimeStamps

  8. #8

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Help with Code

    Hmm, i have applied the code, but nothing happens when i click my button. I've attached my .vb file with the code. Have i made anything wrong?
    Attached Files Attached Files

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    you didn't use what i posted. gungame.txt needs to be in your bin/debug folder

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         Dim lines() As String = IO.File.ReadAllLines("gungame.txt")
    8.  
    9.         For x As Integer = 0 To lines.GetUpperBound(0)
    10.  
    11.             Dim info As New GameInfo
    12.  
    13.             info = info.Parse(lines(x))
    14.  
    15.  
    16.  
    17.             ListView1.Items.Add(New ListViewItem(New String() {info.IpAddress, info.Port, info.Wins.ToString, info.UserName, info.TimeStamp, info.Points.ToString}))
    18.  
    19.         Next
    20.  
    21.         MsgBox("Done!")
    22.  
    23.     End Sub
    24. End Class
    25.  
    26. Public Class GameInfo
    27.     Public IpAddress As String
    28.     Public Port As String
    29.     Public Wins As Integer
    30.     Public UserName As String
    31.     Public TimeStamp As String
    32.     Public Points As Integer
    33.     Public Function Parse(ByVal logLine As String) As GameInfo
    34.         Dim info As New GameInfo
    35.         Dim rx As New Regex("(?<IP>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(\:(?<port>(\d+)))?\t(?<wins>(\d+))\t(?<userName>((.|\s)*))\t(?<timestamp>(\d{10}))\t(?<points>(\d+))")
    36.         Dim lineMatch As Match = rx.Match(logLine)
    37.         If Not lineMatch.Success Then
    38.             ' Invalid line, possibly return an error
    39.             Return info
    40.         End If
    41.         If logLine = "" Then
    42.             ' Invalid line, possibly return an error
    43.             Return info
    44.         End If
    45.         info.IpAddress = lineMatch.Groups("IP").Value
    46.         info.Port = lineMatch.Groups("port").Value
    47.         info.Wins = CInt(lineMatch.Groups("wins").Value)
    48.         info.UserName = lineMatch.Groups("userName").Value
    49.         info.TimeStamp = lineMatch.Groups("timestamp").Value
    50.         info.Points = CInt(lineMatch.Groups("points").Value)
    51.         Return info
    52.     End Function
    53. End Class

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with Code

    just paste Class GameInfo after the end class in your form1 code.
    also as i said you need the text file in the same folder as the executable, which is your projects bin/debug folder. don't forget to set your listview view to details + add 6 columns to it.

  11. #11

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Help with Code

    WAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!
    It works! You're the ****ing man dude! +Reputation!
    Thank you SO much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width