Compare commits

...

6 Commits

Author SHA1 Message Date
BodgeMaster f3e03710f6 lib/file: Ensure a file is actually open before attempting to read 2022-10-05 04:07:46 +02:00
BodgeMaster 72fc923839 lib/nbt: remove now useless comment 2022-10-05 04:02:32 +02:00
BodgeMaster 5059bd0193 lib/file: change File::eof to a function
This is just way easier to implement and less messy.
2022-10-05 04:01:18 +02:00
BodgeMaster 8bb633f118 lib/error and lib/file: reassign code 9 to FILE_NOT_OPEN
These are not exceptions so we don’t need multiple variants of
generic error in case someone wants to catch it somewhere
up the call stack.

It’s okay to use UNKNOWN for generic errors where we don’t know
exactly what happened.
2022-10-05 03:48:05 +02:00
BodgeMaster ec44ac9531 lib/file: Check if there are enough bytes left to read 2022-10-05 03:46:42 +02:00
BodgeMaster 341b4c187e lib/file: Fix (potential) memory leaks 2022-10-05 03:30:01 +02:00
3 changed files with 49 additions and 31 deletions

View File

@ -91,8 +91,7 @@ namespace ErrorCodes {
const uint8_t INVALID_TYPE = 8;
//file errors
const uint8_t FILE_READ_FAILED = 9;
const uint8_t FILE_NOT_OPEN = 9;
const uint8_t FILE_NOT_FOUND = 10;
const uint8_t UNIMPLEMENTED = 254;

View File

@ -29,7 +29,6 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
this->open();
this->isOpen = true;
this->eof = false;
}
@ -57,36 +56,60 @@ void File::close(){
this->isOpen = false;
}
ErrorOr<uint8_t> File::readByte(){
this->fileStream.seekg(this->cursorPosition);
bool File::eof() {
return !this->size.isError && this->cursorPosition >= this->size.value;
}
char* byte = new char;
try{
this->fileStream.read(byte, 1);
}catch(std::exception& e){
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
ErrorOr<uint8_t> File::readByte(){
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
if (!this->isOpen) {
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN);
}
if (!this->size.isError && this->cursorPosition >= this->size.value) {
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
}
this->cursorPosition += 1;
return ErrorOr<uint8_t>((uint8_t) *byte);
try {
this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1);
nextByte = *nextPointer;
this->cursorPosition++;
} catch (std::exception& e) {
failure = true;
}
delete nextPointer;
return failure? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(nextByte);
}
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
this->fileStream.seekg(this->cursorPosition);
char* buffer = new char[bytes];
uint8_t* buffer = new uint8_t[bytes];
std::vector<uint8_t> data;
try{
this->fileStream.read(buffer, bytes);
for(uint64_t i=0; i<bytes; i++){
data.push_back((uint8_t) buffer[i]);
}
delete[] buffer;
}catch(std::exception& e){
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
bool failure = false;
if (!this->isOpen) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
}
this->cursorPosition += bytes;
return ErrorOr<std::vector<uint8_t>>(data);
if (!this->size.isError && this->cursorPosition >= this->size.value+bytes) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN);
}
try {
this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(buffer), bytes);
data = std::vector<uint8_t>(buffer, buffer+bytes);
this->cursorPosition += bytes;
} catch (std::exception& e) {
failure = true;
}
delete[] buffer;
return failure? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) : ErrorOr<std::vector<uint8_t>>(data);
}
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){

View File

@ -39,7 +39,6 @@ class File {
File(std::string path, char mode, uint64_t cursorPosition);
public:
bool isOpen;
bool eof;
std::string path;
uint64_t cursorPosition;
// may be error if not a regular file or size cannot be determined
@ -49,15 +48,12 @@ class File {
void open();
void close();
bool eof();
// only applicable to read and edit modes
// moves the cursor to the right of the read section
ErrorOr<uint8_t> readByte();
ErrorOr<std::vector<uint8_t>> read(uint64_t bytes);
// @SodaFountain (or whoever ends up implementing this):
// Tiny-UTF8 uses UTF-32 internally IIRC.
// There is a constructor for it that takes std::string and
// converts it to the internal format.
//TODO: remove this comment when it served its purpose
ErrorOr<tiny_utf8::string> readString(uint64_t bytes);
// only applicable to write, modify, append, and edit modes