Skip to content

Commit

Permalink
Use platform-specific methods for FileAccess reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueCube3310 committed Oct 29, 2023
1 parent f497156 commit 6b2cf48
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/typedefs.h
Expand Up @@ -232,6 +232,10 @@ constexpr T get_num_bits(T x) {
#define BSWAP16(x) __builtin_bswap16(x)
#define BSWAP32(x) __builtin_bswap32(x)
#define BSWAP64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER)
#define BSWAP16(x) _byteswap_ushort(x)
#define BSWAP32(x) _byteswap_ulong(x)
#define BSWAP64(x) _byteswap_uint64(x)
#else
static inline uint16_t BSWAP16(uint16_t x) {
return (x >> 8) | (x << 8);
Expand Down
75 changes: 75 additions & 0 deletions drivers/unix/file_access_unix.cpp
Expand Up @@ -228,6 +228,51 @@ uint8_t FileAccessUnix::get_8() const {
return b;
}

uint16_t FileAccessUnix::get_16() const {
ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");

uint16_t b = 0;
if (fread(&b, 1, 2, f) != 2) {
check_errors();
}

if (big_endian) {
b = BSWAP16(b);
}

return b;
}

uint32_t FileAccessUnix::get_32() const {
ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");

uint32_t b = 0;
if (fread(&b, 1, 4, f) != 4) {
check_errors();
}

if (big_endian) {
b = BSWAP32(b);
}

return b;
}

uint64_t FileAccessUnix::get_64() const {
ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");

uint64_t b = 0;
if (fread(&b, 1, 8, f) != 8) {
check_errors();
}

if (big_endian) {
b = BSWAP64(b);
}

return b;
}

uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
ERR_FAIL_NULL_V_MSG(f, -1, "File must be opened before use.");
Expand All @@ -251,6 +296,36 @@ void FileAccessUnix::store_8(uint8_t p_dest) {
ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
}

void FileAccessUnix::store_16(uint16_t p_dest) {
ERR_FAIL_NULL_MSG(f, "File must be opened before use.");

if (big_endian) {
p_dest = BSWAP16(p_dest);
}

ERR_FAIL_COND(fwrite(&p_dest, 1, 2, f) != 2);
}

void FileAccessUnix::store_32(uint32_t p_dest) {
ERR_FAIL_NULL_MSG(f, "File must be opened before use.");

if (big_endian) {
p_dest = BSWAP32(p_dest);
}

ERR_FAIL_COND(fwrite(&p_dest, 1, 4, f) != 4);
}

void FileAccessUnix::store_64(uint64_t p_dest) {
ERR_FAIL_NULL_MSG(f, "File must be opened before use.");

if (big_endian) {
p_dest = BSWAP64(p_dest);
}

ERR_FAIL_COND(fwrite(&p_dest, 1, 8, f) != 8);
}

void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
ERR_FAIL_COND(!p_src && p_length > 0);
Expand Down
6 changes: 6 additions & 0 deletions drivers/unix/file_access_unix.h
Expand Up @@ -68,12 +68,18 @@ class FileAccessUnix : public FileAccess {
virtual bool eof_reached() const override; ///< reading passed EOF

virtual uint8_t get_8() const override; ///< get a byte
virtual uint16_t get_16() const override;
virtual uint32_t get_32() const override;
virtual uint64_t get_64() const override;
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;

virtual Error get_error() const override; ///< get last error

virtual void flush() override;
virtual void store_8(uint8_t p_dest) override; ///< store a byte
virtual void store_16(uint16_t p_dest) override;
virtual void store_32(uint32_t p_dest) override;
virtual void store_64(uint64_t p_dest) override;
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) override; ///< store an array of bytes

virtual bool file_exists(const String &p_path) override; ///< return true if a file exists
Expand Down
123 changes: 123 additions & 0 deletions drivers/windows/file_access_windows.cpp
Expand Up @@ -284,6 +284,72 @@ uint8_t FileAccessWindows::get_8() const {
return b;
}

uint16_t FileAccessWindows::get_16() const {
ERR_FAIL_NULL_V(f, 0);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) {
fflush(f);
}
prev_op = READ;
}

uint16_t b = 0;
if (fread(&b, 1, 2, f) != 2) {
check_errors();
}

if (big_endian) {
b = BSWAP16(b);
}

return b;
}

uint32_t FileAccessWindows::get_32() const {
ERR_FAIL_NULL_V(f, 0);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) {
fflush(f);
}
prev_op = READ;
}

uint32_t b = 0;
if (fread(&b, 1, 4, f) != 4) {
check_errors();
}

if (big_endian) {
b = BSWAP32(b);
}

return b;
}

uint64_t FileAccessWindows::get_64() const {
ERR_FAIL_NULL_V(f, 0);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) {
fflush(f);
}
prev_op = READ;
}

uint64_t b = 0;
if (fread(&b, 1, 8, f) != 8) {
check_errors();
}

if (big_endian) {
b = BSWAP64(b);
}

return b;
}

uint64_t FileAccessWindows::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
ERR_FAIL_NULL_V(f, -1);
Expand Down Expand Up @@ -326,6 +392,63 @@ void FileAccessWindows::store_8(uint8_t p_dest) {
fwrite(&p_dest, 1, 1, f);
}

void FileAccessWindows::store_16(uint16_t p_dest) {
ERR_FAIL_NULL(f);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
fseek(f, 0, SEEK_CUR);
}
}
prev_op = WRITE;
}

if (big_endian) {
p_dest = BSWAP16(p_dest);
}

fwrite(&p_dest, 1, 2, f);
}

void FileAccessWindows::store_32(uint32_t p_dest) {
ERR_FAIL_NULL(f);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
fseek(f, 0, SEEK_CUR);
}
}
prev_op = WRITE;
}

if (big_endian) {
p_dest = BSWAP32(p_dest);
}

fwrite(&p_dest, 1, 4, f);
}

void FileAccessWindows::store_64(uint64_t p_dest) {
ERR_FAIL_NULL(f);

if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
fseek(f, 0, SEEK_CUR);
}
}
prev_op = WRITE;
}

if (big_endian) {
p_dest = BSWAP64(p_dest);
}

fwrite(&p_dest, 1, 8, f);
}

void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_NULL(f);
ERR_FAIL_COND(!p_src && p_length > 0);
Expand Down
6 changes: 6 additions & 0 deletions drivers/windows/file_access_windows.h
Expand Up @@ -69,12 +69,18 @@ class FileAccessWindows : public FileAccess {
virtual bool eof_reached() const override; ///< reading passed EOF

virtual uint8_t get_8() const override; ///< get a byte
virtual uint16_t get_16() const override;
virtual uint32_t get_32() const override;
virtual uint64_t get_64() const override;
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;

virtual Error get_error() const override; ///< get last error

virtual void flush() override;
virtual void store_8(uint8_t p_dest) override; ///< store a byte
virtual void store_16(uint16_t p_dest) override;
virtual void store_32(uint32_t p_dest) override;
virtual void store_64(uint64_t p_dest) override;
virtual void store_buffer(const uint8_t *p_src, uint64_t p_length) override; ///< store an array of bytes

virtual bool file_exists(const String &p_name) override; ///< return true if a file exists
Expand Down
81 changes: 81 additions & 0 deletions platform/android/file_access_android.cpp
Expand Up @@ -121,6 +121,75 @@ uint8_t FileAccessAndroid::get_8() const {
return byte;
}

uint16_t FileAccessAndroid::get_16() const {
if (pos >= len) {
eof = true;
return 0;
}

uint16_t bytes = 0;
int r = AAsset_read(asset, &bytes, 2);

if (r >= 0) {
pos += r;
if (pos >= len) {
eof = true;
}
}

if (big_endian) {
bytes = BSWAP16(bytes);
}

return bytes;
}

uint32_t FileAccessAndroid::get_32() const {
if (pos >= len) {
eof = true;
return 0;
}

uint32_t bytes = 0;
int r = AAsset_read(asset, &bytes, 4);

if (r >= 0) {
pos += r;
if (pos >= len) {
eof = true;
}
}

if (big_endian) {
bytes = BSWAP32(bytes);
}

return bytes;
}

uint64_t FileAccessAndroid::get_64() const {
if (pos >= len) {
eof = true;
return 0;
}

uint64_t bytes = 0;
int r = AAsset_read(asset, &bytes, 8);

if (r >= 0) {
pos += r;
if (pos >= len) {
eof = true;
}
}

if (big_endian) {
bytes = BSWAP64(bytes);
}

return bytes;
}

uint64_t FileAccessAndroid::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);

Expand Down Expand Up @@ -151,6 +220,18 @@ void FileAccessAndroid::store_8(uint8_t p_dest) {
ERR_FAIL();
}

void FileAccessAndroid::store_16(uint16_t p_dest) {
ERR_FAIL();
}

void FileAccessAndroid::store_32(uint32_t p_dest) {
ERR_FAIL();
}

void FileAccessAndroid::store_64(uint64_t p_dest) {
ERR_FAIL();
}

bool FileAccessAndroid::file_exists(const String &p_path) {
String path = fix_path(p_path).simplify_path();
if (path.begins_with("/")) {
Expand Down
6 changes: 6 additions & 0 deletions platform/android/file_access_android.h
Expand Up @@ -66,12 +66,18 @@ class FileAccessAndroid : public FileAccess {
virtual bool eof_reached() const override; // reading passed EOF

virtual uint8_t get_8() const override; // get a byte
virtual uint16_t get_16() const override;
virtual uint32_t get_32() const override;
virtual uint64_t get_64() const override;
virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;

virtual Error get_error() const override; // get last error

virtual void flush() override;
virtual void store_8(uint8_t p_dest) override; // store a byte
virtual void store_16(uint16_t p_dest) override;
virtual void store_32(uint32_t p_dest) override;
virtual void store_64(uint64_t p_dest) override;

virtual bool file_exists(const String &p_path) override; // return true if a file exists

Expand Down

0 comments on commit 6b2cf48

Please sign in to comment.