Skip to content

Commit

Permalink
Added read with user buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
SaeidYazdani committed Dec 23, 2023
1 parent f9e9638 commit 8833c59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
35 changes: 27 additions & 8 deletions examples/fileio/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,39 @@
#include "embedonix/simplelibs/utilities/benchmark.h"

void read_file_saya(std::string path) {
volatile auto content = embedonix::simplelibs::fileio::readers::read_file(
"../resources/random-files/one-mb-file"
);
volatile auto content = embedonix::simplelibs::fileio
::readers::read_file_bytes(path);
}
void read_file_mila(std::string path) {

void read_file_mila(std::string path, std::vector<std::byte>& buffer) {
volatile auto result = embedonix::simplelibs::fileio::readers::read_file_bytes_caller_alloc(
path, buffer);
if(!result) {
exit(1);
}
}

int main(int argc, char** argv) {

// Allocate a big enough buffer for files up to 10MB
auto buffer = std::vector<std::byte>(1100000);

for (size_t i = 0; i < 10; ++i) {
std::cout << "mila read: " <<
embedonix::simplelibs::utilities::benchmark::measure::
function_execution_time(read_file_mila,
"../resources/random-files/ten-mb-file",
buffer)
<< std::endl;
}

for (size_t i = 0; i < 10; ++i) {
std::cout << "saya read: " <<
embedonix::simplelibs::utilities::benchmark::measure::
function_execution_time(read_file_saya,
"../resources/random-files/ten-mb-file")
<< std::endl;
}



std::cout << "The size of content is:" << content.size() << std::endl;

return 0;
}
3 changes: 3 additions & 0 deletions libs/fileio/include/embedonix/simplelibs/fileio/filereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace embedonix::simplelibs::fileio::readers {
*/
auto read_file_bytes(std::string_view filepath) -> std::vector<std::byte>;

auto read_file_bytes_caller_alloc(std::string_view filepath,
std::vector<std::byte>& buffer) -> bool;

auto read_file(std::string_view path) -> std::string;

auto read_file_string(std::string_view filepath) -> std::string;
Expand Down
23 changes: 23 additions & 0 deletions libs/fileio/src/filereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ auto read_file_bytes(std::string_view filepath) -> std::vector<std::byte> {
return buffer;
}

auto read_file_bytes_caller_alloc(std::string_view filepath,
std::vector<std::byte>& buffer)-> bool {
std::ifstream ifs(filepath.data(), std::ios::binary | std::ios::ate);

if (!ifs)
throw std::ios_base::failure("File does not exist");

auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);

auto size = std::size_t(end - ifs.tellg());

if (size == 0) // avoid undefined behavior
return false;

if (!ifs.read((char *) buffer.data(), buffer.size()))
throw std::ios_base::failure("Read error");

return true;
}

auto read_file(std::string_view path) -> std::string {
constexpr auto read_size = std::size_t(4096);
auto stream = std::ifstream(path.data());
Expand All @@ -52,4 +73,6 @@ auto read_file_string(std::string_view filepath) -> std::string {
return std::string(reinterpret_cast<char *>(&bytes[0]), bytes.size());
}



} // End Namespace embedonix::simplelibs::fileio::readers

0 comments on commit 8833c59

Please sign in to comment.