[Gambas-user] file question

James Hatridge James.Hatridge at ...17...
Fri Oct 13 20:20:58 CEST 2006


HI Christopher et al..

Thanks for the code! But there is no way in "heck" that I'm going to work 
coding on Friday 13th!

Thanks

JIM

On Friday 13 October 2006 02:50, Christopher Brian Jack wrote:
> On Thu, 12 Oct 2006, James Hatridge wrote:
> > Hi Christopher et al...
> >
> > Ok, count me a bit slow, I think I understand. But can you show me a bit
> > of code? (code I understand better) :)
>
> Okay snippets are inserted look through...
> This is NOT a thread-safe implementation!
>
> Also I may have botched the error reporting syntax royally.  I haven't
> used the exception handling in Gambas as of yet.
>
> > Thanks,
> >
> > JIM
> >
> > On Wednesday 11 October 2006 18:31, Christopher Brian Jack wrote:
> > > On Wed, 11 Oct 2006, James Hatridge wrote:
> > > > Hi all
> > > >
> > > > Is there anyway I can set up a buffer or file so that when its full
> > > > the oldest part is dropped and newst is kept?
> > > >
> > > > The idea I have is a music buffer that holds X minutes of audio when
> > > > the buffer is full it will push out the old and add the new. Is this
> > > > do-able?
> > > >
> > > > Thanks,
> > > >
> > > > JIM
> > >
> > > The solution to your problem is a ring buffer.
>
> <Create a new class file called RingBuffer>
>
> > > To use a ring buffer split your main buffer into sequential chunks,
>
> Private chunkSize As Integer
> Private chunkList As Variant
>
> > > maintaining a head and tail position in the buffer.  If the head and
> > > tail indices are zero-based (0 indicates the first chunk) then you can
> > > use x MOD NumberOfChunks to get the wraparound effect needed for the
> > > index arithmetic.  The explanation below uses MOD and hence assumes
> > > zero-based indices.
>
> Private head As Integer
> Private tail As Integer
> Private status As String
>
> > > When the buffer is initialized  head=0, tail=0, status="empty"
>
> 'put into class "init" or "constructor" sub
> head=0
> tail=0
> status="empty"
>
> 'NOTE: The init sub/ctor must also require a block size parameter and a
> 'block count parameter:
>
> Dim chunks(chunkCount) As Variant
> chunkList=chunks
>
> '
> 'It may be useful to be able to query the full/empty state of the
> '
> Public Property isFull() As Boolean
> Public Property isEmpty() As Boolean
>
> Public Sub isFull()
>   If Not (head=tail) Then Return FALSE
>   If status="full" Then Return TRUE
>   Return FALSE
> End
>
> Public Sub isEmpty()
>   If Not (head=tail) Then Return FALSE
>   If status="empty" Then Return TRUE
>   Return FALSE
> End
>
> > > 1.  To add to the buffer first make sure the status is not "full"
> > > (throw a football if it is) then increase the head with head=(head+1)
> > > MOD BufferChunkCount if this makes head=tail then set status to "full"
> > > and throw a football.  If head!=tail then you can go ahead and fill the
> > > chunk now indexed by head.
>
> Public Sub Push(sourceData As String)
>   If (status="full") Then Throw("Buffer Full")
>   head=(head+1) MOD chunkCount
>   If (head=tail) Then
>     status="full"
>     Throw("Buffer Full")
>   End
>   chunkList(head)=Mid(sourceData,1,Min(chunkSize,Len(sourceData)))
> End
>
> > > 2.  To pull from the buffer first check that the buffer state is not
> > > "empty" and throw a football if it is.  Transfer the chunk to whatever
> > > needs it then increase the tail by one with tail=(tail+1) MOD
> > > BufferChunkCount.  Appropriately setting the status to "empty" if the
> > > condition tail=head is true after increasing the tail.
>
> Public Sub Pull() As String
>   Dim data As String
>   If (status="empty") Then Throw("Buffer Empty")
>   data=chunkList(tail)
>   chunkList(tail)=NULL
>   tail=(tail+1) Mod chunkCount
>   If (head=tail) Then status="empty"
>   Return data;
> End
>
> > > 3. If the accesses to the buffer may be threaded then the head and tail
> > > check-and-change need to be done within semaphores or with a locking
> > > mechanism to make them atomic/serialized (only one thing accesses the
> > > head/tail indices at a time).  Head needs to bumped before writing the
> > > data and tail must only be bumped after the chunk is transferred to
> > > whatever is using the data.
>
> This is not a thread-safe example.
>
> .=================================================.
>
> |  Christopher BRIAN Jack aka "Gau of the Veldt"  |
>
> +================================================='
>
> | oevna at ...1544...
>
> `=================================================-
> Hi Spambots, my email address is sputnik at ...1334...
> Hi Humans, my email address uses rot13 cipher
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier Download IBM WebSphere Application Server v.1.0.1 based on Apache
> Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

-- 
Jim Hatridge
Linux User #88484
------------------------------------------------------     
              WartHog Bulletin
          Info about new German Stamps
   http://www.fuzzybunnymilitia.org/~hatridge/bulletin/index.php

            Viel Feind -- Viel Ehr'
      Anti-US Propaganda stamp collection
  http://www.fuzzybunnymilitia.org/~hatridge/collection/index.php




More information about the User mailing list