Compare commits
No commits in common. "f3e03710f60028c203a7d98d7edcd9363a5f2069" and "79650e390eaa9e182667cc829ed677a472b24781" have entirely different histories.
f3e03710f6
...
79650e390e
|
@ -91,7 +91,8 @@ namespace ErrorCodes {
|
||||||
|
|
||||||
const uint8_t INVALID_TYPE = 8;
|
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 FILE_NOT_FOUND = 10;
|
||||||
|
|
||||||
const uint8_t UNIMPLEMENTED = 254;
|
const uint8_t UNIMPLEMENTED = 254;
|
||||||
|
|
|
@ -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->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
|
||||||
this->open();
|
this->open();
|
||||||
this->isOpen = true;
|
this->isOpen = true;
|
||||||
|
this->eof = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,60 +57,36 @@ void File::close(){
|
||||||
this->isOpen = false;
|
this->isOpen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::eof() {
|
|
||||||
return !this->size.isError && this->cursorPosition >= this->size.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<uint8_t> File::readByte(){
|
ErrorOr<uint8_t> File::readByte(){
|
||||||
uint8_t* nextPointer = new uint8_t;
|
this->fileStream.seekg(this->cursorPosition);
|
||||||
uint8_t nextByte;
|
|
||||||
bool failure = false;
|
|
||||||
|
|
||||||
if (!this->isOpen) {
|
char* byte = new char;
|
||||||
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN);
|
try{
|
||||||
}
|
this->fileStream.read(byte, 1);
|
||||||
if (!this->size.isError && this->cursorPosition >= this->size.value) {
|
}catch(std::exception& e){
|
||||||
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
|
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
this->cursorPosition += 1;
|
||||||
this->fileStream.seekg(this->cursorPosition);
|
return ErrorOr<uint8_t>((uint8_t) *byte);
|
||||||
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){
|
||||||
uint8_t* buffer = new uint8_t[bytes];
|
this->fileStream.seekg(this->cursorPosition);
|
||||||
|
|
||||||
|
char* buffer = new char[bytes];
|
||||||
std::vector<uint8_t> data;
|
std::vector<uint8_t> data;
|
||||||
bool failure = false;
|
try{
|
||||||
|
this->fileStream.read(buffer, bytes);
|
||||||
if (!this->isOpen) {
|
for(uint64_t i=0; i<bytes; i++){
|
||||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
|
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) {
|
this->cursorPosition += bytes;
|
||||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN);
|
return ErrorOr<std::vector<uint8_t>>(data);
|
||||||
}
|
|
||||||
|
|
||||||
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,6 +39,7 @@ 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
|
||||||
|
@ -48,12 +49,15 @@ 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