Ignore:
Timestamp:
Oct 17, 2001, 4:22:33 PM (24 years ago)
Author:
bird
Message:

Corrected buffering algorithm.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/common/kFile.cpp

    r5531 r7093  
    1 /* $Id: kFile.cpp,v 1.8 2001-04-17 00:26:10 bird Exp $
     1/* $Id: kFile.cpp,v 1.9 2001-10-17 14:21:10 bird Exp $
    22 *
    33 * kFile - Simple (for the time being) file class.
     
    556556 * @param       cbBuffer    Amount of bytes to write.
    557557 */
    558 BOOL            kFile::write(void *pvBuffer, long cbBuffer)
     558BOOL            kFile::write(const void *pv, long cb)
    559559{
    560560    if (fReadOnly)
     
    562562    else
    563563    {
    564         ULONG cbWrite;
    565         ULONG cbAddPost = 0;
    566 
    567564        /* buffered writes? */
    568565        if (pachBuffer != NULL)
    569         {   /* Buffered write!
    570              *      Init buffer if necessary.
    571              *      Check if all fits into current buffer.
    572              *          Update buffer and return.
    573              *      Check if some fits into the current buffer
    574              *          Start - update valid part of the buffer. Commit buffer.
    575              *          End   - update buffer. no write
    576              *          Not   - commit buffer.
     566        {
     567            ULONG cbWrite;
     568            ULONG cbAddPost = 0;
     569
     570            /*
     571             * New buffer algorithm.
     572             *  Init buffer if it's invalid.
     573             *  Loop until no more to write
     574             *      If All fits into current buffer Then
     575             *          Insert it. COMPLETED.
     576             *      If Start fits into the current buffer Then
     577             *          Insert it.
     578             *      Else If End fits into the current buffer Then
     579             *          Insert it.
     580             *      Else //nothing fit's into the buffer
     581             *          Commit the buffer.
     582             *          Initiate the buffer to the current offset.
     583             *          Insert as much as possible.
     584             *      Endif
     585             *  EndLoop
    577586             */
    578587            if (offBuffer == ~0UL)
    579588            {   /* Empty buffer at current virtual offset */
    580                 cbBufferValid = offVirtual;
    581                 offBuffer = 0;
     589                fBufferDirty = 0;
     590                cbBufferValid = 0;
     591                offBuffer = offVirtual;
    582592            }
    583593
    584             if (    offBuffer <= offVirtual
    585                 &&  offBuffer + this->cbBuffer > offVirtual + cbBuffer
    586                 )
    587             {   /* all fits into the buffer */
    588                 memcpy(&pachBuffer[offVirtual - offBuffer], pvBuffer, (size_t)cbBuffer);
    589                 fBufferDirty = TRUE;
    590                 if (cbBufferValid < offVirtual - offBuffer + cbBuffer)
    591                     cbBufferValid = offVirtual - offBuffer + cbBuffer;
    592                 offVirtual += cbBuffer;
    593                 return TRUE;
    594             }
    595             else if (   offBuffer <= offVirtual
    596                      && offBuffer + this->cbBufferValid > offVirtual
    597                      )
    598             {   /* start fits into the valid part of the buffer */
    599                 cbWrite = this->cbBuffer - (offVirtual - offBuffer);
    600                 memcpy(&pachBuffer[offVirtual - offBuffer], pvBuffer, (size_t)cbWrite);
    601                 fBufferDirty = TRUE;
    602                 if (cbBufferValid < offVirtual - offBuffer + cbWrite)
    603                     cbBufferValid = offVirtual - offBuffer + cbWrite;
    604                 pvBuffer = (char*)pvBuffer + cbWrite;
    605                 cbBuffer -= cbWrite;
    606                 offVirtual += cbWrite;
    607                 if (!bufferCommit())
    608                     return FALSE;
    609             }
    610             else if (   offBuffer < offVirtual + cbBuffer
    611                      && offBuffer + this->cbBuffer >= offVirtual + cbBuffer
    612                      )
    613             {   /* end fits into the buffer */
    614                 cbWrite = offVirtual + cbBuffer - offBuffer;
    615                 memcpy(pachBuffer, &((char*)pvBuffer)[offBuffer - offVirtual], (size_t)cbWrite);
    616                 fBufferDirty = TRUE;
    617                 if (cbBufferValid < cbWrite)
    618                     cbBufferValid = cbWrite;
    619                 cbBuffer -= cbWrite;
    620                 cbAddPost = cbWrite;
    621             }
    622             else if (   offVirtual + cbBuffer <= offBuffer
    623                      || offVirtual >= offBuffer + this->cbBufferValid
    624                      )
    625             {   /* don't fit into the buffer at all */
    626                 if (!bufferCommit())
    627                     return FALSE;
    628             }
    629 
    630             /* Set filepointer. */
    631             if (!position())
    632                 return FALSE;
    633 
    634             /* Write. */
    635             rc = DosWrite(hFile, pvBuffer, cbBuffer, &cbWrite);
    636             if (rc == NO_ERROR)
     594            while (cb > 0)
    637595            {
    638                 offVirtual = offReal += cbWrite;
    639                 if (cbAddPost == 0)
    640                 {   /* no post add; start empty buffer at current virtual offset .*/
    641                     offBuffer = offVirtual;
    642                     cbBufferValid = 0;
    643                     fBufferDirty = FALSE;
     596                if (    offBuffer <= offVirtual
     597                    &&  offBuffer + cbBufferValid >= offVirtual
     598                    &&  offBuffer + cbBuffer >= offVirtual + cb
     599                    )
     600                {   /* everything fits into the buffer */
     601                    memcpy(&pachBuffer[offVirtual - offBuffer], pv, cb);
     602                    if (cbBufferValid < cb + offVirtual - offBuffer)
     603                        cbBufferValid = cb + offVirtual - offBuffer;
     604                    offVirtual += cb + cbAddPost;
     605                    fBufferDirty = TRUE;
     606                    return TRUE;
     607                }
     608
     609                if (    offBuffer <= offVirtual
     610                    &&  offBuffer + cbBufferValid >= offVirtual
     611                    &&  offBuffer + cbBuffer < offVirtual
     612                    )
     613                {   /* start fits into the buffer */
     614                    cbWrite = cbBuffer - (offVirtual - offBuffer);
     615                    memcpy(&pachBuffer[offVirtual - offBuffer], pv, cbWrite);
     616                    cbBufferValid = cbBuffer;
     617                    offVirtual += cbWrite;
     618                    cb -= cbWrite;
     619                    pv = (char*)pv + cbWrite;
     620                }
     621                else if (   offBuffer > offVirtual
     622                         && offBuffer <= offVirtual + cb
     623                         && offBuffer + cbBuffer >= offVirtual + cb
     624                         )
     625                {   /* end fits into the buffer */
     626                    cbWrite = offVirtual + cb - offBuffer;
     627                    memcpy(pachBuffer, pv, cbWrite);
     628                    if (cbBufferValid <  cbWrite)
     629                        cbBufferValid = cbWrite;
     630                    cbAddPost += cbWrite;
     631                    cb -= cbWrite;
    644632                }
    645633                else
    646                     offVirtual += cbAddPost;
    647                 return TRUE;
    648             }
     634                {   /* don't fit anywhere... */
     635                    if (!bufferCommit())
     636                        return FALSE;
     637                    offBuffer = offVirtual;
     638                    cbWrite = cbBufferValid = cb > cbBuffer ? cbBuffer : cb;
     639                    memcpy(pachBuffer, pv, cbWrite);
     640                    cb -= cbWrite;
     641                    pv = (char*)pv + cbWrite;
     642                    offVirtual += cbWrite;
     643                }
     644                fBufferDirty = TRUE;
     645
     646
     647            }   /* loop */
     648            offVirtual += cbAddPost;
     649
     650            return TRUE;
    649651        }
    650652        else if (position())
     
    652654            ULONG   cbWrote;
    653655
    654             rc = DosWrite(hFile, pvBuffer, cbBuffer, &cbWrote);
     656            rc = DosWrite(hFile, (PVOID)pv, cb, &cbWrote);
    655657            if (rc == NO_ERROR)
    656658            {
Note: See TracChangeset for help on using the changeset viewer.