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 authored and dragonmux committed Jan 18, 2024
1 parent 9f0886d commit 168593d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
65 changes: 65 additions & 0 deletions substrate/file_utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: BSD-3-Clause

#ifndef SUBSTRATE_FILE_UTILS
#define SUBSTRATE_FILE_UTILS

#include <string>
#include <vector>
#include <algorithm>
#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 *const end
{
[&]()
{
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 != end;)
{
const auto *boundary
{
std::find_if
(
begin,
end,
[&](const auto chr) { return chr == separator || (separator == '\n' && chr == '\r'); }
)
};

result.emplace_back(begin, boundary);
if (separator == '\n' && *boundary == '\r' && (boundary + 1) != end && *(boundary + 1) == '\n')
std::advance(boundary, 2);
else
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
81 changes: 81 additions & 0 deletions test/file_utils.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 &line : lines)
REQUIRE(text.write(line));
}

{
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 &line : lines)
REQUIRE(text.write(line.data(), line.size() * sizeof(std::u16string::value_type)));
}

{
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 168593d

Please sign in to comment.