Compare commits
No commits in common. "5920d1d0046369791d63fa69b32a6fa50cbdd5a0" and "8b1491c311009b5f6a0cd9061b8e055b22163d27" have entirely different histories.
5920d1d004
...
8b1491c311
|
@ -27,7 +27,22 @@
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -58,20 +73,29 @@ 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];
|
||||||
|
@ -85,11 +109,14 @@ 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,7 +13,6 @@
|
||||||
// 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"
|
||||||
|
@ -24,19 +23,10 @@ int main(){
|
||||||
std::cout << "################################################################################" << std::endl;
|
std::cout << "################################################################################" << std::endl;
|
||||||
File* file;
|
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
|
//read test
|
||||||
std::vector<uint8_t> bytes = file->read(5).value;
|
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
|
||||||
//cursorPosition has already moved forward by one
|
tiny_utf8::string data = file->readString(file->size.value).value;
|
||||||
ASSERT(bytes == std::vector<uint8_t>({'e', 's', 't', ' ', 's'}));
|
std::cout << file->size.isError << std::endl;
|
||||||
std::cout << "Passed read test." << std::endl;
|
|
||||||
|
|
||||||
//readString test
|
|
||||||
tiny_utf8::string data = file->readString(5).value;
|
|
||||||
std::cout << data << std::endl;
|
std::cout << data << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue