int TFRnd::GetFLen(){ int FPos=GetFPos(); EAssertR( fseek(FileId, 0, SEEK_END)==0, "Error seeking into file '"+TStr(FNm)+"'."); int FLen=GetFPos(); SetFPos(FPos); return FLen; }
static toff_t _tiffSeekProc(thandle_t fd, toff_t off, int whence) { long fpos, size; if (GetEOF((short) fd, &size) != noErr) return EOF; (void) GetFPos((short) fd, &fpos); switch (whence) { case SEEK_CUR: if (off + fpos > size) SetEOF((short) fd, off + fpos); if (SetFPos((short) fd, fsFromMark, off) != noErr) return EOF; break; case SEEK_END: if (off > 0) SetEOF((short) fd, off + size); if (SetFPos((short) fd, fsFromStart, off + size) != noErr) return EOF; break; case SEEK_SET: if (off > size) SetEOF((short) fd, off); if (SetFPos((short) fd, fsFromStart, off) != noErr) return EOF; break; } return (toff_t)(GetFPos((short) fd, &fpos) == noErr ? fpos : EOF); }
long file_seek(FILEH handle, long pointer, int method) { SInt32 pos; SInt32 setp; setp = pointer; switch(method) { case FSEEK_SET: break; case FSEEK_CUR: if (GetFPos(handle, &pos) != noErr) { return(-1); } setp += pos; break; case FSEEK_END: if (GetEOF(handle, &pos) != noErr) { return(-1); } setp += pos; break; default: return(-1); } SetFPos(handle, fsFromStart, setp); if (GetFPos(handle, &pos) != noErr) { return(-1); } return((long)pos); }
int TFIn::GetFLen() const { const int FPos=GetFPos(); EAssertR( fseek(FileId, 0, SEEK_END)==0, "Error seeking into file '"+GetSNm()+"'."); const int FLen=GetFPos(); SetFPos(FPos); return FLen; }
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque) { SInt16 ref = *((SInt16 *) opaque); SInt32 curPos; BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, -1); return((PHYSFS_sint64) curPos); } /* __PHYSFS_platformTell */
XERCES_CPP_NAMESPACE_BEGIN //---------------------------------------------------------------------------- // XMLMacCarbonFile methods //---------------------------------------------------------------------------- unsigned int XMLMacCarbonFile::currPos() { OSErr err = noErr; unsigned int pos = 0; if (!mFileValid) ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos); if (gHasHFSPlusAPIs) { SInt64 bigPos = 0; err = FSGetForkPosition(mFileRefNum, &bigPos); if (err == noErr) pos = bigPos; } else { long longPos; err = GetFPos(mFileRefNum, &longPos); if (err == noErr) pos = longPos; } if (err != noErr) ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos); return pos; }
int __PHYSFS_platformEOF(void *opaque) { SInt16 ref = *((SInt16 *) opaque); SInt32 eofPos, curPos; BAIL_IF_MACRO(oserr(GetEOF(ref, &eofPos)) != noErr, NULL, 1); BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, 1); return(curPos >= eofPos); } /* __PHYSFS_platformEOF */
INT64 NCSFileSeek(int hFile, INT64 nOffset, int origin) { #ifdef WIN32 return((INT64)_lseeki64(hFile, (__int64)nOffset, origin)); #elif defined MACINTOSH long fpos, size; if (GetEOF((short) hFile, &size) != noErr) return EOF; (void) GetFPos((short) hFile, &fpos); switch (origin) { case SEEK_CUR: if (nOffset + fpos > size) SetEOF((short) hFile, nOffset + fpos); if (SetFPos((short) hFile, fsFromMark, nOffset) != noErr) return EOF; break; case SEEK_END: if (nOffset > 0) SetEOF((short) hFile, nOffset + size); if (SetFPos((short) hFile, fsFromStart, nOffset + size) != noErr) return EOF; break; case SEEK_SET: if (nOffset > size) SetEOF((short) hFile, nOffset); if (SetFPos((short) hFile, fsFromStart, nOffset) != noErr) return EOF; break; } return (INT64)(GetFPos((short) hFile, &fpos) == noErr ? fpos : EOF); #else /* WIN32 */ #error: NCSFileSeek() #endif /* WIN32 */ }
TStr TFRnd::GetStr(const int& StrLen, bool& IsOk){ IsOk=false; TStr Str; if (GetFPos()+StrLen+1<=GetFLen()){ char* CStr=new char[StrLen+1]; GetBf(CStr, StrLen+1); if (CStr[StrLen+1-1]==TCh::NullCh){IsOk=true; Str=CStr;} delete[] CStr; } return Str; }
UINT file_write(FILEH handle, const void *data, UINT length) { if (length) { long size = length; if (FSWrite(handle, &size, (char *)data) == noErr) { return(size); } } else { SInt32 pos; if (GetFPos(handle, &pos) == noErr) { SetEOF(handle, pos); } } return(0); }
globle void GenTellBinary( void *theEnv, long *offset) { #if MAC GetFPos(SystemDependentData(theEnv)->BinaryRefNum,offset); #endif #if IBM_TBC || IBM_MSC || IBM_ICB /* || IBM_MCW || IBM_ZTC */ *offset = lseek(SystemDependentData(theEnv)->BinaryFileHandle,0,SEEK_CUR); #endif #if (! MAC) && (! IBM_TBC) && (! IBM_MSC) && (! IBM_ICB) /* && (! IBM_MCW) && (! IBM_ZTC) */ *offset = ftell(SystemDependentData(theEnv)->BinaryFP); #endif }
DWORD RIFFClass::RIFFTell() { switch (byWhereIsRIFFData) { case RIFF_ONDISK: return (ftell(pFile)); //break; #ifdef USE_MACINTOSH case RIFF_ONMACDISK: INT lPos = 0; GetFPos(fRefNum, &lPos); return lPos; //break; #endif default: return(1); } }
int TFRnd::GetRecN(){ IAssert(RecAct); int FPos=GetFPos()-HdLen; EAssertR(FPos%RecLen==0, "Invalid position in file'"+TStr(FNm)+"'."); return FPos/RecLen; }
NCSError NCSFileRead(NCS_FILE_HANDLE hFile, void *pBuffer, UINT32 nLength, UINT32* pRead) { #ifdef WIN32 BOOLEAN bError; DWORD nBytesRead; bError = (BOOLEAN)ReadFile( hFile, // handle of file to read pBuffer, // pointer to buffer that receives data nLength, // number of bytes to read &nBytesRead, // pointer to number of bytes read NULL); // pointer to structure for data if(pRead) { *pRead = (UINT32)nBytesRead; } if( bError != 0 && nBytesRead == nLength ) return( NCS_SUCCESS ); else return( NCS_FILE_IO_ERROR ); #elif defined MACINTOSH int iOReturn; long myLength; UINT32 theOffset; BOOLEAN result; result = GetFPos((short)hFile, (long *)&theOffset); myLength = nLength; iOReturn = FSRead((short)hFile, (long *)&nLength,pBuffer); switch (iOReturn) { case noErr: return NCS_SUCCESS; break; case eofErr: return ((myLength == nLength)?NCS_FILE_IO_ERROR:NCS_SUCCESS); break; case rfNumErr: return (NCS_FILE_IO_ERROR); break; default: return NCS_FILE_IO_ERROR; break; } #elif defined PALM Err eErr; INT16 iStartRecord = hFile->iOffset / hFile->nRecordSize + 1; INT16 iEndRecord = (hFile->iOffset + nLength) / hFile->nRecordSize + 1; INT16 iRecord = hFile->iOffset / hFile->nRecordSize + 1; INT64 nRead = 0; INT32 nToRead = nLength; for(iRecord = iStartRecord; iRecord <= iEndRecord; iRecord++) { if(hFile->iRecord != iRecord) { if(hFile->hRecord) { if(hFile->pData) { MemHandleUnlock(hFile->hRecord); hFile->pData = NULL; } DmReleaseRecord(hFile->dbRef, hFile->iRecord, false); hFile->iRecord = 0; hFile->hRecord = NULL; } hFile->iRecord = iRecord; if(hFile->hRecord = DmGetRecord(hFile->dbRef, hFile->iRecord)) { hFile->pData = MemHandleLock(hFile->hRecord); if(hFile->pData == NULL) { DmReleaseRecord(hFile->dbRef, hFile->iRecord, false); } } if(!hFile->hRecord || !hFile->pData) { return(NCS_FILE_IO_ERROR); } } if(hFile->pData) { INT32 nThisRead = MIN(nToRead, hFile->nRecordSize - (hFile->iOffset % hFile->nRecordSize)); memcpy((UINT8*)pBuffer + nRead, (void*)((UINT8*)hFile->pData + hFile->iOffset % hFile->nRecordSize), nThisRead); hFile->iOffset += nThisRead; nToRead -= nThisRead; nRead += nThisRead; } } if(nRead == nLength) { *pRead = nRead; return(NCS_SUCCESS); } /* nRead = FileRead(hFile, pBuffer, (Int32)(nLength & 0x7fffffff) , 1, &eErr); *pRead = (UINT32)nRead; return(NCSPalmGetNCSError(eErr)); */ #elif defined(POSIX) int nThisRead = read(hFile, (void*)pBuffer, (unsigned int)nLength); if(pRead) { *pRead = nThisRead; } if(nThisRead == nLength) { return(NCS_SUCCESS); } else { return(NCS_FILE_IO_ERROR); } #else /* SOLARIS||IRIX */ #error ERROR: EcwReadFile() routine is not defined for this platform #endif /* WIN32 */ }
NCSError NCSRemoveDir( const char *pDirName) { #ifdef LINUX if(rmdir(pDirName) == 0) { #elif defined(_WIN32_WCE) if(RemoveDirectory(OS_STRING(pDirName)) != 0) { #else if(rmdir(pDirName) == 0) { #endif return(NCS_SUCCESS); } else { return(NCS_FILEIO_ERROR); } } /* ** Seek to given position */ INT64 NCSFileSeekNative(NCS_FILE_HANDLE hFile, INT64 nOffset, NCS_FILE_ORIGIN origin) { #if defined WIN32 LARGE_INTEGER li; li.QuadPart = nOffset; li.LowPart = SetFilePointer(hFile, // file handle li.LowPart, // low 32 bits &li.HighPart, // high 32 bits origin); // relative to start of file if( li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) return( -1 ); else return( li.QuadPart ); #elif defined PALM switch(origin) { case NCS_FILE_SEEK_START: hFile->iOffset = nOffset; break; case NCS_FILE_SEEK_CURRENT: hFile->iOffset += nOffset; break; case NCS_FILE_SEEK_END: hFile->iOffset = hFile->nDBSize + nOffset - 1; } return(hFile->iOffset); #elif defined MACINTOSH UINT32 myOffset; int result; myOffset = U32SetU(nOffset); if(!myOffset) result = 0; result = SetFPos((short)hFile, fsFromStart, myOffset); if(!result) return myOffset; else return (0); #elif defined SOLARIS return (INT64) llseek(hFile, (offset_t)nOffset, origin); #elif defined LINUX return (INT64) lseek64(hFile, (__off64_t)nOffset, origin); #elif defined HPUX return (INT64) lseek64(hFile, (off64_t)nOffset, origin); #elif defined IRIX return (INT64) lseek64(hFile, (off64_t)nOffset, origin); #elif defined MACOSX return (INT64) lseek(hFile, nOffset, origin); #endif } /* ** Get current file position */ INT64 NCSFileTellNative(NCS_FILE_HANDLE hFile) { #if defined WIN32 LARGE_INTEGER li; li.QuadPart = 0; li.LowPart = SetFilePointer(hFile, // file handle li.LowPart, // low 32 bits &li.HighPart, // high 32 bits FILE_CURRENT); // relative to present position if( li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) return(-1); else return(li.QuadPart); #elif defined MACINTOSH long myOffset; OSErr result; result = GetFPos((short)hFile, &myOffset); if (result==noErr) return ((INT64)myOffset); else return ((INT64)-1); #elif defined PALM return(hFile->iOffset); #elif defined SOLARIS INT64 nOffset = llseek(hFile, (offset_t)0, SEEK_CUR); return(nOffset); #elif defined IRIX return((INT64)telli64(hFile)); #elif defined LINUX return (INT64) lseek64(hFile, (__off64_t)0, SEEK_CUR); #elif defined HPUX return (INT64) lseek64(hFile, (off64_t)0, SEEK_CUR); #elif defined MACOSX return (INT64) lseek(hFile, (long)0, SEEK_CUR); #else #error ERROR: EcwFileGetPos() routine is not defined for this platform #endif }
OSErr IsFlattenedResourceFile( ConstFSSpecPtr inFile, Boolean* outIsFlat) { OSErr err; CInfoPBRec pb; if (not inFile) { // This can occur when we create a new project document (Cmd-N) *outIsFlat = false; return noErr; } pb.hFileInfo.ioNamePtr = (StringPtr)inFile->name; pb.hFileInfo.ioVRefNum = inFile->vRefNum; pb.hFileInfo.ioDirID = inFile->parID; pb.hFileInfo.ioFDirIndex = 0; err = PBGetCatInfoSync(&pb); if (err == noErr) { if (pb.hFileInfo.ioFlAttrib & kioFlAttribDirMask) { // This is a directory *outIsFlat = false; return paramErr; } else { UInt32 dfSize; UInt32 rfSize; SInt16 dfRefNum; SInt32 filePos; dfSize = pb.hFileInfo.ioFlLgLen; rfSize = pb.hFileInfo.ioFlRLgLen; if (rfSize > 0) { *outIsFlat = false; } else if (dfSize == 0) { // This file has no data or resource fork. *outIsFlat = false; } else { // Only the data fork is non-empty. // Now we need to determine if it contains resources or not. UInt32 firstFourWords[4]; SInt32 byteCount; err = FSpOpenDF(inFile, fsRdPerm, &dfRefNum); if (err) return err; err = GetFPos(dfRefNum, &filePos); byteCount = sizeof(firstFourWords); err = FSRead(dfRefNum, &byteCount, &firstFourWords); if (err == noErr) { // Test is based on resource file format as described in IM: More Mac Toolbox // <http://developer.apple.com/techpubs/mac/MoreToolbox/MoreToolbox-99.html#HEADING99-0> // // First four words of the file represent the resource header // Word1: Offset from beginning of resource fork to resource data // Word2: Offset from beginning of resource fork to resource map // Word3: Length of resource data // Word4: Length of resource map // // So... // (Word1 + Word3 + Word4) == (Word2 + Word4) == size of resource fork if ((byteCount == sizeof(firstFourWords)) and (EndianU32_BtoN(firstFourWords[0]) + EndianU32_BtoN(firstFourWords[2]) + EndianU32_BtoN(firstFourWords[3]) == dfSize) and (EndianU32_BtoN(firstFourWords[1]) + EndianU32_BtoN(firstFourWords[3]) == dfSize)) { *outIsFlat = true; } } err = SetFPos(dfRefNum, fsFromStart, filePos); err = FSClose(dfRefNum); } } } return err; }