Checkpoint/3ds/source/fsstream.cpp

105 lines
2.2 KiB
C++
Raw Normal View History

2017-09-29 07:45:56 +00:00
/* This file is part of Checkpoint
2018-01-17 07:53:50 +00:00
> Copyright (C) 2017/2018 Bernardo Giordano
2017-09-29 07:45:56 +00:00
>
> This program is free software: you can redistribute it and/or modify
> it under the terms of the GNU General Public License as published by
> the Free Software Foundation, either version 3 of the License, or
> (at your option) any later version.
>
> This program is distributed in the hope that it will be useful,
> but WITHOUT ANY WARRANTY; without even the implied warranty of
> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> GNU General Public License for more details.
>
> You should have received a copy of the GNU General Public License
> along with this program. If not, see <http://www.gnu.org/licenses/>.
> See LICENSE for information.
*/
#include "fsstream.h"
FSStream::FSStream(FS_Archive archive, std::u16string path, u32 flags)
{
loaded = false;
size = 0;
2017-10-10 17:17:04 +00:00
offset = 0;
2017-09-29 07:45:56 +00:00
res = FSUSER_OpenFile(&handle, archive, fsMakePath(PATH_UTF16, path.data()), flags, 0);
if (R_SUCCEEDED(res))
{
2017-10-10 17:17:04 +00:00
FSFILE_GetSize(handle, (u64*)&size);
2017-09-29 07:45:56 +00:00
loaded = true;
}
}
2017-10-10 17:17:04 +00:00
FSStream::FSStream(FS_Archive archive, std::u16string path, u32 flags, u32 _size)
2017-09-29 07:45:56 +00:00
{
loaded = false;
size = _size;
2017-10-10 17:17:04 +00:00
offset = 0;
2017-09-29 07:45:56 +00:00
res = FSUSER_OpenFile(&handle, archive, fsMakePath(PATH_UTF16, path.data()), flags, 0);
if (R_FAILED(res))
{
res = FSUSER_CreateFile(archive, fsMakePath(PATH_UTF16, path.data()), 0, size);
2017-09-29 07:45:56 +00:00
if (R_SUCCEEDED(res))
{
res = FSUSER_OpenFile(&handle, archive, fsMakePath(PATH_UTF16, path.data()), flags, 0);
if (R_SUCCEEDED(res))
{
loaded = true;
}
}
2017-12-19 08:28:13 +00:00
}
else
{
loaded = true;
}
2017-09-29 07:45:56 +00:00
}
Result FSStream::close(void)
{
res = FSFILE_Close(handle);
return res;
}
bool FSStream::getLoaded(void)
{
return loaded;
}
Result FSStream::getResult(void)
{
return res;
}
u32 FSStream::getSize(void)
{
return size;
}
2017-10-10 17:17:04 +00:00
u32 FSStream::read(void *buf, u32 sz)
2017-09-29 07:45:56 +00:00
{
2017-10-10 17:17:04 +00:00
u32 rd = 0;
res = FSFILE_Read(handle, &rd, offset, buf, sz);
offset += rd;
return rd;
2017-09-29 07:45:56 +00:00
}
2017-10-10 17:17:04 +00:00
u32 FSStream::write(void *buf, u32 sz)
2017-09-29 07:45:56 +00:00
{
2017-10-10 17:17:04 +00:00
u32 wt = 0;
res = FSFILE_Write(handle, &wt, offset, buf, sz, FS_WRITE_FLUSH);
offset += wt;
return wt;
}
bool FSStream::isEndOfFile(void)
{
2017-12-21 10:03:03 +00:00
return offset >= size;
2017-09-29 07:45:56 +00:00
}
2017-12-22 21:30:37 +00:00
u32 FSStream::getOffset(void)
{
return offset;
2017-09-29 07:45:56 +00:00
}