uint64 CFileDataIO::Seek(sint64 offset, wxSeekMode from) const { sint64 newpos = 0; switch (from) { case wxFromStart: newpos = offset; break; case wxFromCurrent: newpos = GetPosition() + offset; break; case wxFromEnd: newpos = GetLength() + offset; break; default: MULE_VALIDATE_PARAMS(false, wxT("Invalid seek-mode specified.")); } MULE_VALIDATE_PARAMS(newpos >= 0, wxT("Position after seeking would be less than zero!")); sint64 result = doSeek(newpos); MULE_VALIDATE_STATE(result >= 0, wxT("Seeking resulted in invalid offset.")); MULE_VALIDATE_STATE(result == newpos, wxT("Target position and actual position disagree.")); return result; }
wxString CMuleListCtrl::GetTTSText(unsigned item) const { MULE_VALIDATE_PARAMS(item < (unsigned)GetItemCount(), wxT("Invalid row.")); MULE_VALIDATE_STATE((GetWindowStyle() & wxLC_OWNERDRAW) == 0, wxT("GetTTSText must be overwritten for owner-drawn lists.")); return GetItemText(item); }
bool CFile::Flush() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot flush closed file.")); bool flushed = (FLUSH_FD(m_fd) != -1); syscall_check(flushed, m_filePath, wxT("flushing file")); return flushed; }
bool CFile::Flush() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot flush closed file.")); bool flushed = (FLUSH_FD(m_fd) != -1); SYSCALL_CHECK(flushed, wxT("flushing file")); return flushed; }
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; }
uint64 CFile::GetPosition() const { MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot get position in closed file.")); sint64 pos = TELL_FD(m_fd); if (pos == wxInvalidOffset) { throw CSeekFailureException(wxString(wxT("Failed to retrieve position in file: ")) + wxSysErrorMsg()); } return pos; }
bool CFile::Close() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot close closed file.")); bool closed = (close(m_fd) != -1); SYSCALL_CHECK(closed, wxT("closing file")); m_fd = fd_invalid; return closed; }
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; }
bool CFile::SetLength(size_t new_len) { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open.")); #ifdef __WXMSW__ int result = chsize(m_fd, new_len); #else int result = ftruncate(m_fd, new_len); #endif SYSCALL_CHECK((result != -1), wxT("truncating file")); return (result != -1); }
sint64 CFile::doSeek(sint64 offset) const { MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot seek on closed file.")); MULE_VALIDATE_PARAMS(offset >= 0, wxT("Invalid position, must be positive.")); sint64 result = SEEK_FD(m_fd, offset, SEEK_SET); if (result == offset) { return result; } else if (result == wxInvalidOffset) { throw CSeekFailureException(wxString(wxT("Seeking failed: ")) + wxSysErrorMsg()); } else { throw CSeekFailureException(wxT("Seeking returned incorrect position")); } }
bool CFile::Close() { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot close closed file.")); bool closed = (close(m_fd) != -1); syscall_check(closed, m_filePath, wxT("closing file")); m_fd = fd_invalid; if (m_safeWrite) { CPath filePathTemp(m_filePath); m_filePath = m_filePath.RemoveExt(); // restore m_filePath for Reopen() if (closed) { closed = CPath::RenameFile(filePathTemp, m_filePath, true); } } return closed; }
bool CFile::SetLength(uint64 new_len) { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open.")); #ifdef __WINDOWS__ #ifdef _MSC_VER // MSVC has a 64bit version bool result = _chsize_s(m_fd, new_len) == 0; #else // MingW has an old runtime without it bool result = chsize(m_fd, new_len) == 0; #endif #else bool result = ftruncate(m_fd, new_len) != -1; #endif syscall_check(result, m_filePath, wxT("truncating file")); return result; }
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; }
const wxString& CFile::GetFilePath() const { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot return path of closed file.")); return m_filePath; }