Char Buffer Class Module

This is a class module implementing a buffer of characters with useful features. May contain up to 2^31 characters, with size control, full/partial push, overrun/underrun events and auto-pop.

Properties

ActualSize: Actual size of the data stored in the buffer.
AutoPop: Enables auto-pop feature.
AutoPopString: String which will cause auto-pop.
AutoPopStringInclude: Whether to include the AutoPopString in the data which will be popped or not.
FreeSpace: Space left in buffer.

Methods

Flush: Clears buffer.
Pop, Push: Self-explaining.

Events

AutoPopEvent
BufferOverrun
BufferUnderrun

History

V1.0: Module version of the buffer of chars.

Acknowledgements

Thanks to <jatrini>, for his support.
Thanks to Alberto Vazquez Navarro, a good partner at programming.
Thanks to Developer Fusion, for their great job.

The Code

'' classCharBuffer   v1.1
'' ----------------------
''   By Carlos Ivan Conde Martin ([email protected])
''   (c)2002 Spain
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Class module implementing a buffer of characters with useful features.
'' May contain up tu 2^31 characters, with size control, full/partial push,
'' overrun/underrun events and auto-pop.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Use and modification of this class module is allowed as long as its name
'' and this commented header is left as it is. Please be respectful with
'' author's work.
'' Suggestions,reports and bugs are wellcome.
Option Explicit

'Buffer may contain up to 2^31 characters.
Dim Buffer As String
'Auto pop feature.
'No control needed for this.
Public AutoPop As Boolean
Public AutoPopString As String
Public AutoPopStringInclude As Boolean

'Events.
Public Event AutoPopEvent(ByVal Data As String)
Public Event BufferOverrun(ByVal Data As String)
Public Event BufferUnderrun(ByVal RealDataExtracted As Long)


Public Property Get ActualSize() As Long
'Returns actual size of the buffer.
   ActualSize = Len(Buffer)
End Property

Public Property Get FreeSpace() As Long
   FreeSpace = 2 ^ 31 - 1 - Len(Buffer)
End Property

Public Sub Flush()
   Buffer = ""
End Sub

Public Function Pop(Optional ByVal N As Long = 0) As String
'Takes N characters from buffer, or all of them if N<=0.
'If there's less than N characters in the buffer, this function
' will return the whole buffer.
   If N > 0 Then
       Pop = Left(Buffer, N)
       'Raise event in case of not enough data.
       If N > Len(Buffer) Then RaiseEvent BufferUnderrun(Len(Buffer))
       Buffer = Mid(Buffer, N)
   Else
       Pop = Buffer
       Buffer = ""
   End If
End Function

Public Function Push(ByVal Data As String) As Boolean
'Puts some data into the buffer.
Dim Found As Long, Found2 As Long
   If Len(Data) > FreeSpace Then
       'If there's not enough space to store data:
       RaiseEvent BufferOverrun(Data)
       Push = False
   Else
       'Else store the data asked.
       Buffer = Buffer + Data
       Push = True
       'Check if there has occurred an AutoPop event,
       'only if Autopop feature is enabled.
       If AutoPop Then
           Found = InStr(1, Buffer, AutoPopString, vbBinaryCompare)
           Do Until Found = 0
               'If AutoPopString is to be included. Else it's discarded.
               Found = Found - 1
               Found2 = Found + Len(AutoPopString)
               If AutoPopStringInclude Then Found = Found2
               'Pop automatically data.
               RaiseEvent AutoPopEvent(Left(Buffer, Found))
               Buffer = Mid(Buffer, Found2)
               'Search again.
               Found = InStr(1, Buffer, AutoPopString, vbBinaryCompare)
           Loop
       End If
   End If
End Function

Private Sub Class_Initialize()
   Flush
   'Default values
   AutoPop = False
   AutoPopString = ""
   AutoPopStringInclude = False
End Sub

You might also like...

Comments

Carlos Ivan Conde Martin

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous