ULONGLONG CMemFile::Seek( LONGLONG lOff, UINT nFrom ) /***************************************************/ { switch( nFrom ) { case begin: if( lOff < 0 ) { throw new CFileException( CFileException::badSeek ); } m_nPosition = (SIZE_T)lOff; break; case end: if( lOff > 0 ) { throw new CFileException( CFileException::badSeek ); } m_nPosition = m_nFileSize - (SIZE_T)lOff; break; default: if( m_nPosition + lOff < 0 ) { throw new CFileException( CFileException::badSeek ); } m_nPosition += (SIZE_T)lOff; break; } GrowFile( m_nPosition ); return( m_nPosition ); }
void CSafeMemFile::WriteUInt8(uint8 nVal) { if (m_nPosition + sizeof(uint8) > m_nBufferSize) GrowFile(m_nPosition + sizeof(uint8)); *(m_lpBuffer + m_nPosition++) = nVal; if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; }
void CSafeMemFile::WriteUInt64(uint64 nVal) { if (m_nPosition + sizeof(uint64) > m_nBufferSize) GrowFile(m_nPosition + sizeof(uint64)); *((uint64*)(m_lpBuffer + m_nPosition)) = nVal; m_nPosition += sizeof(uint64); if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; }
void CMemFile::Write( const void *lpBuf, UINT nCount ) /****************************************************/ { if( m_nPosition + nCount >= m_nFileSize ) { m_nFileSize = m_nPosition + nCount; GrowFile( m_nFileSize ); } Memcpy( m_lpBuffer + m_nPosition, (const BYTE *)lpBuf, nCount ); m_nPosition += nCount; }
MemStream::MemStream( unsigned long nLen ) { m_nGrowBytes = 1024; m_nPosition = 0; m_nBufferSize = 0; m_nFileSize = 0; m_pBuffer = NULL; m_bAutoDelete = true; GrowFile( nLen ); }
void MemStream::SetLength( unsigned long nNewLen ) { if ( nNewLen > m_nBufferSize ) { GrowFile( nNewLen ); } if ( nNewLen < m_nPosition ) { m_nPosition = nNewLen; } m_nFileSize = nNewLen; }
void CMemFile::SetLength(DWORD dwNewLen) { ASSERT_VALID(this); if (dwNewLen > m_nBufferSize) GrowFile(dwNewLen); if (dwNewLen < m_nPosition) m_nPosition = dwNewLen; m_nFileSize = dwNewLen; ASSERT_VALID(this); }
void CSafeMemFile::WriteHash16(const uchar* pVal) { if (m_nPosition + sizeof(uint32)*4 > m_nBufferSize) GrowFile(m_nPosition + sizeof(uint32)*4); uint32* pUInt32 = (uint32*)(m_lpBuffer + m_nPosition); pUInt32[0] = ((uint32*)pVal)[0]; pUInt32[1] = ((uint32*)pVal)[1]; pUInt32[2] = ((uint32*)pVal)[2]; pUInt32[3] = ((uint32*)pVal)[3]; m_nPosition += sizeof(uint32)*4; if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; }
void CSafeMemFile::WriteUInt128(const Kademlia::CUInt128* pVal) { if (m_nPosition + sizeof(uint32)*4 > m_nBufferSize) GrowFile(m_nPosition + sizeof(uint32)*4); uint32* pUInt32 = (uint32*)(m_lpBuffer + m_nPosition); const uint32* pUInt32Val = (uint32*)pVal->GetData(); pUInt32[0] = pUInt32Val[0]; pUInt32[1] = pUInt32Val[1]; pUInt32[2] = pUInt32Val[2]; pUInt32[3] = pUInt32Val[3]; m_nPosition += sizeof(uint32)*4; if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; }
int MemStream::PutChar( int c ) { if ( m_nPosition + 1 > m_nBufferSize ) { GrowFile( m_nPosition + 1 ); } unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition; *bt = c; m_nPosition++; if ( m_nPosition > m_nFileSize ) { m_nFileSize = m_nPosition; } return 1; }
// only CMemFile supports "direct buffering" interaction with CArchive UINT CMemFile::GetBufferPtr(UINT nCommand, UINT nCount, void** ppBufStart, void**ppBufMax) { ASSERT(nCommand == bufferCheck || nCommand == bufferCommit || nCommand == bufferRead || nCommand == bufferWrite); if (nCommand == bufferCheck) return 1; // just a check for direct buffer support if (nCommand == bufferCommit) { // commit buffer ASSERT(ppBufStart == NULL); ASSERT(ppBufMax == NULL); m_nPosition += nCount; if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; return 0; } ASSERT(nCommand == bufferWrite || nCommand == bufferRead); ASSERT(ppBufStart != NULL); ASSERT(ppBufMax != NULL); // when storing, grow file as necessary to satisfy buffer request if (nCommand == bufferWrite && m_nPosition + nCount > m_nBufferSize) GrowFile(m_nPosition + nCount); // store buffer max and min *ppBufStart = m_lpBuffer + m_nPosition; // end of buffer depends on whether you are reading or writing if (nCommand == bufferWrite) *ppBufMax = m_lpBuffer + min(m_nBufferSize, m_nPosition + nCount); else { if (nCount == (UINT)-1) nCount = m_nBufferSize - m_nPosition; *ppBufMax = m_lpBuffer + min(m_nFileSize, m_nPosition + nCount); m_nPosition += LPBYTE(*ppBufMax) - LPBYTE(*ppBufStart); } // return number of bytes in returned buffer space (may be <= nCount) return LPBYTE(*ppBufMax) - LPBYTE(*ppBufStart); }
unsigned long MemStream::Write( const void* pBuf, unsigned long nCount ) { if ( nCount == 0 ) { return 0; } if ( m_nPosition + nCount > m_nBufferSize ) { GrowFile( m_nPosition + nCount ); } memcpy( (unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)pBuf, nCount ); m_nPosition += nCount; if ( m_nPosition > m_nFileSize ) { m_nFileSize = m_nPosition; } return nCount; }
void CMemFile::Write(const void* lpBuf, UINT nCount) { ASSERT_VALID(this); if (nCount == 0) return; ASSERT(lpBuf != NULL); ASSERT(AfxIsValidAddress(lpBuf, nCount, FALSE)); if (m_nPosition + nCount > m_nBufferSize) GrowFile(m_nPosition + nCount); ASSERT(m_nPosition + nCount <= m_nBufferSize); Memcpy((BYTE*)m_lpBuffer + m_nPosition, (BYTE*)lpBuf, nCount); m_nPosition += nCount; if (m_nPosition > m_nFileSize) m_nFileSize = m_nPosition; ASSERT_VALID(this); }
void CMemFile::SetLength( ULONGLONG dwNewLen ) /********************************************/ { GrowFile( (SIZE_T)dwNewLen ); m_nFileSize = (SIZE_T)dwNewLen; }
/*** Write bytes in a file from a given buffer. Parameters: - pvBuf: User buffer with data to write in file - tsSize: Size of iten to write - tsNumItens: Number of itens to write Return: Number of writtem itens in succes or E_NOTOPEN, E_BUFFNULL or E_WRITE. ***/ size_t C_File::Write( void FAR *pvBuf, size_t tsSize, size_t tsNumItens ) { C_FileCritSect cCS0( this, CRITSECT0 ); size_t i = 0; BOOL bContinue = TRUE; #ifdef _USE_PRINTF_ Printf( "C_File: Estou em Write. (arq %s)", szFileName ); #endif int iFlag = FALSE; if( !pvBuf ){ return( (size_t) E_BUFFNULL ); } if( (_bIs32s && iFile == -1) || (!_bIs32s && hFile == INVALID_HANDLE_VALUE) ){ if( ReOpen() != OK ){ if( !_xFile ){ return( 0 ); } _xFile->PseudoClose(); ReOpen(); iFlag = TRUE; } } LockStruct ls( this, CurPos(), (tsSize * tsNumItens) ); if( IsLocked( &ls ) ){ if( iFlag ){ PseudoClose(); _xFile->ReOpen(); } #ifdef _USE_PRINTF_ Printf( "C_File: Write: IsLocked." ); #endif return( (size_t) E_WRITE ); } if( cCriptoOn ){ Encrypt( pvBuf, szKey, tsSize ); } // MMF BOOL bWriteHeader = FALSE; if( !_bIs32s && hMMF ){ // preparar gravacao a partir do MMF long lFSize = FileSize(); long lCPos = CurPos(); if( ( lCPos + (int) (tsNumItens * tsSize) ) > lFSize ){ // este write vai exceder os limites do arquivo. vamos aumentar-lo. if( GrowFile( lCPos + (tsNumItens * tsSize) ) != OK ){ // nao foi possivel aumentar o arquivo. bContinue = FALSE; } } if( bContinue ){ if( (lCPos == iHeadInit) && (((int) tsSize) == iHeadSize) ){ bWriteHeader = TRUE; // vamos fazer write no header do arquivo. if( !pHeadView ){ // o header ainda nao esta' mapeado. vamos fazer isso. CreateViewOfHead(); } } else { if( !pWinView ){ // o arquivo ainda nao esta' mapeado. vamos fazer isso. CreateViewOfFile(); } } } } if( bContinue ){ for( i = 0; i < tsNumItens; i++ ){ if( _bIs32s ){ if( write( iFile, pvBuf, (unsigned) tsSize ) < (int) tsSize ){ break; } } else { if( bWriteHeader ){ // gravar header no MMF. memcpy( pHeadView, pvBuf, tsSize ); } else { if( pWinView ){ // gravar no MMF. int iNumBytes = ((iMMFWinSize * iPageSize) - iSeekWin) - (iHeadSize + 1); memcpy( (void*) ((char*) PWINVIEW + iSeekWin), pvBuf, min( iNumBytes, ((int) tsSize) ) ); if( iNumBytes < ((int) tsSize) ){ // precisamos mapear outra janela e continuar a gravacao ++iWinNum; // proxima janela CreateViewOfFile(); if( !pWinView ){ // mapeamento nao foi feito. fudeu. break; } // gravacao do restante memcpy( (void*) ((char*) PWINVIEW + iSeekWin), (void*) ((char*) pvBuf + iNumBytes), (tsSize - iNumBytes) ); iSeekWin += (tsSize - iNumBytes); } else { iSeekWin += tsSize; } } else { DWORD dwWrittenBytes; BOOL bResult = WriteFile( hFile, pvBuf, (unsigned) tsSize, &dwWrittenBytes, NULL ); if( !bResult || ( dwWrittenBytes < tsSize ) ){ DWORD dwErr = GetLastError(); break; } } } } } } // if( ::GetTypeInt() == PE_REDE ){ // Flush(); // } dwLastUse = GetTickCount(); if( cCriptoOn ){ Decrypt( pvBuf, szKey, tsSize ); } if( i < tsNumItens ){ #ifdef _USE_PRINTF_ Printf( "C_File: Write: E_WRITE." ); #endif i = (size_t) E_WRITE; } if( iFlag ){ PseudoClose(); _xFile->ReOpen(); } #ifdef _USE_PRINTF_ Printf( "C_File: Write: Vou retornar %d.", i ); #endif return( i ); }