Compare commits

..

No commits in common. "5920d1d0046369791d63fa69b32a6fa50cbdd5a0" and "8b1491c311009b5f6a0cd9061b8e055b22163d27" have entirely different histories.

2 changed files with 35 additions and 18 deletions

View File

@ -27,7 +27,22 @@
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
std::filesystem::path filePath = path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
this->open();
switch(this->mode){
case 'w':
case 'm':
this->fileStream.open(path, std::fstream::out | std::fstream::binary);
break;
case 'r':
case 'e':
this->fileStream.open(path, std::fstream::in | std::fstream::binary);
break;
case 'a':
this->fileStream.open(path, std::fstream::app | std::fstream::binary);
break;
default:
break;
}
this->isOpen = true;
this->eof = false;
}
@ -58,20 +73,29 @@ void File::close(){
}
ErrorOr<uint8_t> File::readByte(){
if(this->size.isError){
return ErrorOr<uint8_t>(true, this->size.errorCode);
}
this->cursorPosition += 1;
this->fileStream.seekg(this->cursorPosition);
char* byte = new char;
try{
this->fileStream.read(byte, 1);
delete byte;
}catch(std::exception& e){
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
}
this->cursorPosition += 1;
return ErrorOr<uint8_t>((uint8_t) *byte);
}
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
if(this->size.isError){
return ErrorOr<std::vector<uint8_t>>(true, this->size.errorCode);
}
this->cursorPosition += 1;
this->fileStream.seekg(this->cursorPosition);
char* buffer = new char[bytes];
@ -85,11 +109,14 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
}catch(std::exception& e){
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
}
this->cursorPosition += bytes;
return ErrorOr<std::vector<uint8_t>>(data);
}
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
if(this->size.isError){
return ErrorOr<tiny_utf8::string>(true, this->size.errorCode);
}
ErrorOr<std::vector<uint8_t>> data = this->read(bytes);
if(data.isError){
return ErrorOr<tiny_utf8::string>(true, data.errorCode);

View File

@ -13,7 +13,6 @@
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#include <vector>
#include "assert.hpp"
#include "tinyutf8/tinyutf8.h"
#include "../lib/file.hpp"
@ -24,19 +23,10 @@ int main(){
std::cout << "################################################################################" << std::endl;
File* file;
//readByte test
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
uint8_t byte = file->readByte().value;
ASSERT(byte == 'T');
std::cout << "Passed read byte test." << std::endl;
//read test
std::vector<uint8_t> bytes = file->read(5).value;
//cursorPosition has already moved forward by one
ASSERT(bytes == std::vector<uint8_t>({'e', 's', 't', ' ', 's'}));
std::cout << "Passed read test." << std::endl;
//readString test
tiny_utf8::string data = file->readString(5).value;
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
tiny_utf8::string data = file->readString(file->size.value).value;
std::cout << file->size.isError << std::endl;
std::cout << data << std::endl;
}