(hopefully) working initial Static.File class

This commit is contained in:
mrexodia 2015-12-28 01:53:46 +01:00
parent a006043c26
commit 470396f736
4 changed files with 147 additions and 0 deletions

View File

@ -163,6 +163,7 @@
<ClCompile Include="Debugger.Thread.HardwareBreakpoint.cpp" />
<ClCompile Include="Debugger.Thread.Registers.cpp" />
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
<ClCompile Include="Static.File.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Debugger.Breakpoint.h" />
@ -175,6 +176,7 @@
<ClInclude Include="Debugger.Thread.Registers.h" />
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
<ClInclude Include="GleeBug.h" />
<ClInclude Include="Static.File.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -62,6 +62,9 @@
<ClCompile Include="Debugger.Thread.HardwareBreakpoint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Static.File.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Debugger.h">
@ -94,5 +97,8 @@
<ClInclude Include="Debugger.Breakpoint.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Static.File.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

101
GleeBug/Static.File.cpp Normal file
View File

@ -0,0 +1,101 @@
#include "Static.File.h"
namespace GleeBug
{
File::File(const wchar_t* szFileName, File::Mode mode)
: _fileName(szFileName ? szFileName : L""),
_mode(mode),
_hFile(INVALID_HANDLE_VALUE)
{
}
File::~File()
{
Close();
}
bool File::Open()
{
return internalOpen(OPEN_EXISTING);
}
bool File::Create(bool overwrite)
{
return internalOpen(overwrite ? CREATE_ALWAYS : CREATE_NEW);
}
bool File::IsOpen() const
{
return _hFile != INVALID_HANDLE_VALUE;
}
void File::Close()
{
if (IsOpen())
{
CloseHandle(_hFile);
_hFile = INVALID_HANDLE_VALUE;
}
}
uint32 File::GetSize()
{
return IsOpen() ? GetFileSize(_hFile, nullptr) : 0;
}
bool File::Read(uint32 offset, uint8* data, uint32 size, uint32* bytesRead)
{
if (!IsOpen() || !SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN))
{
if (bytesRead)
*bytesRead = 0;
return false;
}
DWORD NumberOfBytesRead = 0;
auto result = !!ReadFile(_hFile, data, size, &NumberOfBytesRead, nullptr);
if (bytesRead)
*bytesRead = uint32(NumberOfBytesRead);
return result;
}
bool File::Write(uint32 offset, const uint8* data, uint32 size, uint32* bytesWritten)
{
if (!IsOpen() || !SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN))
{
if (bytesWritten)
*bytesWritten = 0;
return false;
}
DWORD NumberOfBytesWritten = 0;
auto result = !!WriteFile(_hFile, data, size, &NumberOfBytesWritten, nullptr);
if (bytesWritten)
*bytesWritten = uint32(NumberOfBytesWritten);
return result;
}
bool File::internalOpen(DWORD creation)
{
//get the access and sharemode flags
DWORD access, sharemode;
switch (_mode)
{
case ReadOnly:
access = GENERIC_READ;
sharemode = FILE_SHARE_READ;
break;
case ReadWrite:
access = GENERIC_READ | GENERIC_WRITE;
sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
break;
default:
return false;
}
//close the previous file
Close();
//use WinAPI to get the file handle
_hFile = CreateFileW(_fileName.c_str(), access, sharemode, nullptr, creation, 0, nullptr);
return IsOpen();
}
};

38
GleeBug/Static.File.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef _STATIC_FILE_H
#define _STATIC_FILE_H
#include "Static.Global.h"
namespace GleeBug
{
class File
{
public:
enum Mode
{
ReadOnly,
ReadWrite
};
explicit File(const wchar_t* szFileName, Mode mode = ReadOnly);
~File();
bool Open();
bool Create(bool overwrite = true);
bool IsOpen() const;
void Close();
uint32 GetSize();
bool Read(uint32 offset, uint8* data, uint32 size, uint32* bytesRead = nullptr);
bool Write(uint32 offset, const uint8* data, uint32 size, uint32* bytesWritten = nullptr);
private:
bool internalOpen(DWORD creation);
std::wstring _fileName;
Mode _mode;
HANDLE _hFile;
};
};
#endif //_STATIC_FILE_H