-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
substrate: Add basic file utility to read all the lines from a file
- Loading branch information
Showing
3 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters