FileIO: Simplify Seek()

We can return early from invalid conditions, which allows getting rid
of quite a few levels of indentation.

And let's not duplicate the new_position > file_size check.
This commit is contained in:
Léo Lam 2017-02-10 21:04:52 +01:00
parent ca4d2969a0
commit f96d71ce81
1 changed files with 25 additions and 49 deletions

View File

@ -158,61 +158,37 @@ void FileIO::OpenFile()
IPCCommandResult FileIO::Seek(const SeekRequest& request) IPCCommandResult FileIO::Seek(const SeekRequest& request)
{ {
u32 return_value = FS_EINVAL; if (!m_file->IsOpen())
return GetDefaultReply(FS_ENOENT);
if (m_file->IsOpen())
{
const u32 file_size = static_cast<u32>(m_file->GetSize()); const u32 file_size = static_cast<u32>(m_file->GetSize());
DEBUG_LOG(IOS_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08x)", request.offset, DEBUG_LOG(IOS_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08x)", request.offset,
request.mode, m_name.c_str(), file_size); request.mode, m_name.c_str(), file_size);
u32 new_position = 0;
switch (request.mode) switch (request.mode)
{ {
case SeekRequest::IOS_SEEK_SET: case SeekRequest::IOS_SEEK_SET:
{ new_position = request.offset;
if (request.offset <= file_size)
{
m_SeekPos = request.offset;
return_value = m_SeekPos;
}
break; break;
}
case SeekRequest::IOS_SEEK_CUR: case SeekRequest::IOS_SEEK_CUR:
{ new_position = m_SeekPos + request.offset;
const u32 wanted_pos = request.offset + m_SeekPos;
if (wanted_pos <= file_size)
{
m_SeekPos = wanted_pos;
return_value = m_SeekPos;
}
break; break;
}
case SeekRequest::IOS_SEEK_END: case SeekRequest::IOS_SEEK_END:
{ new_position = file_size + request.offset;
const u32 wanted_pos = request.offset + file_size;
if (wanted_pos <= file_size)
{
m_SeekPos = wanted_pos;
return_value = m_SeekPos;
}
break; break;
}
default: default:
{ return GetDefaultReply(FS_EINVAL);
PanicAlert("FileIO Unsupported seek mode %i", request.mode);
return_value = FS_EINVAL;
break;
} }
}
} if (new_position > file_size)
else return GetDefaultReply(FS_EINVAL);
{
return_value = FS_ENOENT; m_SeekPos = new_position;
} return GetDefaultReply(new_position);
return GetDefaultReply(return_value);
} }
IPCCommandResult FileIO::Read(const ReadWriteRequest& request) IPCCommandResult FileIO::Read(const ReadWriteRequest& request)