Flappy-Bird_GX2/src/fs/CFile.cpp

173 lines
3.3 KiB
C++
Raw Normal View History

2016-10-05 16:03:29 +02:00
#include <stdarg.h>
#include <stdlib.h>
2019-08-15 11:21:02 +02:00
#include <stdio.h>
#include <strings.h>
#include <fs/CFile.hpp>
CFile::CFile() {
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
CFile::CFile(const std::string &filepath, eOpenTypes mode) {
2019-08-15 11:21:02 +02:00
iFd = -1;
this->open(filepath, mode);
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
CFile::CFile(const uint8_t *mem, int32_t size) {
2019-08-15 11:21:02 +02:00
iFd = -1;
this->open(mem, size);
2016-10-05 16:03:29 +02:00
}
2019-08-15 11:21:02 +02:00
CFile::~CFile() {
this->close();
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
int32_t CFile::open(const std::string &filepath, eOpenTypes mode) {
2019-08-15 11:21:02 +02:00
this->close();
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
int32_t openMode = 0;
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
switch (mode) {
default:
case ReadOnly:
openMode = O_RDONLY;
break;
case WriteOnly:
openMode = O_WRONLY;
break;
case ReadWrite:
openMode = O_RDWR;
break;
case Append:
openMode = O_APPEND | O_WRONLY;
break;
2019-08-15 11:21:02 +02:00
}
2016-10-05 16:03:29 +02:00
//! Using fopen works only on the first launch as expected
//! on the second launch it causes issues because we don't overwrite
//! the .data sections which is needed for a normal application to re-init
//! this will be added with launching as RPX
2019-08-15 11:21:02 +02:00
iFd = ::open(filepath.c_str(), openMode);
2020-07-05 13:34:13 +02:00
if (iFd < 0)
2019-08-15 11:21:02 +02:00
return iFd;
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
filesize = ::lseek(iFd, 0, SEEK_END);
::lseek(iFd, 0, SEEK_SET);
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
return 0;
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
int32_t CFile::open(const uint8_t *mem, int32_t size) {
2019-08-15 11:21:02 +02:00
this->close();
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
mem_file = mem;
filesize = size;
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
return 0;
2016-10-05 16:03:29 +02:00
}
2019-08-15 11:21:02 +02:00
void CFile::close() {
2020-07-05 13:34:13 +02:00
if (iFd >= 0)
2019-08-15 11:21:02 +02:00
::close(iFd);
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
int32_t CFile::read(uint8_t *ptr, size_t size) {
if (iFd >= 0) {
int32_t ret = ::read(iFd, ptr, size);
if (ret > 0)
2019-08-15 11:21:02 +02:00
pos += ret;
return ret;
}
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
int32_t readsize = size;
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
if (readsize > (int64_t) (filesize - pos))
readsize = filesize - pos;
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
if (readsize <= 0)
2019-08-15 11:21:02 +02:00
return readsize;
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
if (mem_file != NULL) {
memcpy(ptr, mem_file + pos, readsize);
2019-08-15 11:21:02 +02:00
pos += readsize;
return readsize;
}
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
return -1;
2016-10-05 16:03:29 +02:00
}
2020-07-05 13:34:13 +02:00
int32_t CFile::write(const uint8_t *ptr, size_t size) {
if (iFd >= 0) {
2019-08-15 11:21:02 +02:00
size_t done = 0;
2020-07-05 13:34:13 +02:00
while (done < size) {
2019-08-15 11:21:02 +02:00
int32_t ret = ::write(iFd, ptr, size - done);
2020-07-05 13:34:13 +02:00
if (ret <= 0)
2016-10-05 16:03:29 +02:00
return ret;
ptr += ret;
done += ret;
pos += ret;
}
2019-08-15 11:21:02 +02:00
return done;
}
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
return -1;
2016-10-05 16:03:29 +02:00
}
2019-08-15 11:21:02 +02:00
int32_t CFile::seek(long int offset, int32_t origin) {
int32_t ret = 0;
int64_t newPos = pos;
2020-07-05 13:34:13 +02:00
if (origin == SEEK_SET) {
2019-08-15 11:21:02 +02:00
newPos = offset;
2020-07-05 13:34:13 +02:00
} else if (origin == SEEK_CUR) {
2019-08-15 11:21:02 +02:00
newPos += offset;
2020-07-05 13:34:13 +02:00
} else if (origin == SEEK_END) {
newPos = filesize + offset;
2019-08-15 11:21:02 +02:00
}
2020-07-05 13:34:13 +02:00
if (newPos < 0) {
2019-08-15 11:21:02 +02:00
pos = 0;
} else {
2016-10-05 16:03:29 +02:00
pos = newPos;
2019-08-15 11:21:02 +02:00
}
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
if (iFd >= 0)
2019-08-15 11:21:02 +02:00
ret = ::lseek(iFd, pos, SEEK_SET);
2016-10-05 16:03:29 +02:00
2020-07-05 13:34:13 +02:00
if (mem_file != NULL) {
if (pos > filesize) {
2019-08-15 11:21:02 +02:00
pos = filesize;
}
}
2016-10-05 16:03:29 +02:00
2019-08-15 11:21:02 +02:00
return ret;
2016-10-05 16:03:29 +02:00
}
2019-08-15 11:21:02 +02:00
int32_t CFile::fwrite(const char *format, ...) {
char tmp[512];
tmp[0] = 0;
int32_t result = -1;
va_list va;
va_start(va, format);
2020-07-05 13:34:13 +02:00
if ((vsprintf(tmp, format, va) >= 0)) {
result = this->write((uint8_t *) tmp, strlen(tmp));
2019-08-15 11:21:02 +02:00
}
va_end(va);
2016-10-05 16:03:29 +02:00
return result;
}