//************************************************************************************** // Filename: CUTextFileStream.cp // Copyright © 1999 Tomasz Kukielka. All rights reserved. // // Description: // //************************************************************************************** // Revision History: // Tuesday, May 4, 1999 - Original //************************************************************************************** #include "CUTextFileStream.h" CUTextFileStream::CUTextFileStream(const FSRef& inFileRef, SInt8 inPermissions) : ARefFork(inFileRef, inPermissions) { mByteOrder = kByteOrderUndefined; } CUTextFileStream::~CUTextFileStream() { } //the fork is already opened in constructor //this is a legacy logic, but still we need to interpret and skip BOM void CUTextFileStream::OpenDataForkForReading(Boolean isUnicode /*= kTextIsUnicode*/) { SetPosition(0); if(isUnicode == kTextIsNOTUnicode) { mByteOrder = kByteOrderUndefined; return; } //read BOM, take care not to invert it // ByteCount byteCount = ARefFork::Read( sizeof(UniChar), (void*)&mByteOrder, fsFromStart, 0) ; ByteCount byteCount = 0; OSStatus err = ::FSReadFork(mForkRefNum, fsFromStart, 0, sizeof(UniChar), (void*)&mByteOrder, &byteCount); if( (err == noErr) && (byteCount == sizeof(UniChar)) && (mByteOrder == kMacByteOrderChar) || (mByteOrder == kPCByteOrderChar) ) { ;//OK } else {//move back and start over - there is no byte-order char, we default to mac mByteOrder = kMacByteOrderChar; SetPosition(0); } } //the fork is already opened in constructor //this is a legacy logic, but still we need to put BOM void CUTextFileStream::OpenDataForkForWriting(Boolean isUnicode /*= kTextIsUnicode*/) { SetPosition(0); if(isUnicode == kTextIsNOTUnicode) { mByteOrder = kByteOrderUndefined; return; } //add byte order char at the begining mByteOrder = kMacByteOrderChar; ARefFork::Write( sizeof(UniChar), &mByteOrder, fsFromStart, 0); } //does not throw OSStatus CUTextFileStream::ReadData( SInt32 &ioCount, void *inBuffer, UInt16 inPosMode, SInt64 inOffset) { ByteCount readCount = 0; OSStatus err = ::FSReadFork(mForkRefNum, inPosMode, inOffset, ioCount, inBuffer, &readCount); ioCount = readCount; // ByteCount outCount = ARefFork::Read( inCount, inBuffer, inPosMode, inOffset); if( (err == noErr) && (ioCount > 0) && (mByteOrder == kPCByteOrderChar) ) { InvertByteOrder( (UniCharPtr)inBuffer, ioCount/sizeof(UniChar) ); } return err; } void CUTextFileStream::InvertByteOrder(UniCharPtr uniTextPtr, UniCharCount uniTextLen) { for(UInt32 i = 0; i< uniTextLen; i++) { uniTextPtr[i] = SwapHiLoBytes( uniTextPtr[i] ); } }