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;
|
||||
|
||||
//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;
|
||||
|
|
|
@ -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){
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue