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:
parent
ca4d2969a0
commit
f96d71ce81
|
@ -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());
|
||||||
|
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);
|
||||||
|
|
||||||
|
u32 new_position = 0;
|
||||||
|
switch (request.mode)
|
||||||
{
|
{
|
||||||
const u32 file_size = static_cast<u32>(m_file->GetSize());
|
case SeekRequest::IOS_SEEK_SET:
|
||||||
DEBUG_LOG(IOS_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08x)", request.offset,
|
new_position = request.offset;
|
||||||
request.mode, m_name.c_str(), file_size);
|
break;
|
||||||
|
|
||||||
switch (request.mode)
|
case SeekRequest::IOS_SEEK_CUR:
|
||||||
{
|
new_position = m_SeekPos + request.offset;
|
||||||
case SeekRequest::IOS_SEEK_SET:
|
break;
|
||||||
{
|
|
||||||
if (request.offset <= file_size)
|
|
||||||
{
|
|
||||||
m_SeekPos = request.offset;
|
|
||||||
return_value = m_SeekPos;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case SeekRequest::IOS_SEEK_CUR:
|
case SeekRequest::IOS_SEEK_END:
|
||||||
{
|
new_position = file_size + request.offset;
|
||||||
const u32 wanted_pos = request.offset + m_SeekPos;
|
break;
|
||||||
if (wanted_pos <= file_size)
|
|
||||||
{
|
|
||||||
m_SeekPos = wanted_pos;
|
|
||||||
return_value = m_SeekPos;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case SeekRequest::IOS_SEEK_END:
|
default:
|
||||||
{
|
return GetDefaultReply(FS_EINVAL);
|
||||||
const u32 wanted_pos = request.offset + file_size;
|
|
||||||
if (wanted_pos <= file_size)
|
|
||||||
{
|
|
||||||
m_SeekPos = wanted_pos;
|
|
||||||
return_value = m_SeekPos;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
PanicAlert("FileIO Unsupported seek mode %i", request.mode);
|
|
||||||
return_value = FS_EINVAL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
if (new_position > file_size)
|
||||||
return_value = FS_ENOENT;
|
return GetDefaultReply(FS_EINVAL);
|
||||||
}
|
|
||||||
return GetDefaultReply(return_value);
|
m_SeekPos = new_position;
|
||||||
|
return GetDefaultReply(new_position);
|
||||||
}
|
}
|
||||||
|
|
||||||
IPCCommandResult FileIO::Read(const ReadWriteRequest& request)
|
IPCCommandResult FileIO::Read(const ReadWriteRequest& request)
|
||||||
|
|
Loading…
Reference in New Issue