Compare commits
6 Commits
79650e390e
...
f3e03710f6
Author | SHA1 | Date |
---|---|---|
BodgeMaster | f3e03710f6 | |
BodgeMaster | 72fc923839 | |
BodgeMaster | 5059bd0193 | |
BodgeMaster | 8bb633f118 | |
BodgeMaster | ec44ac9531 | |
BodgeMaster | 341b4c187e |
|
@ -91,8 +91,7 @@ namespace ErrorCodes {
|
||||||
|
|
||||||
const uint8_t INVALID_TYPE = 8;
|
const uint8_t INVALID_TYPE = 8;
|
||||||
|
|
||||||
//file errors
|
const uint8_t FILE_NOT_OPEN = 9;
|
||||||
const uint8_t FILE_READ_FAILED = 9;
|
|
||||||
const uint8_t FILE_NOT_FOUND = 10;
|
const uint8_t FILE_NOT_FOUND = 10;
|
||||||
|
|
||||||
const uint8_t UNIMPLEMENTED = 254;
|
const uint8_t UNIMPLEMENTED = 254;
|
||||||
|
|
|
@ -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->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
|
||||||
this->open();
|
this->open();
|
||||||
this->isOpen = true;
|
this->isOpen = true;
|
||||||
this->eof = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,36 +56,60 @@ void File::close(){
|
||||||
this->isOpen = false;
|
this->isOpen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<uint8_t> File::readByte(){
|
bool File::eof() {
|
||||||
this->fileStream.seekg(this->cursorPosition);
|
return !this->size.isError && this->cursorPosition >= this->size.value;
|
||||||
|
}
|
||||||
|
|
||||||
char* byte = new char;
|
ErrorOr<uint8_t> File::readByte(){
|
||||||
try{
|
uint8_t* nextPointer = new uint8_t;
|
||||||
this->fileStream.read(byte, 1);
|
uint8_t nextByte;
|
||||||
}catch(std::exception& e){
|
bool failure = false;
|
||||||
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
|
|
||||||
|
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;
|
try {
|
||||||
return ErrorOr<uint8_t>((uint8_t) *byte);
|
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){
|
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
|
||||||
this->fileStream.seekg(this->cursorPosition);
|
uint8_t* buffer = new uint8_t[bytes];
|
||||||
|
|
||||||
char* buffer = new char[bytes];
|
|
||||||
std::vector<uint8_t> data;
|
std::vector<uint8_t> data;
|
||||||
try{
|
bool failure = false;
|
||||||
this->fileStream.read(buffer, bytes);
|
|
||||||
for(uint64_t i=0; i<bytes; i++){
|
if (!this->isOpen) {
|
||||||
data.push_back((uint8_t) buffer[i]);
|
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
|
||||||
}
|
|
||||||
delete[] buffer;
|
|
||||||
}catch(std::exception& e){
|
|
||||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
|
|
||||||
}
|
}
|
||||||
this->cursorPosition += bytes;
|
if (!this->size.isError && this->cursorPosition >= this->size.value+bytes) {
|
||||||
return ErrorOr<std::vector<uint8_t>>(data);
|
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){
|
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
|
||||||
|
|
|
@ -39,7 +39,6 @@ class File {
|
||||||
File(std::string path, char mode, uint64_t cursorPosition);
|
File(std::string path, char mode, uint64_t cursorPosition);
|
||||||
public:
|
public:
|
||||||
bool isOpen;
|
bool isOpen;
|
||||||
bool eof;
|
|
||||||
std::string path;
|
std::string path;
|
||||||
uint64_t cursorPosition;
|
uint64_t cursorPosition;
|
||||||
// may be error if not a regular file or size cannot be determined
|
// may be error if not a regular file or size cannot be determined
|
||||||
|
@ -49,15 +48,12 @@ class File {
|
||||||
void open();
|
void open();
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
|
bool eof();
|
||||||
|
|
||||||
// only applicable to read and edit modes
|
// only applicable to read and edit modes
|
||||||
// moves the cursor to the right of the read section
|
// moves the cursor to the right of the read section
|
||||||
ErrorOr<uint8_t> readByte();
|
ErrorOr<uint8_t> readByte();
|
||||||
ErrorOr<std::vector<uint8_t>> read(uint64_t bytes);
|
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);
|
ErrorOr<tiny_utf8::string> readString(uint64_t bytes);
|
||||||
|
|
||||||
// only applicable to write, modify, append, and edit modes
|
// only applicable to write, modify, append, and edit modes
|
||||||
|
|
Loading…
Reference in New Issue