lib/file.cpp: fix read fubctions.

Soda
Shwoomple 2022-10-02 08:38:39 +05:30
parent 89baeebc65
commit 056c1e6b11
2 changed files with 18 additions and 35 deletions

View File

@ -27,22 +27,7 @@
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));
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->open();
this->isOpen = true;
this->eof = false;
}
@ -73,29 +58,20 @@ 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];
@ -109,14 +85,11 @@ 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,6 +13,7 @@
// 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"
@ -23,10 +24,19 @@ int main(){
std::cout << "################################################################################" << std::endl;
File* file;
//read test
//readByte test
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;
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;
std::cout << data << std::endl;
}