home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 23.2 KB | 669 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWStrmF.cpp
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWSTRMF_H
- #include "FWStrmF.h"
- #endif
-
- #ifndef FWPRISTR_H
- #include "FWPriStr.h"
- #endif
-
- #ifndef FWMATH_H
- #include "FWMath.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #pragma segment FWStream
-
- //========================================================================================
- // CLASS FW_CReadableStreamFormatter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::FW_CReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStreamFormatter::FW_CReadableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::~FW_CReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStreamFormatter::~FW_CReadableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadBytes
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadBytes(FW_CSink& sink,
- void* destination,
- long count)
- {
- Identity(sink, destination, count, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadChars(FW_CSink& sink,
- char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadSignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadSignedChars(FW_CSink& sink,
- signed char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(signed char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedChars(FW_CSink& sink,
- unsigned char* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadNullTerminatedString(FW_CSink& sink,
- char* destination)
- {
- long totalBytesCopied = 0;
- long availableReadBytes;
- const char * source = (const char *)sink.ReadPeek(availableReadBytes);
- long i;
- char copyChar;
- FW_Boolean foundTerminator = FALSE;
-
- // Read sink until null terminator found or no bytes to read.
- while (!foundTerminator && availableReadBytes)
- {
- // Copy all characters until null-terminator found or all available bytes copied.
- for (i = 0; !foundTerminator && (i < availableReadBytes); ++i)
- {
- copyChar = source[i];
- *destination++ = copyChar; // Note: null-terminator is copied too.
- if (copyChar == '\0')
- foundTerminator = TRUE;
- }
-
- // Advance sink position by number of characters processed.
- sink.ReadPeekAdvance(i);
-
- // If null-terminator not found, advance position in sink.
- if (!foundTerminator)
- {
- totalBytesCopied += availableReadBytes;
- source = (const char *)sink.ReadPeek(availableReadBytes);
- }
- }
-
- if (!foundTerminator)
- FW_THROW(FW_XReadableStreamFormatter(0, 1, totalBytesCopied));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadInts(FW_CSink& sink,
- int* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedInts(FW_CSink& sink,
- unsigned int* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadShorts(FW_CSink& sink,
- short* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedShorts(FW_CSink& sink,
- unsigned short* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadLongs(FW_CSink& sink,
- long* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadUnsignedLongs(FW_CSink& sink,
- unsigned long* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(unsigned long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadFloats
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadFloats(FW_CSink& sink,
- float* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(float));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadDoubles(FW_CSink& sink,
- double* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(double));
- }
-
- #ifdef FW_COMPILER_SUPPORTS_LONG_DOUBLE
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::ReadLongDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::ReadLongDoubles(FW_CSink& sink,
- long double* destination,
- long count)
- {
- Identity(sink, destination, count, sizeof(long double));
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CReadableStreamFormatter::Identity
- //----------------------------------------------------------------------------------------
-
- void FW_CReadableStreamFormatter::Identity(FW_CSink& sink,
- void* destination,
- long count,
- long itemSize)
- {
- char* currentDestination = (char*) destination;
- long totalBytesToRead = count * itemSize;
- long bytesLeftToRead = totalBytesToRead;
- long bytesAvailable = sink.GetReadableBytes();
- long bytesToReadThisTime = FW_Minimum(bytesLeftToRead, bytesAvailable);
-
- while (bytesToReadThisTime)
- {
- sink.Read(currentDestination, bytesToReadThisTime);
- currentDestination += bytesToReadThisTime;
- bytesLeftToRead -= bytesToReadThisTime;
- bytesAvailable = sink.GetReadableBytes();
- bytesToReadThisTime = FW_Minimum(bytesLeftToRead, bytesAvailable);
- }
-
- if (bytesLeftToRead)
- FW_THROW(FW_XReadableStreamFormatter(count, itemSize, (totalBytesToRead - bytesLeftToRead) / itemSize));
- }
-
-
- //======================================================================================
- // class FW_CSwapBytesFormatter
- //======================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::FW_CSwapBytesFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CSwapBytesFormatter::FW_CSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::~FW_CSwapBytesFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CSwapBytesFormatter::~FW_CSwapBytesFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadInts
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadInts(FW_CSink& sink,
- int* destination,
- long count)
- {
- if (sizeof(int) == sizeof(short))
- ReadShorts(sink, (short*)destination, count);
- else if (sizeof(int) == sizeof(long))
- ReadLongs(sink, (long*)destination, count);
- else
- FW_ASSERT(FALSE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadUnsignedInts(FW_CSink& sink,
- unsigned int* destination,
- long count)
- {
- ReadInts(sink, (int*)destination, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadShorts(FW_CSink& sink,
- short* destination,
- long count)
- {
- FW_CReadableStreamFormatter::ReadShorts(sink, destination, count);
-
- short* p = destination;
- while (p < destination + count)
- {
- char* s = (char*)p;
- char c = s[0];
- s[0] = s[1];
- s[1] = c;
-
- p++;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadUnsignedShorts(FW_CSink& sink,
- unsigned short* destination,
- long count)
- {
- ReadShorts(sink, (short*)destination, count);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadLongs(FW_CSink& sink,
- long* destination,
- long count)
- {
- FW_CReadableStreamFormatter::ReadLongs(sink, destination, count);
-
- long* p = destination;
- while (p < destination + count)
- {
- char* s = (char*)p;
- char c = s[0];
- s[0] = s[3];
- s[3] = c;
- c = s[1];
- s[1] = s[2];
- s[2] = c;
-
- p++;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSwapBytesFormatter::ReadUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CSwapBytesFormatter::ReadUnsignedLongs(FW_CSink& sink,
- unsigned long* destination,
- long count)
- {
- ReadLongs(sink, (long*)destination, count);
- }
-
-
- //========================================================================================
- // CLASS FW_CWritableStreamFormatter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::FW_CWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStreamFormatter::FW_CWritableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::~FW_CWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStreamFormatter::~FW_CWritableStreamFormatter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteBytes
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteBytes(FW_CSink& sink,
- const void* source,
- long count)
- {
- Identity(sink, source, count, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteChars(FW_CSink& sink,
- const char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteSignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteSignedChars(FW_CSink& sink,
- const signed char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(signed char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedChars
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedChars(FW_CSink& sink,
- const unsigned char* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned char));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteNullTerminatedString(FW_CSink& sink,
- const char* source)
- {
- Identity(sink, source, FW_PrimitiveStringLength(source) + 1, 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteInts(FW_CSink& sink,
- const int* source,
- long count)
- {
- Identity(sink, source, count, sizeof(int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedInts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedInts(FW_CSink& sink,
- const unsigned int* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned int));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteShorts(FW_CSink& sink,
- const short* source,
- long count)
- {
- Identity(sink, source, count, sizeof(short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedShorts
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedShorts(FW_CSink& sink,
- const unsigned short* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned short));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteLongs(FW_CSink& sink,
- const long* source,
- long count)
- {
- Identity(sink, source, count, sizeof(long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteUnsignedLongs
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteUnsignedLongs(FW_CSink& sink,
- const unsigned long* source,
- long count)
- {
- Identity(sink, source, count, sizeof(unsigned long));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteFloats
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteFloats(FW_CSink& sink,
- const float* source,
- long count)
- {
- Identity(sink, source, count, sizeof(float));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteDoubles(FW_CSink& sink,
- const double* source,
- long count)
- {
- Identity(sink, source, count, sizeof(double));
- }
-
- #ifdef FW_COMPILER_SUPPORTS_LONG_DOUBLE
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::WriteLongDoubles
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::WriteLongDoubles(FW_CSink& sink,
- const long double* source,
- long count)
- {
- Identity(sink, source, count, sizeof(long double));
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CWritableStreamFormatter::Identity
- //----------------------------------------------------------------------------------------
-
- void FW_CWritableStreamFormatter::Identity(FW_CSink& sink,
- const void* source,
- long count,
- long itemSize)
- {
- const char* currentSource = (const char*) source;
- long totalBytesToWrite = count * itemSize;
- long bytesLeftToWrite = totalBytesToWrite;
- long bytesAvailable = sink.GetWritableBytes();
- long bytesToWriteThisTime = FW_Minimum(bytesLeftToWrite, bytesAvailable);
-
- while (bytesToWriteThisTime)
- {
- sink.Write(currentSource, bytesToWriteThisTime);
- currentSource += bytesToWriteThisTime;
- bytesLeftToWrite -= bytesToWriteThisTime;
- bytesAvailable = sink.GetWritableBytes();
- bytesToWriteThisTime = FW_Minimum(bytesLeftToWrite, bytesAvailable);
- }
-
- if (bytesLeftToWrite)
- FW_THROW(FW_XWritableStreamFormatter(count, itemSize, (totalBytesToWrite - bytesLeftToWrite) / itemSize));
- }
-
-
- //========================================================================================
- // CLASS FW_XReadableStreamFormatter
- //========================================================================================
-
- _FW_EXCEPTION_IMPLEMENT(FW_XReadableStreamFormatter,
- FW_XPrivException)
-
- //----------------------------------------------------------------------------------------
- // FW_XReadableStreamFormatter::FW_XReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XReadableStreamFormatter::FW_XReadableStreamFormatter(long itemsRequested,
- long itemSize,
- long itemsRead) :
- FW_XPrivException(),
- fItemsRequested(itemsRequested),
- fItemSize(itemSize),
- fItemsRead(itemsRead)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XReadableStreamFormatter::FW_XReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XReadableStreamFormatter::FW_XReadableStreamFormatter(const FW_XReadableStreamFormatter& formatter) :
- FW_XPrivException(),
- fItemsRequested(formatter.fItemsRequested),
- fItemSize(formatter.fItemSize),
- fItemsRead(formatter.fItemsRead)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XReadableStreamFormatter::~FW_XReadableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XReadableStreamFormatter::~FW_XReadableStreamFormatter()
- {
- }
-
-
- //========================================================================================
- // CLASS FW_XWritableStreamFormatter
- //========================================================================================
-
- _FW_EXCEPTION_IMPLEMENT(FW_XWritableStreamFormatter,
- FW_XPrivException)
-
- //----------------------------------------------------------------------------------------
- // FW_XWritableStreamFormatter::FW_XWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XWritableStreamFormatter::FW_XWritableStreamFormatter(long itemsRequested,
- long itemSize,
- long itemsWritten) :
- FW_XPrivException(),
- fItemsRequested(itemsRequested),
- fItemSize(itemSize),
- fItemsWritten(itemsWritten)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XWritableStreamFormatter::FW_XWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XWritableStreamFormatter::FW_XWritableStreamFormatter(const FW_XWritableStreamFormatter& formatter) :
- FW_XPrivException(),
- fItemsRequested(formatter.fItemsRequested),
- fItemSize(formatter.fItemSize),
- fItemsWritten(formatter.fItemsWritten
- )
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XWritableStreamFormatter::~FW_XWritableStreamFormatter
- //----------------------------------------------------------------------------------------
-
- FW_XWritableStreamFormatter::~FW_XWritableStreamFormatter()
- {
- }
-