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 18, 2024
1 parent 11a178b commit 9198eb5
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
66 changes: 66 additions & 0 deletions substrate/file_utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: BSD-3-Clause

#ifndef SUBSTRATE_FILE_UTILS
#define SUBSTRATE_FILE_UTILS

#include <algorithm>
#include <iterator>
#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
{
using char_t = typename Str::value_type;

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

const auto map{file.map(PROT_READ)};
if (!map.valid())
return {};

std::vector<Str> result;

const auto *const cbegin{map.address<char_t>()};

const auto *const cend
{
[&]()
{
const auto *begin{cbegin};
std::advance(begin, map.length() / sizeof(char_t));
return begin;
}()
};

for (const auto *begin{cbegin}; begin < cend;)
{
const auto *boundary
{
std::find_if
(
begin,
cend,
[&](const auto chr) { return chr == separator || (separator == '\n' && chr == '\r'); }
)
};

result.emplace_back(begin, boundary);
if (separator == '\n' && *boundary == '\r' && (boundary + 1) != cend && *(boundary + 1) == '\n')
std::advance(boundary, 2);
else
std::advance(boundary, 1);

begin = boundary;
}

return result;
}
} // namespace substrate

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

using substrate::fd_t;
using substrate::indexedIterator_t;
using substrate::read_lines;

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

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

{
fd_t text{"info.txt", O_RDONLY | O_TEXT};
REQUIRE(text.valid());
// Memory mapping here will cause the conversion to be skipped
const auto input {read_lines(text)};
REQUIRE(lines.size() == input.size());
for (const auto &line : indexedIterator_t<decltype(lines)>{lines})
{
const auto &contents {line.second};
const auto &input_line {input[line.first]};
REQUIRE(input_line.find_first_of('\n', input_line.size() - 1) == std::string::npos);
REQUIRE(input_line.find_first_of('\r', input_line.size() - 1) == std::string::npos);
REQUIRE(input_line == contents);
}
}

{
const auto result{unlink("info.txt")};
if (result != 0)
REQUIRE(errno == 0);
else
SUCCEED();
}
}

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

{
fd_t text{"info.txt", O_WRONLY | O_CREAT | O_EXCL | O_BINARY, substrate::normalMode};
REQUIRE(text.valid());
for (const auto &line : lines)
{
REQUIRE(text.write(line.data(), line.size() * sizeof(std::u16string::value_type)));
REQUIRE(text.write(u'\n'));
}
}

{
fd_t text{"info.txt", O_RDONLY | O_BINARY};
REQUIRE(text.valid());
const auto input{substrate::read_lines<std::u16string>(text)};
REQUIRE(lines.size() == input.size());
for (const auto &line : indexedIterator_t<decltype(lines)>{lines})
{
const auto &contents {line.second};
const auto &input_line {input[line.first]};
REQUIRE(input_line.find_first_of(u'\n', input_line.size() - 1) == std::u16string::npos);
REQUIRE(input_line.find_first_of(u'\r', input_line.size() - 1) == std::u16string::npos);
REQUIRE(input_line == contents);
}
}

{
const auto result{unlink("info.txt")};
if (result != 0)
REQUIRE(errno == 0);
else
SUCCEED();
}
}

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 9198eb5

Please sign in to comment.