Skip to content

Commit

Permalink
substrate: Add basic file utility to read all the lines from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark committed Jan 17, 2024
1 parent 9f0886d commit c2c87fe
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
53 changes: 53 additions & 0 deletions substrate/file_utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: BSD-3-Clause

#ifndef SUBSTRATE_FILE_UTILS
#define SUBSTRATE_FILE_UTILS

#include <string>
#include <vector>
#include <substrate/fd>
#include <substrate/mmap>

namespace substrate
{
template<typename Str = std::string>
std::vector<Str> read_lines(fd_t &file, typename Str::value_type separator = '\n') noexcept
{
if (!file.valid())
return {};

const auto map{file.map(PROT_READ)};

if (!map.valid())
return {};

std::vector<Str> result;

const auto *cend{[&]() {
const auto *begin{map.address<typename Str::value_type>()};
std::advance(begin, map.length() / sizeof(typename Str::value_type));
return begin;
}()};

for (const auto *begin{map.address<typename Str::value_type>()}; begin < cend;)
{
const auto *boundary{std::find(begin, cend, separator)};

result.emplace_back(begin, boundary);

std::advance(boundary, 1);

begin = boundary;
}

return result;
}

extern template SUBSTRATE_CLS_API std::vector<std::string> read_lines<>(fd_t &, std::string::value_type);

extern template SUBSTRATE_CLS_API std::vector<std::wstring> read_lines<>(fd_t &, std::wstring::value_type);

extern template SUBSTRATE_CLS_API std::vector<std::u16string> read_lines<>(fd_t &, std::u16string::value_type);
} // namespace substrate

#endif // SUBSTRATE_FILE_UTILS
79 changes: 79 additions & 0 deletions test/file_utils.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: BSD-3-Clause
#include <array>
#include <substrate/fd>
#include <substrate/file_utils>
#include <catch2/catch_test_macros.hpp>

using substrate::fd_t;

TEST_CASE("lines reader", "[boundedIterator_t]")
{
const std::array<std::string, 6> lines {{
"## Enforcement Responsibilities\n",
"\n",
"Community leaders are responsible for clarifying and enforcing our standards of\n",
"acceptable behavior and will take appropriate and fair corrective action in\n",
"response to any behavior that they deem inappropriate, threatening, offensive,\n",
"or harmful.\n"
}};

{
fd_t text{"info.txt", O_WRONLY | O_CREAT | O_EXCL, substrate::normalMode};
REQUIRE(text.valid());
for (const auto &i: lines) {
REQUIRE(text.write(i));
}
}

{
fd_t text{"info.txt", O_RDONLY};
REQUIRE(text.valid());
const auto input {substrate::read_lines(text)};
REQUIRE(lines.size() == input.size());
for (size_t i = 0; i < input.size(); i++) {
const auto &line {lines.at(i)};
REQUIRE(line.compare(0, line.size() - 1, input[i]) == 0);
}
}

REQUIRE(unlink("info.txt") == 0);
}

// TEST_CASE("lines reader with std::u16string", "[boundedIterator_t]")
// {
// const std::array<std::u16string, 6> lines {{
// u"## Enforcement Responsibilities\n",
// u"\n",
// u"Community leaders are responsible for clarifying and enforcing our standards of\n",
// u"acceptable behavior and will take appropriate and fair corrective action in\n",
// u"response to any behavior that they deem inappropriate, threatening, offensive,\n",
// u"or harmful.\n"
// }};

// {
// fd_t text{"info.txt", O_WRONLY | O_CREAT | O_EXCL, substrate::normalMode};
// REQUIRE(text.valid());
// for (const auto &i: lines) {
// REQUIRE(text.write(i.data(), i.size()));
// }
// }

// {
// fd_t text{"info.txt", O_RDONLY};
// REQUIRE(text.valid());
// const auto input {substrate::read_lines<std::u16string>(text)};
// REQUIRE(lines.size() == input.size());
// for (size_t i = 0; i < input.size(); i++) {
// const auto &line {lines.at(i)};
// REQUIRE(line.compare(0, line.size() - 1, input[i]) == 0);
// }
// }

// REQUIRE(unlink("info.txt") == 0);
// }

TEST_CASE()
{
unlink("info.txt");
SUCCEED();
}
2 changes: 1 addition & 1 deletion test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ testSrcs = [
'buffer_utils.cxx', 'pointer_utils.cxx',
'crypto/twofish.cxx', 'crypto/sha256.cxx', 'crypto/sha512.cxx',
'zip_container.cxx', 'affinity.cxx', 'threaded_queue.cxx', 'thread_pool.cxx',
'mmap.cxx'
'mmap.cxx', 'file_utils.cxx'
]

if target_machine.system() == 'linux'
Expand Down

0 comments on commit c2c87fe

Please sign in to comment.