SeqAn3
The Modern C++ library for sequence analysis.
misc.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <variant>
16 
19 #include <seqan3/io/exception.hpp>
21 #include <seqan3/std/algorithm>
22 #include <seqan3/std/filesystem>
23 #include <seqan3/std/iterator>
24 
25 namespace seqan3::detail
26 {
27 
30 template <typename list_t, template <typename...> typename output_t>
31 struct variant_from_tags;
32 
35 template <template <typename...> typename output_t, typename ...ts>
36 struct variant_from_tags<meta::list<ts...>, output_t>
37 {
39  using type = std::variant<output_t<ts>...>;
40 };
41 
48 template <std::OutputIterator<char> it_t>
49 constexpr void write_eol(it_t & it, bool const add_cr)
50 {
51  if (add_cr)
52  it = '\r';
53 
54  it = '\n';
55 }
56 
67 template <typename format_variant_type>
68 void set_format(format_variant_type & format,
69  std::filesystem::path const & file_name)
70 {
71  using valid_formats = detail::transfer_template_args_onto_t<format_variant_type, type_list>;
72 
73  bool format_found = false;
74  std::string extension = file_name.extension().string();
75  if (extension.size() > 1)
76  {
77  extension = extension.substr(1); // drop leading "."
78  meta::for_each(valid_formats{}, [&] (auto && fmt)
79  {
80  using fmt_type = remove_cvref_t<decltype(fmt)>;
81  using fmt_tag = typename fmt_type::format_tag;
82 
83  for (auto const & ext : fmt_tag::file_extensions)
84  {
85  if (std::ranges::equal(ext, extension))
86  {
87  format = fmt_type{};
88  format_found = true;
89  return;
90  }
91  }
92  });
93  }
94 
95  if (!format_found)
96  throw unhandled_extension_error("No valid format found for this extension.");
97 }
98 
99 } // namespace seqan3::detail
::ranges::equal equal
Alias for ranges::equal. Determines if two sets of elements are the same.
Definition: algorithm:54
Provides seqan3::SequenceFileInputFormat and auxiliary classes.
T extension(T... args)
Provides exceptions used in the I/O module.
Provides C++20 additions to the <iterator> header.
Provides seqan3::type_list and auxiliary type traits.
Provides seqan3::type_list and auxiliary type traits.
Definition: aligned_sequence_concept.hpp:35
T size(T... args)
Adaptations of algorithms from the Ranges TS.
T substr(T... args)
This header includes C++17 filesystem support and imports it into namespace seqan3::filesystem (indep...