Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

substrate: Add basic file utility to read all the lines from a file #87

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions substrate/fd
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
#ifndef SUBSTRATE_FD
#define SUBSTRATE_FD

#include <array>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703L
#include <string_view>
Expand Down
69 changes: 69 additions & 0 deletions substrate/file_utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 {};

Check warning on line 21 in substrate/file_utils

View check run for this annotation

Codecov / codecov/patch

substrate/file_utils#L21

Added line #L21 was not covered by tests

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

Check warning on line 25 in substrate/file_utils

View check run for this annotation

Codecov / codecov/patch

substrate/file_utils#L25

Added line #L25 was not covered by tests

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 (boundary != cend) {
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
3 changes: 3 additions & 0 deletions substrate/internal/fd_compat
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
# include <share.h>
# undef fstat // macro conflicts with fstat definition below
# endif
#else
# define O_TEXT 0
# define O_BINARY 0
#endif


Expand Down
2 changes: 2 additions & 0 deletions substrate/mmap
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ namespace substrate
#ifdef _WIN32
if (_mapping)
CloseHandle(_mapping);
if (_fd != -1)
_close(_fd);
#else
if (_fd != -1)
::close(_fd);
Expand Down
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
Loading