simple file handler class

This commit is contained in:
georgemoralis 2022-10-27 13:03:57 +03:00
parent 84ac2326cd
commit 2c8cab1f05
5 changed files with 187 additions and 0 deletions

14
shadPS4/Types.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
using S08 = char;
using S16 = short;
using S32 = int;
using S64 = long long;
using U08 = unsigned char;
using U16 = unsigned short;
using U32 = unsigned int;
using U64 = unsigned long long;
using F32 = float;
using F64 = double;

90
shadPS4/core/FsFile.cpp Normal file
View File

@ -0,0 +1,90 @@
#include "FsFile.h"
FsFile::FsFile()
{
m_file = nullptr;
}
FsFile::FsFile(const std::string& path, fsOpenMode mode)
{
Open(path, mode);
}
bool FsFile::Open(const std::string& path, fsOpenMode mode)
{
Close();
fopen_s(&m_file, path.c_str(), getOpenMode(mode));
return IsOpen();
}
bool FsFile::Close()
{
if (!IsOpen() || std::fclose(m_file) != 0) {
m_file = nullptr;
return false;
}
m_file = nullptr;
return true;
}
FsFile::~FsFile()
{
Close();
}
bool FsFile::Write(const void* src, U64 size)
{
if (!IsOpen() || std::fwrite(src, 1, size, m_file) != size) {
return false;
}
return true;
}
bool FsFile::Read(void* dst, U64 size)
{
if (!IsOpen() || std::fread(dst, 1, size, m_file) != size) {
return false;
}
return true;
}
U32 FsFile::ReadBytes(void* dst, U64 size)
{
return std::fread(dst, 1, size, m_file);
}
bool FsFile::Seek(S64 offset, fsSeekMode mode)
{
if (!IsOpen() || _fseeki64(m_file, offset, getSeekMode(mode)) != 0) {
return false;
}
return true;
}
U64 FsFile::Tell() const
{
if (IsOpen()) {
return _ftelli64(m_file);
}
else {
return -1;
}
}
U64 FsFile::getFileSize()
{
U64 pos = _ftelli64(m_file);
if (_fseeki64(m_file, 0, SEEK_END) != 0) {
return 0;
}
U64 size = _ftelli64(m_file);
if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
return 0;
}
return size;
}
bool FsFile::IsOpen() const
{
return m_file != nullptr;
}

64
shadPS4/core/FsFile.h Normal file
View File

@ -0,0 +1,64 @@
#pragma once
#include <cstdio>
#include <string>
#include "../Types.h"
enum fsOpenMode
{
fsRead = 0x1,
fsWrite = 0x2,
fsReadWrite = fsRead | fsWrite
};
enum fsSeekMode
{
fsSeekSet,
fsSeekCur,
fsSeekEnd,
};
class FsFile
{
std::FILE* m_file;
public:
FsFile();
FsFile(const std::string& path, fsOpenMode mode = fsRead);
bool Open(const std::string& path, fsOpenMode mode = fsRead);
bool IsOpen() const;
bool Close();
bool Read(void* dst, U64 size);
U32 ReadBytes(void* dst, U64 size);
bool Write(const void* src, U64 size);
bool Seek(S64 offset, fsSeekMode mode);
U64 getFileSize();
U64 Tell() const;
~FsFile();
const char* getOpenMode(fsOpenMode mode)
{
switch (mode) {
case fsRead: return "rb";
case fsWrite: return "wb";
case fsReadWrite: return "r+b";
}
return "r";
}
const int getSeekMode(fsSeekMode mode)
{
switch (mode) {
case fsSeekSet: return SEEK_SET;
case fsSeekCur: return SEEK_CUR;
case fsSeekEnd: return SEEK_END;
}
return SEEK_SET;
}
std::FILE* fileDescr()
{
return m_file;
}
};

View File

@ -11,6 +11,7 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="core\FsFile.cpp" />
<ClCompile Include="gui\GameListViewer.cpp" /> <ClCompile Include="gui\GameListViewer.cpp" />
<ClCompile Include="gui\shadps4gui.cpp" /> <ClCompile Include="gui\shadps4gui.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
@ -24,6 +25,10 @@
<ItemGroup> <ItemGroup>
<QtMoc Include="gui\GameListViewer.h" /> <QtMoc Include="gui\GameListViewer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="core\FsFile.h" />
<ClInclude Include="Types.h" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{F005E4D9-1FBE-40B3-9FBD-35CEC59081CD}</ProjectGuid> <ProjectGuid>{F005E4D9-1FBE-40B3-9FBD-35CEC59081CD}</ProjectGuid>
<Keyword>QtVS_v304</Keyword> <Keyword>QtVS_v304</Keyword>

View File

@ -24,6 +24,9 @@
<Filter Include="gui"> <Filter Include="gui">
<UniqueIdentifier>{29ee17bd-9ad4-44e8-b1cd-b4be13ec6509}</UniqueIdentifier> <UniqueIdentifier>{29ee17bd-9ad4-44e8-b1cd-b4be13ec6509}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Core">
<UniqueIdentifier>{73d07238-8864-48b5-9987-e455fa73c82f}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
@ -35,6 +38,9 @@
<ClCompile Include="gui\GameListViewer.cpp"> <ClCompile Include="gui\GameListViewer.cpp">
<Filter>gui</Filter> <Filter>gui</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="core\FsFile.cpp">
<Filter>Core</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtUic Include="gui\shadps4gui.ui"> <QtUic Include="gui\shadps4gui.ui">
@ -49,4 +55,12 @@
<Filter>gui</Filter> <Filter>gui</Filter>
</QtMoc> </QtMoc>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="Types.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="core\FsFile.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
</Project> </Project>