Streams.c

/*
    File:       Streams.c
 
    Contains:   Utility routines to stream data into a block of memory
 
    Written by: Chris White 
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
 
#ifndef __TYPES__
    #include <Types.h>
#endif
 
#ifdef __MEMORY__
    #include <Memory.h>
#endif
 
#ifndef __ERRORS__
    #include <Errors.h>
#endif
 
 
#ifndef __STREAMS__
    #include "Streams.h"
#endif
 
 
 
 
 
OSErr NewStream ( tStreamRef* streamRef, Size blockSize )
{
    OSErr               theErr;
    Ptr                 thePtr;
    tStreamHeaderPtr    theHeader;
    
    
    *streamRef = nil;
    thePtr = NewPtr ( sizeof ( tStreamHeader ) + blockSize );
    theErr = MemError ( );
    if ( theErr )
        return theErr;
    
    theHeader = (tStreamHeaderPtr) thePtr;
    theHeader->blockSize = blockSize;
    theHeader->currentSize = sizeof ( tStreamHeader ) + blockSize;
    theHeader->cursor = sizeof ( tStreamHeader );
    theHeader->maxCursor = sizeof ( tStreamHeader );
    
    *streamRef = thePtr;
    
    return noErr;
}
 
 
 
OSErr DisposeStream ( tStreamRef streamRef )
{
    OSErr   theErr;
    
    DisposePtr ( streamRef );
    theErr = MemError ( );
    if ( theErr == memWZErr )
        return kInvalidStreamRef;
    
    return theErr;
}
 
 
 
OSErr SetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
{
    OSErr               theErr;
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    
    /* Allocate more memory if we need to */
    if ( theHeader->cursor + dataSize > theHeader->currentSize )
    {
        SetPtrSize ( streamRef, theHeader->currentSize + theHeader->blockSize );
        theErr = MemError ( );
        if ( theErr )
            return theErr;
        
        theHeader->currentSize += theHeader->blockSize;
    }
    
    if ( dataPtr )
        BlockMoveData ( dataPtr, streamRef + theHeader->cursor, dataSize );
    theHeader->cursor += dataSize;
    
    if ( theHeader->cursor > theHeader->maxCursor )
        theHeader->maxCursor = theHeader->cursor;
    
    return noErr;
}
 
 
 
OSErr GetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
{
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    if ( theHeader->cursor + dataSize > theHeader->maxCursor )
        return kBoundsErr;
        
    BlockMoveData ( streamRef + theHeader->cursor, dataPtr, dataSize );
    theHeader->cursor += dataSize;
    
    return noErr;
}
 
 
 
OSErr GetStreamDataPtr ( tStreamRef streamRef, Ptr* dataPtr )
{
    OSErr               theErr;
    Ptr                 thePtr;
    Size                theSize;
    tStreamHeaderPtr    theHeader;
    
    *dataPtr = nil;
    theHeader = (tStreamHeaderPtr) streamRef;
    theSize = GetStreamDataSize ( streamRef );
    
    thePtr = NewPtr ( theSize );
    theErr = MemError ( );
    if ( theErr )
        return theErr;
        
    BlockMoveData ( streamRef + sizeof ( tStreamHeader ), thePtr, theSize );
    *dataPtr = thePtr;
    
    return noErr;
}
 
 
 
Size GetStreamDataSize ( tStreamRef streamRef )
{
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    return theHeader->maxCursor - sizeof ( tStreamHeader );
}
 
 
 
tStreamCursor GetStreamCursor ( tStreamRef streamRef )
{
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    return theHeader->cursor;
}
 
 
 
OSErr SetStreamCursor ( tStreamRef streamRef, tStreamCursor cursor )
{
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    if ( cursor > theHeader->currentSize )
        return kBoundsErr;
    if ( cursor < sizeof ( tStreamHeader ) )
        return kBoundsErr;
        
    theHeader->cursor = cursor;
    
    return noErr;
}
 
 
 
void ResetStreamCursor ( tStreamRef streamRef )
{
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    theHeader->cursor = sizeof ( tStreamHeader );
    
    return;
}
 
 
 
OSErr CompactStream ( tStreamRef streamRef )
{
    OSErr               theErr;
    tStreamHeaderPtr    theHeader;
    
    theHeader = (tStreamHeaderPtr) streamRef;
    SetPtrSize ( streamRef, theHeader->maxCursor );
    theErr = MemError ( );
    if ( theErr == memWZErr )
        return kInvalidStreamRef;
    
    theHeader->currentSize = theHeader->maxCursor;
    
    return theErr;
}