lib/file.cpp: fix read fubctions.
parent
89baeebc65
commit
056c1e6b11
|
@ -27,22 +27,7 @@
|
||||||
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
|
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
|
||||||
std::filesystem::path filePath = path;
|
std::filesystem::path filePath = path;
|
||||||
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
|
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->isOpen = true;
|
||||||
this->eof = false;
|
this->eof = false;
|
||||||
}
|
}
|
||||||
|
@ -73,29 +58,20 @@ void File::close(){
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<uint8_t> File::readByte(){
|
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);
|
this->fileStream.seekg(this->cursorPosition);
|
||||||
|
|
||||||
char* byte = new char;
|
char* byte = new char;
|
||||||
try{
|
try{
|
||||||
this->fileStream.read(byte, 1);
|
this->fileStream.read(byte, 1);
|
||||||
delete byte;
|
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
|
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->cursorPosition += 1;
|
||||||
return ErrorOr<uint8_t>((uint8_t) *byte);
|
return ErrorOr<uint8_t>((uint8_t) *byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
|
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);
|
this->fileStream.seekg(this->cursorPosition);
|
||||||
|
|
||||||
char* buffer = new char[bytes];
|
char* buffer = new char[bytes];
|
||||||
|
@ -109,14 +85,11 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
|
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
|
||||||
}
|
}
|
||||||
|
this->cursorPosition += bytes;
|
||||||
return ErrorOr<std::vector<uint8_t>>(data);
|
return ErrorOr<std::vector<uint8_t>>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
|
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);
|
ErrorOr<std::vector<uint8_t>> data = this->read(bytes);
|
||||||
if(data.isError){
|
if(data.isError){
|
||||||
return ErrorOr<tiny_utf8::string>(true, data.errorCode);
|
return ErrorOr<tiny_utf8::string>(true, data.errorCode);
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
// version 3 along with this program.
|
// version 3 along with this program.
|
||||||
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
|
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include "assert.hpp"
|
#include "assert.hpp"
|
||||||
#include "tinyutf8/tinyutf8.h"
|
#include "tinyutf8/tinyutf8.h"
|
||||||
#include "../lib/file.hpp"
|
#include "../lib/file.hpp"
|
||||||
|
@ -23,10 +24,19 @@ int main(){
|
||||||
std::cout << "################################################################################" << std::endl;
|
std::cout << "################################################################################" << std::endl;
|
||||||
File* file;
|
File* file;
|
||||||
|
|
||||||
//read test
|
//readByte test
|
||||||
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
|
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
|
||||||
tiny_utf8::string data = file->readString(file->size.value).value;
|
uint8_t byte = file->readByte().value;
|
||||||
std::cout << file->size.isError << std::endl;
|
ASSERT(byte == 'T');
|
||||||
std::cout << data << std::endl;
|
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue