1 #include <boost/filesystem/path.hpp>
8 using namespace std::literals::string_literals;
19 const std::optional<boost::filesystem::path> &ruleFilePath,
20 std::vector<std::string> targetStyles,
22 ) : targetStyles(std::move(targetStyles)),
34 std::optional<std::stringstream> ruleFileContents,
35 std::vector<std::string> targetStyles,
37 ) : targetStyles(std::move(targetStyles)),
50 const boost::filesystem::path &inputPath,
51 const std::optional<std::string> &sorting
52 )
const -> std::vector<BibElement> {
53 if (!boost::filesystem::exists(inputPath)) {
54 throw std::invalid_argument(
"No such file or directory. [path=" + inputPath.string() +
"]");
55 }
else if (boost::filesystem::is_directory(inputPath)) {
56 std::vector<BibElement> collector{};
57 for (
const auto &file : boost::filesystem::directory_iterator(inputPath)) {
58 std::ifstream inFile{file.path().string()};
59 inFile >> std::noskipws;
60 std::string inContent{std::istream_iterator<char>{inFile}, std::istream_iterator<char>{}};
61 for (
const auto &element : generate(std::string_view(inContent), sorting, inputPath.string())) {
62 collector.push_back(element);
65 if (sorting) sortElements(collector, sorting.value());
67 }
else if (boost::filesystem::is_regular_file(inputPath)) {
68 std::ifstream inFile{inputPath.string()};
69 inFile >> std::noskipws;
70 std::string inContent{std::istream_iterator<char>{inFile}, std::istream_iterator<char>{}};
71 auto elements = generate(std::string_view(inContent), sorting, inputPath.string());
72 if (sorting) sortElements(elements, sorting.value());
75 throw std::invalid_argument(
"Unexpected file-descriptor. [path=" + inputPath.string() +
"]");
88 std::string_view inputFileContent,
89 const std::optional<std::string> &sorting,
90 const std::string &filename
91 )
const -> std::vector<BibElement> {
92 const std::vector<StyleProperties> targetStructures = translationTable.stylePropertiesOf(targetStyles);
93 std::vector<BibElement> parsedElements =
Parser::elementsOf(inputFileContent, filename);
95 if (sorting) sortElements(parsedElements, sorting.value());
96 return parsedElements;
97 }
else if (targetStructures.empty()) {
100 auto filteredElements = filterElements(parsedElements, targetStructures);
101 if (sorting) sortElements(filteredElements, sorting.value());
102 return filteredElements;
114 std::string_view input,
115 const std::string &filename
116 ) -> std::vector<BibElement> {
118 std::vector<BibElement> result;
120 delete std::accumulate<std::string_view::iterator, ParserState *>(
136 throw std::runtime_error(e.
what());
147 std::begin(elements),
150 return l.
findAttribute(sorting).value_or<Field>({
""s,
""s}).value
165 const std::vector<boost::filesystem::path> &inputPaths,
166 const std::optional<std::string> &sorting
167 )
const -> std::vector<BibElement> {
168 std::vector<BibElement> parsedElements;
170 std::cbegin(inputPaths),
171 std::cend(inputPaths),
172 [
this, &parsedElements, &sorting](
const boost::filesystem::path &p) {
173 const auto elements = generate(p, sorting);
174 parsedElements.insert(std::end(parsedElements), std::cbegin(elements), std::cend(elements));
177 if (sorting) sortElements(parsedElements, sorting.value());
178 return parsedElements;
188 const std::vector<BibElement> &elements,
189 const std::vector<StyleProperties> &props
190 ) noexcept -> std::vector<BibElement> {
191 std::vector<BibElement> filteredElements{};
193 std::cbegin(elements),
195 std::back_inserter(filteredElements),
203 ) != std::cend(props);
206 return filteredElements;