From c2fe70969f2ba1a5f15a5f18d8bd17b54f75b114 Mon Sep 17 00:00:00 2001 From: mrexodia Date: Mon, 28 Dec 2015 22:09:54 +0100 Subject: [PATCH] fixed the File class. --- GleeBug/Static.File.cpp | 10 +++--- GleeBug/Static.File.h | 72 +++++++++++++++++++++++++++++++++++------ GleeBug/Static.Global.h | 14 ++++++++ 3 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 GleeBug/Static.Global.h diff --git a/GleeBug/Static.File.cpp b/GleeBug/Static.File.cpp index 7168853..71df809 100644 --- a/GleeBug/Static.File.cpp +++ b/GleeBug/Static.File.cpp @@ -38,14 +38,14 @@ namespace GleeBug } } - uint32 File::GetSize() + uint32 File::GetSize() const { return IsOpen() ? GetFileSize(_hFile, nullptr) : 0; } - bool File::Read(uint32 offset, uint8* data, uint32 size, uint32* bytesRead) + bool File::Read(uint32 offset, void* data, uint32 size, uint32* bytesRead) const { - if (!IsOpen() || !SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN)) + if (!IsOpen() || SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { if (bytesRead) *bytesRead = 0; @@ -58,9 +58,9 @@ namespace GleeBug return result; } - bool File::Write(uint32 offset, const uint8* data, uint32 size, uint32* bytesWritten) + bool File::Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten) const { - if (!IsOpen() || !SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN)) + if (!IsOpen() || SetFilePointer(_hFile, offset, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { if (bytesWritten) *bytesWritten = 0; diff --git a/GleeBug/Static.File.h b/GleeBug/Static.File.h index 0320a37..2039cc3 100644 --- a/GleeBug/Static.File.h +++ b/GleeBug/Static.File.h @@ -1,30 +1,84 @@ #ifndef _STATIC_FILE_H #define _STATIC_FILE_H -#include "GleeBug.h" +#include "Static.Global.h" namespace GleeBug { + /** + \brief Class for simple File I/O. + */ class File { public: + /** + \brief Possible I/O modes. + */ enum Mode { ReadOnly, ReadWrite }; + /** + \brief Constructor. + \param szFileName Path of the file. + \param mode (Optional) the I/O mode. + */ explicit File(const wchar_t* szFileName, Mode mode = ReadOnly); - ~File(); - bool Open(); - bool Create(bool overwrite = true); - bool IsOpen() const; - void Close(); + /** + \brief Destructor. + */ + virtual ~File(); - 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); + /** + \brief Opens an existing file. + \return true if the file was opened successfully, false otherwise. + */ + virtual bool Open(); + + /** + \brief Creates a new file. + \param overwrite (Optional) true to overwrite, false to preserve fail if the file already exists. + \return true if the file was created, false otherwise. + */ + virtual bool Create(bool overwrite = true); + + /** + \brief Check if there is an open/created file. + */ + virtual bool IsOpen() const; + + /** + \brief Closes the file. + */ + virtual void Close(); + + /** + \brief Gets the size of the file. + */ + virtual uint32 GetSize() const; + + /** + \brief Reads from the open file. + \param offset The offset to start reading from. + \param [out] data Destination buffer. + \param size The size to read. + \param [out] bytesRead (Optional) If set, returns the number of bytes read. + \return true if the read was successful, false otherwise. + */ + virtual bool Read(uint32 offset, void* data, uint32 size, uint32* bytesRead = nullptr) const; + + /** + \brief Writes to the open file. + \param offset The offset to start writing to. Everything after this offset will be truncated! + \param data The data to write. + \param size The size to write. + \param [out] bytesWritten (Optional) If set, returns the number of bytes written. + \return true if the write was successful, false otherwise. + */ + virtual bool Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten = nullptr) const; private: bool internalOpen(DWORD creation); diff --git a/GleeBug/Static.Global.h b/GleeBug/Static.Global.h new file mode 100644 index 0000000..2493b39 --- /dev/null +++ b/GleeBug/Static.Global.h @@ -0,0 +1,14 @@ +#ifndef _STATIC_GLOBAL_H +#define _STATIC_GLOBAL_H + +#include "GleeBug.h" + +#define GLEEBUG_INVALID_VALUE uint32(0xFFFFFFFF) + +namespace GleeBug +{ + //constants + const uint32 INVALID_VALUE = GLEEBUG_INVALID_VALUE; +}; + +#endif //_STATIC_GLOBAL_H \ No newline at end of file