|
|
@@ -9,10 +9,10 @@
|
|
|
#include "types.h"
|
|
|
#include "error.h"
|
|
|
#include "macros.h"
|
|
|
-#include "config.h"
|
|
|
+#include "platform.h"
|
|
|
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
- #include <cstdio>
|
|
|
+ #include <stdio.h>
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
#include "tchar.h"
|
|
|
#include "win_headers.h"
|
|
|
@@ -27,24 +27,14 @@ class OsFile
|
|
|
public:
|
|
|
|
|
|
/// Opens the file located at @a path with the given @a mode.
|
|
|
- OsFile(const char* path, FileOpenMode mode)
|
|
|
-#if CROWN_PLATFORM_WINDOWS
|
|
|
- : _eof(false)
|
|
|
-#endif
|
|
|
- {
|
|
|
+ OsFile()
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
- _file = fopen(path, (mode == FOM_READ) ? "rb" : "wb");
|
|
|
- CE_ASSERT(_file != NULL, "Unable to open file: %s", path);
|
|
|
+ : _file(NULL)
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
- _file = CreateFile(path,
|
|
|
- mode == FOM_READ ? GENERIC_READ : GENERIC_WRITE,
|
|
|
- 0,
|
|
|
- NULL,
|
|
|
- OPEN_ALWAYS,
|
|
|
- FILE_ATTRIBUTE_NORMAL,
|
|
|
- NULL);
|
|
|
- CE_ASSERT(_file != INVALID_HANDLE_VALUE, "Unable to open file: %s", path);
|
|
|
+ : _file(INVALID_HANDLE_VALUE)
|
|
|
+ , _eof(false)
|
|
|
#endif
|
|
|
+ {
|
|
|
}
|
|
|
|
|
|
~OsFile()
|
|
|
@@ -52,6 +42,23 @@ public:
|
|
|
close();
|
|
|
}
|
|
|
|
|
|
+ void open(const char* path, FileOpenMode mode)
|
|
|
+ {
|
|
|
+#if CROWN_PLATFORM_POSIX
|
|
|
+ _file = fopen(path, (mode == FOM_READ) ? "rb" : "wb");
|
|
|
+ CE_ASSERT(_file != NULL, "Unable to open file: %s", path);
|
|
|
+#elif CROWN_PLATFORM_WINDOWS
|
|
|
+ _file = CreateFile(path
|
|
|
+ , mode == FOM_READ ? GENERIC_READ : GENERIC_WRITE,
|
|
|
+ , 0
|
|
|
+ , NULL
|
|
|
+ , OPEN_ALWAYS
|
|
|
+ , FILE_ATTRIBUTE_NORMAL
|
|
|
+ , NULL);
|
|
|
+ CE_ASSERT(_file != INVALID_HANDLE_VALUE, "Unable to open file: %s", path);
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
/// Closes the file.
|
|
|
void close()
|
|
|
{
|
|
|
@@ -65,32 +72,36 @@ public:
|
|
|
if (is_open())
|
|
|
{
|
|
|
CloseHandle(_file);
|
|
|
- _file = NULL;
|
|
|
+ _file = INVALID_HANDLE_VALUE;
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
bool is_open() const
|
|
|
{
|
|
|
+#if CROWN_PLATFORM_POSIX
|
|
|
return _file != NULL;
|
|
|
+#elif CROWN_PLATFORM_WINDOWS
|
|
|
+ return _file != INVALID_HANDLE_VALUE;
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
/// Return the size of the file in bytes.
|
|
|
- size_t size() const
|
|
|
+ uint32_t size() const
|
|
|
{
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
size_t pos = position();
|
|
|
|
|
|
- int fseek_result = fseek(_file, 0, SEEK_END);
|
|
|
- CE_ASSERT(fseek_result == 0, "Failed to seek");
|
|
|
+ int err = fseek(_file, 0, SEEK_END);
|
|
|
+ CE_ASSERT(err == 0, "Failed to seek");
|
|
|
|
|
|
size_t size = position();
|
|
|
|
|
|
- fseek_result = fseek(_file, (long) pos, SEEK_SET);
|
|
|
- CE_ASSERT(fseek_result == 0, "Failed to seek");
|
|
|
- CE_UNUSED(fseek_result);
|
|
|
+ err = fseek(_file, (long)pos, SEEK_SET);
|
|
|
+ CE_ASSERT(err == 0, "Failed to seek");
|
|
|
+ CE_UNUSED(err);
|
|
|
|
|
|
- return size;
|
|
|
+ return (uint32_t)size;
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
return GetFileSize(_file, NULL);
|
|
|
#endif
|
|
|
@@ -98,22 +109,18 @@ public:
|
|
|
|
|
|
/// Reads @a size bytes from the file and stores it into @a data.
|
|
|
/// Returns the number of bytes read.
|
|
|
- size_t read(void* data, size_t size)
|
|
|
+ uint32_t read(void* data, uint32_t size)
|
|
|
{
|
|
|
CE_ASSERT(data != NULL, "Data must be != NULL");
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
return fread(data, 1, size, _file);
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
DWORD bytes_read;
|
|
|
-
|
|
|
BOOL result = ReadFile(_file, data, size, &bytes_read, NULL);
|
|
|
-
|
|
|
CE_ASSERT(result == TRUE, "Unable to read from file");
|
|
|
|
|
|
if (result && bytes_read == 0)
|
|
|
- {
|
|
|
_eof = true;
|
|
|
- }
|
|
|
|
|
|
return bytes_read;
|
|
|
#endif
|
|
|
@@ -121,7 +128,7 @@ public:
|
|
|
|
|
|
/// Writes @a size bytes of data stored in @a data and returns the
|
|
|
/// number of bytes written.
|
|
|
- size_t write(const void* data, size_t size)
|
|
|
+ uint32_t write(const void* data, uint32_t size)
|
|
|
{
|
|
|
CE_ASSERT(data != NULL, "Data must be != NULL");
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
@@ -135,7 +142,7 @@ public:
|
|
|
}
|
|
|
|
|
|
/// Moves the file pointer to the given @a position.
|
|
|
- void seek(size_t position)
|
|
|
+ void seek(uint32_t position)
|
|
|
{
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
int err = fseek(_file, (long) position, SEEK_SET);
|
|
|
@@ -162,31 +169,28 @@ public:
|
|
|
|
|
|
/// Moves the file pointer @a bytes bytes ahead the current
|
|
|
/// file pointer position.
|
|
|
- void skip(size_t bytes)
|
|
|
+ void skip(uint32_t bytes)
|
|
|
{
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
- int fseek_result = fseek(_file, bytes, SEEK_CUR);
|
|
|
- CE_ASSERT(fseek_result == 0, "Failed to seek");
|
|
|
- CE_UNUSED(fseek_result);
|
|
|
+ int err = fseek(_file, bytes, SEEK_CUR);
|
|
|
+ CE_ASSERT(err == 0, "Failed to seek");
|
|
|
+ CE_UNUSED(err);
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
- DWORD seek_result = SetFilePointer(_file, bytes, NULL, FILE_CURRENT);
|
|
|
-
|
|
|
- CE_ASSERT(seek_result != INVALID_SET_FILE_POINTER, "Failed to skip");
|
|
|
+ DWORD err = SetFilePointer(_file, bytes, NULL, FILE_CURRENT);
|
|
|
+ CE_ASSERT(err != INVALID_SET_FILE_POINTER, "Failed to skip");
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
/// Returns the position of the file pointer from the
|
|
|
/// start of the file in bytes.
|
|
|
- size_t position() const
|
|
|
+ uint32_t position() const
|
|
|
{
|
|
|
#if CROWN_PLATFORM_POSIX
|
|
|
- return (size_t) ftell(_file);
|
|
|
+ return (uint32_t)ftell(_file);
|
|
|
#elif CROWN_PLATFORM_WINDOWS
|
|
|
- DWORD position = SetFilePointer(_file, 0, NULL, FILE_CURRENT);
|
|
|
-
|
|
|
- CE_ASSERT(position != INVALID_SET_FILE_POINTER, "Failed to get position");
|
|
|
-
|
|
|
- return position;
|
|
|
+ DWORD pos = SetFilePointer(_file, 0, NULL, FILE_CURRENT);
|
|
|
+ CE_ASSERT(pos != INVALID_SET_FILE_POINTER, "Failed to get position");
|
|
|
+ return (uint32_t)pos;
|
|
|
#endif
|
|
|
}
|
|
|
|