Good afternoon StackOverflow,
I've just signed up here - I've been using this site for ages, and it seems to always be the site to supply the answer so I decided to be part of things.
Without further ado, here is my question -
I am writing an API for LAN parties that me and a group have monthly to help sort things out on the scorekeeping side. My friend is writing the backend for it, and I'm writing the VB6 frontend. It's been a while since I wrote VB6, and I never wrote it as intensively as the grade of frontend I'm aiming for here.
The premise of the program is this - The backend will write events from the game we're playing to a text file in realtime - Which the frontend reads from in realtime. The part I'd like to enquire about at the moment is this -
I know you can read text files line-by-line in VB6. I want the program to 'listen' (so to speak) for certain buzzwords and use their defined 'Values' to affect variables. Here is a mock example of the kind of file it'll be reading -
******************
LANrealm Match Log
******************
Game: Call of Duty 4
Game Type: Team Deathmatch
Date: 01-Jan-2013
Time: 19:00:00
Players: Tramp
Roper
d00b
Pleb
Score Limit: 150
Event: Game Start
Event: Roper killed Pleb (M4A1) shots=5 Feet=2 Body=2 Head=1
Event: Tramp committed suicide (Fall damage)
Event: Tramp killed d00b (Grenade)
Event: Pleb said "I'm saying something"
Event: Pleb teamkilled d00b (G3) shots=3 Feet=0 Body=2 Head=1
Event: Game Finished
Winner: Roper
Roper: Kills=1,Deaths=0,Suicides=0,Teamkills=0
Tramp: Kills=1,Deaths=0,Suicides=1,Teamkills=0
Pleb: Kills=0,Deaths=0,Suicides=0,Teamkills=1
d00b: Kills=0,Deaths=0,Suicides=0,Teamkills=0
Well, I think just by looking at this you can tell what I want the program to pick out of that. It would be a lot easier if I just made it fully comma delimited, but I want to maintain readability of the raw text file. But yeah, just in case you didn't get it, I'd want the program to recognise that 'Roper' had 1 'Kill' and so on and so forth. An example code snippet would be great!
Thanks in advance, guys.
Here's a function you could use to load the contents of a file:
Public Function LoadFile(dFile As String) As String
Dim ff As Integer
On Error Resume Next
ff = FreeFile
Open dFile For Binary As #ff
LoadFile = Space(LOF(ff))
Get #ff, , LoadFile
Close #ff
End Function
Next, you want to split the output of that file. First, you will need to know what type of EOL termination character will be produced by the back-end. Assuming each line ends with a carriage return (13) and a line feed (10), you could use this code to store each line into a string array:
Dim lines() As String
lines = Split(LoadFile("LANrealm.log"), vbCrLf)
Finally, it's a matter of cycling through each line (using a For...Next loop) and look for whatever information you want to extract:
For i = 0 To Ubound(lines)
' Add here necessary logic to extract the information.
' Each line can be accessed by indexing the array as: lines(i)
Next
Hope this helps you get started...
To test the code:
NOTE: Make sure you overwrite any pre-existing code
Option Explicit
Private Sub Form_Load()
cDlg.DefaultExt = "txt"
cDlg.Filter = "Text Files|*.txt;*.log"
cDlg.ShowOpen
If cDlg.fileName <> "" Then AnalyzeFile .fileName
End Sub
Private Sub AnalyzeFile(fileName As String)
Dim fileContents As String
Dim lines() As String
Dim i As Integer
fileContents = LoadFile(fileName)
lines = Split(fileContents, vbCrLf)
For i = 0 To UBound(lines)
If InStr(1, lines(i), "event:", vbTextCompare) Then
MsgBox "Line #" & i & " contains the string 'event'" + vbCrLf + vbCrLf + lines(i)
End If
Next
End Sub
Private Function LoadFile(dFile As String) As String
Dim ff As Integer
On Error Resume Next
ff = FreeFile
Open dFile For Binary As #ff
LoadFile = Space(LOF(ff))
Get #ff, , LoadFile
Close #ff
End Function
Run the program and, when asked to supply a file, select one of the logs that will be generated by the back-end.
In this example, the program will tell you which lines contain "event information", such as "Event: Roper killed Pleb (M4A1) shots=5 Feet=2 Body=2 Head=1".