Option
Explicit
Private
Const
C_SOURCE
As
String
=
"LogFile"
Private
Const
E_INVALID_SEPERATOR
As
Long
= vbObjectError + 1
Private
Const
E_INVALID_POSITION
As
Long
= vbObjectError + 2
Private
m_colLogEntries
As
VBA.Collection
Private
m_strSeperator
As
String
Private
m_strFilename
As
String
Private
Sub
Class_Initialize()
Set
m_colLogEntries =
New
VBA.Collection
m_strSeperator =
","
End
Sub
Public
Function
ReadFromFile(Filename
As
String
)
As
Boolean
Dim
colLogEntries
As
VBA.Collection
Set
colLogEntries =
New
VBA.Collection
Dim
lngPosition
As
Long
Dim
QNr
As
Long
lngPosition = 1
QNr = FreeFile
Dim
objLogEntry
As
LogEntry
Dim
strLine
As
String
On
Error
GoTo
ErrHandler
Open Filename
For
Input
As
#QNr
Do
Until
EOF(QNr)
Set
objLogEntry =
New
LogEntry
objLogEntry.Position = lngPosition
Line Input #QNr, strLine
Call
objLogEntry.FromLineExpr(strLine,
Me
.Seperator)
Call
colLogEntries.Add(objLogEntry)
lngPosition = lngPosition + Len(strLine) + Len(vbNewLine)
Loop
Close #QNr
On
Error
GoTo
0
Set
m_colLogEntries = colLogEntries
m_strFilename = Filename
ReadFromFile =
True
Exit
Function
ErrHandler:
Debug.Print Err.Number &
":"
, Err.Description
Close #QNr
ReadFromFile =
False
End
Function
Public
Property
Get
Seperator()
As
String
Seperator = m_strSeperator
End
Property
Public
Property
Let
Seperator(RHS
As
String
)
If
Len(RHS) = 0
Then
Call
Err.Raise(E_INVALID_SEPERATOR, C_SOURCE,
"Seperator cannot be of length zero"
)
m_strSeperator = RHS
End
Property
Public
Property
Get
Entry(Index
As
Long
)
As
LogEntry
Set
Entry = m_colLogEntries(Index)
End
Property
Public
Property
Get
EntryCount()
As
Long
EntryCount = m_colLogEntries.Count
End
Property
Public
Sub
WriteEntry(LineExpr
As
String
,
Optional
Position
As
Long
= 0)
If
m_strFilename =
""
Then
Exit
Sub
End
If
Dim
QNr
As
Long
QNr = FreeFile
On
Error
GoTo
ErrHandler
If
Position <= 0
Then
Open m_strFilename
For
Append
As
#QNr
Else
Open m_strFilename
For
Append
As
#QNr
Seek #QNr, Position
End
If
Print #QNr, LineExpr
Close #QNr
If
Position > 0
Then
End
If
Exit
Sub
ErrHandler:
Close #QNr
Call
Err.Raise(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)
End
Sub