void CFileArea::CheckError() { bool err = m_error; m_error = false; if (err) throw CIOFailureException(wxT("Read error, failed to read from file.")); }
void CFileDataIO::Write(const void* buffer, size_t count) { MULE_VALIDATE_PARAMS(buffer, wxT("Attempting to read from NULL buffer.")); if (doWrite(buffer, count) != (signed)count) { throw CIOFailureException(wxT("Write error, failed to write to file.")); } }
uint64 CFile::GetLength() const { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot get length of closed file.")); STAT_STRUCT buf; if (STAT_FD(m_fd, &buf) == -1) { throw CIOFailureException(wxString(wxT("Failed to retrieve length of file: ")) + wxSysErrorMsg()); } return buf.st_size; }
sint64 CFile::doWrite(const void* buffer, size_t nCount) { MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in write operation.")); MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot write to closed file.")); sint64 result = ::write(m_fd, buffer, nCount); if (result != (sint64)nCount) { throw CIOFailureException(wxString(wxT("Error writing to file: ")) + wxSysErrorMsg()); } return result; }
void CFileDataIO::Read(void *buffer, size_t count) const { MULE_VALIDATE_PARAMS(buffer, wxT("Attempting to write to NULL buffer.")); // Check that we read everything we wanted. if (doRead(buffer, count) == (signed)count) { return; } // To reduce potential system calls, we only do EOF checks when reads fail. if (Eof()) { throw CEOFException(wxT("Attempt to read past end of file.")); } else { throw CIOFailureException(wxT("Read error, failed to read from file.")); } }
sint64 CFile::doRead(void* buffer, size_t count) const { MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in read operation.")); MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot read from closed file.")); size_t totalRead = 0; while (totalRead < count) { int current = ::read(m_fd, (char*)buffer + totalRead, count - totalRead); if (current == -1) { // Read error, nothing we can do other than abort. throw CIOFailureException(wxString(wxT("Error reading from file: ")) + wxSysErrorMsg()); } else if ((totalRead + current < count) && Eof()) { // We may fail to read the specified count in a couple // of situations: EOF and interrupts. The check for EOF // is needed to avoid inf. loops. break; } totalRead += current; } return totalRead; }
void CFile::Reopen(OpenMode mode) { if (!Open(m_filePath, mode)) { throw CIOFailureException(wxString(wxT("Error reopening file"))); } }