fixed the File class.

This commit is contained in:
mrexodia 2015-12-28 22:09:54 +01:00
parent e36f85a49b
commit c2fe70969f
3 changed files with 82 additions and 14 deletions

View File

@ -38,14 +38,14 @@ namespace GleeBug
} }
} }
uint32 File::GetSize() uint32 File::GetSize() const
{ {
return IsOpen() ? GetFileSize(_hFile, nullptr) : 0; 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) if (bytesRead)
*bytesRead = 0; *bytesRead = 0;
@ -58,9 +58,9 @@ namespace GleeBug
return result; 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) if (bytesWritten)
*bytesWritten = 0; *bytesWritten = 0;

View File

@ -1,30 +1,84 @@
#ifndef _STATIC_FILE_H #ifndef _STATIC_FILE_H
#define _STATIC_FILE_H #define _STATIC_FILE_H
#include "GleeBug.h" #include "Static.Global.h"
namespace GleeBug namespace GleeBug
{ {
/**
\brief Class for simple File I/O.
*/
class File class File
{ {
public: public:
/**
\brief Possible I/O modes.
*/
enum Mode enum Mode
{ {
ReadOnly, ReadOnly,
ReadWrite 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); explicit File(const wchar_t* szFileName, Mode mode = ReadOnly);
~File();
bool Open(); /**
bool Create(bool overwrite = true); \brief Destructor.
bool IsOpen() const; */
void Close(); virtual ~File();
uint32 GetSize(); /**
bool Read(uint32 offset, uint8* data, uint32 size, uint32* bytesRead = nullptr); \brief Opens an existing file.
bool Write(uint32 offset, const uint8* data, uint32 size, uint32* bytesWritten = nullptr); \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: private:
bool internalOpen(DWORD creation); bool internalOpen(DWORD creation);

14
GleeBug/Static.Global.h Normal file
View File

@ -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