UINT CStdioFile::Read(void* lpBuf, UINT nCount) { ASSERT_VALID(this); ASSERT(m_pStream != NULL); if (nCount == 0) return 0; // avoid Win32 "null-read" ASSERT(AfxIsValidAddress(lpBuf, nCount)); if (lpBuf == NULL) { AfxThrowInvalidArgException(); } UINT nRead = 0; if ((nRead = (UINT)fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream)) AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName); if (ferror(m_pStream)) { Afx_clearerr_s(m_pStream); AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName); } return nRead; }
UINT CBuffFile::Read(void* lpBuf, UINT nCount) { UINT nRead = 0; if ((nRead = fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream)) AfxThrowFileException(CFileException::generic, _doserrno); if (ferror(m_pStream)) { clearerr(m_pStream); AfxThrowFileException(CFileException::generic, _doserrno); } return nRead; }
void CMsregPacket::ReadData(CFile& cfFile, void* pBuffer, UINT uCount) { if (cfFile.Read(pBuffer, uCount) != uCount) { AfxThrowFileException(CFileException::endOfFile); } }
LONG CMyMemFile::Seek(LONG lOff, UINT nFrom) { switch(nFrom) { case CFile::begin: if(NULL != m_pData) { m_pData -= m_dwLength; m_pData += lOff; } m_dwLength = lOff; break; case CFile::current: if(NULL != m_pData) { m_pData += lOff; } m_dwLength += lOff; break; case CFile::end: default: AfxThrowFileException(CFileException::badSeek); } return m_dwLength; }
void PASCAL CFileException::ThrowErrno(int nErrno, LPCTSTR lpszFileName /* = NULL */) { if (nErrno != 0) AfxThrowFileException(CFileException::ErrnoToException(nErrno), _doserrno, lpszFileName); }
DWORD CBuffFile::GetPosition() const { fpos_t pos; if (fgetpos(m_pStream, &pos) != 0) AfxThrowFileException(CFileException::invalidFile, _doserrno); return (DWORD)pos; }
void CStdioFileEx::WriteWideString(LPCWSTR lpsz) { ASSERT(lpsz != NULL); if (lpsz == NULL) { AfxThrowInvalidArgException(); } if(m_bIsUnicodeText) { ASSERT(m_pStream != NULL); // If writing Unicode and at the start of the file, need to write byte mark if(GetPosition() == 0) { wchar_t cBOM = (wchar_t)UNICODE_BOM; CFile::Write(&cBOM, sizeof(wchar_t)); } if (fputws(lpsz, m_pStream) == _TEOF) AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName); } else { USES_CONVERSION; WriteAnsiString(CW2A(lpsz)); } }
UINT CConverter::Read(void FAR* lpBuf, UINT nCount) { ASSERT(m_bForeignToRtf); if (m_bDone) return 0; // if converter is done int cch = nCount; BYTE* pBuf = (BYTE*)lpBuf; while (cch != 0) { if (m_nBytesAvail == 0) { if (m_pBuf != NULL) GlobalUnlock(m_hBuff); m_pBuf = NULL; SetEvent(m_hEventConv); WaitForConverter(); VERIFY(ResetEvent(m_hEventFile)); if (m_bConvErr) AfxThrowFileException(CFileException::generic); if (m_bDone) return nCount - cch; m_pBuf = (BYTE*)GlobalLock(m_hBuff); ASSERT(m_pBuf != NULL); } int nBytes = min(cch, m_nBytesAvail); memcpy(pBuf, m_pBuf, nBytes); pBuf += nBytes; m_pBuf += nBytes; m_nBytesAvail -= nBytes; cch -= nBytes; OutputPercent(m_nPercent); } return nCount - cch; }
COXRegistryValFile::COXRegistryValFile(HKEY hkey, LPCTSTR lpszKey, LPCTSTR lpszValue) : CMemFile(1024), m_key(0) { LONG error; if (!Open(hkey, lpszKey, lpszValue, error)) AfxThrowFileException(CFileException::accessDenied, error, lpszKey); }
void PASCAL CFileException::ThrowOsError(LONG lOsError, LPCTSTR lpszFileName /* = NULL */) { if (lOsError != 0) AfxThrowFileException(CFileException::OsErrorToException(lOsError), lOsError, lpszFileName); }
uint64 CSafeMemFile::ReadUInt64() { if (m_nPosition + sizeof(uint64) > m_nFileSize) AfxThrowFileException(CFileException::endOfFile, 0, GetFileName()); uint64 nResult = *((uint64*)(m_lpBuffer + m_nPosition)); m_nPosition += sizeof(uint64); return nResult; }
LONG CBuffFile::Seek(LONG lOff, UINT nFrom) { fpos_t pos; if (fseek(m_pStream, lOff, nFrom) != 0) AfxThrowFileException(CFileException::badSeek, _doserrno); fgetpos(m_pStream, &pos); return (DWORD)pos; }
void CStdioFile::WriteString(LPCTSTR lpsz) { ASSERT(lpsz != NULL); ASSERT(m_pStream != NULL); if (_fputts(lpsz, m_pStream) == _TEOF) AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName); }
void CStdioFile::Flush() { ASSERT_VALID(this); if (m_pStream != NULL && fflush(m_pStream) != 0) AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName); }
CStdioFile::CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags) { ASSERT(lpszFileName != NULL); ASSERT(AfxIsValidString(lpszFileName)); CFileException e; if (!Open(lpszFileName, nOpenFlags, &e)) AfxThrowFileException(e.m_cause, e.m_lOsError, e.m_strFileName); }
void CStdioFile::Write(const void* lpBuf, UINT nCount) { ASSERT_VALID(this); ASSERT(m_pStream != NULL); ASSERT(AfxIsValidAddress(lpBuf, nCount, FALSE)); if (fwrite(lpBuf, sizeof(BYTE), nCount, m_pStream) != nCount) AfxThrowFileException(CFileException::generic, _doserrno, m_strFileName); }
int CSafeBufferedFile::printf(LPCTSTR pszFmt, ...) { va_list args; va_start(args, pszFmt); int iResult = _vftprintf(m_pStream, pszFmt, args); va_end(args); if (iResult < 0) AfxThrowFileException(CFileException::generic, _doserrno, m_strFileName); return iResult; }
UINT CSafeBufferedFile::Read(void* lpBuf, UINT nCount) { // that's terrible slow // if (GetPosition()+nCount > this->GetLength()) // AfxThrowFileException(CFileException::endOfFile, 0, GetFileName()); UINT uRead = CStdioFile::Read(lpBuf,nCount); if (uRead != nCount) AfxThrowFileException(CFileException::endOfFile, 0, GetFileName()); return uRead; }
void PASCAL CFileException::ThrowErrno(int nErrno) { if (nErrno != 0) AfxThrowFileException(CFileException::ErrnoToException(nErrno), #ifndef _MAC _doserrno); #else GetLastError()); #endif }
void CSafeMemFile::ReadHash16(uchar* pVal) { if (m_nPosition + sizeof(uint32)*4 > m_nFileSize) AfxThrowFileException(CFileException::endOfFile, 0, GetFileName()); const uint32* pUInt32 = (uint32*)(m_lpBuffer + m_nPosition); ((uint32*)pVal)[0] = pUInt32[0]; ((uint32*)pVal)[1] = pUInt32[1]; ((uint32*)pVal)[2] = pUInt32[2]; ((uint32*)pVal)[3] = pUInt32[3]; m_nPosition += sizeof(uint32)*4; }
DWORD CStdioFile::GetPosition() const { ASSERT_VALID(this); ASSERT(m_pStream != NULL); long pos = ftell(m_pStream); if (pos == -1) AfxThrowFileException(CFileException::invalidFile, _doserrno, m_strFileName); return pos; }
void CSocketFile::Write(const void* lpBuf, UINT nCount) { ASSERT (m_pSocket!=NULL); int nWritten = m_pSocket->Send(lpBuf, nCount); if (nWritten == SOCKET_ERROR) { int nError = m_pSocket->GetLastError(); AfxThrowFileException(CFileException::generic, nError); } }
BOOL CStdioFileEx::ReadWideString(CStringW& rString) { _ASSERTE(m_pStream); rString = L"";// empty string without deallocating if(m_bIsUnicodeText) { // If at position 0, discard byte-order mark before reading if(GetPosition() == 0) { wchar_t bom; Read(&bom, sizeof(wchar_t)); } const int nMaxSize = 128; LPWSTR lpsz = rString.GetBuffer(nMaxSize); LPWSTR lpszResult; int nLen = 0; for (;;) { lpszResult = fgetws(lpsz, nMaxSize+1, m_pStream); rString.ReleaseBuffer(); // handle error/eof case if (lpszResult == NULL && !feof(m_pStream)) { Afx_clearerr_s(m_pStream); AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName); } // if string is read completely or EOF if (lpszResult == NULL || (nLen = (int)lstrlenW(lpsz)) < nMaxSize || lpsz[nLen-1] == '\n') break; nLen = rString.GetLength(); lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen; } //remove crlf if exist. nLen = rString.GetLength(); if (nLen > 1 && rString.Mid(nLen-2) == L"\r\n") { rString.GetBufferSetLength(nLen-2); } return rString.GetLength() > 0; } else { CStringA ansiString; BOOL bRetval = ReadAnsiString(ansiString); //setlocale(LC_ALL, "chs_chn.936");//no need rString = ansiString; return bRetval; } }
void CSafeMemFile::ReadUInt128(Kademlia::CUInt128* pVal) { if (m_nPosition + sizeof(uint32)*4 > m_nFileSize) AfxThrowFileException(CFileException::endOfFile, 0, GetFileName()); uint32* pUInt32Val = (uint32*)pVal->GetDataPtr(); const uint32* pUInt32 = (uint32*)(m_lpBuffer + m_nPosition); pUInt32Val[0] = pUInt32[0]; pUInt32Val[1] = pUInt32[1]; pUInt32Val[2] = pUInt32[2]; pUInt32Val[3] = pUInt32[3]; m_nPosition += sizeof(uint32)*4; }
UINT CSocketFile::Read(void* lpBuf, UINT nCount) { ASSERT(m_pSocket != NULL); int nRead; if (!m_bArchiveCompatible) { int nLeft = nCount; PBYTE pBuf = (PBYTE)lpBuf; while(nLeft > 0) { nRead = m_pSocket->Receive(pBuf, nLeft); if (nRead == SOCKET_ERROR) { int nError = m_pSocket->GetLastError(); AfxThrowFileException(CFileException::generic, nError); ASSERT(FALSE); } else if (nRead == 0) { return nCount - nLeft; } nLeft -= nRead; pBuf += nRead; } return nCount - nLeft; } nRead = m_pSocket->Receive(lpBuf, nCount, 0); if (nRead == SOCKET_ERROR) { int nError = m_pSocket->GetLastError(); AfxThrowFileException(CFileException::generic, nError); ASSERT(FALSE); } return nRead; }
LONG CStdioFile::Seek(LONG lOff, UINT nFrom) { ASSERT_VALID(this); ASSERT(nFrom == begin || nFrom == end || nFrom == current); ASSERT(m_pStream != NULL); if (fseek(m_pStream, lOff, nFrom) != 0) AfxThrowFileException(CFileException::badSeek, _doserrno, m_strFileName); long pos = ftell(m_pStream); return pos; }
void CBuffFile::Close() { int nErr= 0; if (m_pStream != NULL) nErr = fclose(m_pStream); m_hFile = hFileNull; m_bCloseOnDelete = FALSE; m_pStream = NULL; if (nErr != 0) AfxThrowFileException(CFileException::diskFull, _doserrno); }
void CStdioFileT::WriteString(LPCWSTR lpsz) { ASSERT(lpsz != NULL); ASSERT(m_pStream != NULL); if (lpsz == NULL) { AfxThrowInvalidArgException(); } if (fputws(lpsz, m_pStream) == EOF) AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName); }
LPTSTR CStdioFile::ReadString(LPTSTR lpsz, UINT nMax) { ASSERT(lpsz != NULL); ASSERT(AfxIsValidAddress(lpsz, nMax)); ASSERT(m_pStream != NULL); LPTSTR lpszResult = _fgetts(lpsz, nMax, m_pStream); if (lpszResult == NULL && !feof(m_pStream)) { clearerr(m_pStream); AfxThrowFileException(CFileException::generic, _doserrno, m_strFileName); } return lpszResult; }
void CDocument::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU) { // store the path fully qualified TCHAR szFullPath[_MAX_PATH]; if ( lstrlen(lpszPathName) >= _MAX_PATH ) { ASSERT(FALSE); // MFC requires paths with length < _MAX_PATH // No other way to handle the error from a void function AfxThrowFileException(CFileException::badPath); } if( AfxFullPath(szFullPath, lpszPathName) == FALSE ) { ASSERT(FALSE); // MFC requires paths with length < _MAX_PATH // No other way to handle the error from a void function AfxThrowFileException(CFileException::badPath); } m_strPathName = szFullPath; ASSERT(!m_strPathName.IsEmpty()); // must be set to something m_bEmbedded = FALSE; ASSERT_VALID(this); // set the document title based on path name TCHAR szTitle[_MAX_FNAME]; if (AfxGetFileTitle(szFullPath, szTitle, _MAX_FNAME) == 0) SetTitle(szTitle); // add it to the file MRU list if (bAddToMRU) AfxGetApp()->AddToRecentFileList(m_strPathName); ASSERT_VALID(this); }