-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCFile.cpp
59 lines (48 loc) · 1.12 KB
/
CFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "CFile.h"
PurrFX::CFile::CFile(const pathstring& i_sPath, EMode i_eMode)
{
#ifdef _WIN32
const pathchar_t* sMode = (i_eMode == Read ? L"rb" : L"wb");
_wfopen_s(&m_pFile, i_sPath.data(), sMode);
#else
const pathchar_t* sMode = (i_eMode == Read ? "rb" : "wb");
m_pFile = fopen(i_sPath.data(), sMode);
#endif
}
PurrFX::CFile::~CFile()
{
if (m_pFile != nullptr)
fclose(m_pFile);
}
size_t PurrFX::CFile::read(void* o_pBuffer, size_t i_nBytes)
{
#ifdef _WIN32
return fread_s(o_pBuffer, i_nBytes, 1, i_nBytes, m_pFile);
#else
return fread(o_pBuffer, 1, i_nBytes, m_pFile);
#endif
}
size_t PurrFX::CFile::write(const void* i_pBuffer, size_t i_nBytes)
{
return fwrite(i_pBuffer, 1, i_nBytes, m_pFile);
}
bool PurrFX::CFile::isOpened() const
{
return (m_pFile != nullptr);
}
size_t PurrFX::CFile::size() const
{
if (!isOpened())
return 0;
// Save position
auto nPosition = ftell(m_pFile);
fseek(m_pFile, 0, SEEK_END);
auto nFileSize = ftell(m_pFile);
// Restore position
fseek(m_pFile, nPosition, SEEK_SET);
return size_t(nFileSize < 0 ? 0: nFileSize);
}
PurrFX::CFile::operator FILE* () const
{
return m_pFile;
}