Compare commits

..

No commits in common. "f3e03710f60028c203a7d98d7edcd9363a5f2069" and "79650e390eaa9e182667cc829ed677a472b24781" have entirely different histories.

3 changed files with 30 additions and 48 deletions

View File

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

View File

@ -29,6 +29,7 @@ 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;
}
@ -56,60 +57,36 @@ void File::close(){
this->isOpen = false;
}
bool File::eof() {
return !this->size.isError && this->cursorPosition >= this->size.value;
}
ErrorOr<uint8_t> File::readByte(){
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
this->fileStream.seekg(this->cursorPosition);
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);
char* byte = new char;
try{
this->fileStream.read(byte, 1);
}catch(std::exception& e){
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
}
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);
this->cursorPosition += 1;
return ErrorOr<uint8_t>((uint8_t) *byte);
}
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
uint8_t* buffer = new uint8_t[bytes];
this->fileStream.seekg(this->cursorPosition);
char* buffer = new char[bytes];
std::vector<uint8_t> data;
bool failure = false;
if (!this->isOpen) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
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);
}
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);
this->cursorPosition += bytes;
return ErrorOr<std::vector<uint8_t>>(data);
}
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){

View File

@ -39,6 +39,7 @@ 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
@ -48,12 +49,15 @@ 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