SeqAn3
format_base.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
41  #pragma once
42 
43 #include <iostream>
44 #include <limits>
45 #include <sstream>
46 #include <string>
47 
48 #include <meta/meta.hpp>
49 
53 
54 namespace seqan3::detail
55 {
56 
61 class format_base
62 {
63 protected:
68  template <typename value_type>
69  static std::string get_type_name_as_string(value_type const & )
70  {
71  using type = std::decay_t<value_type>;
72  using types = meta::list<int8_t,
73  uint8_t,
74  int16_t,
75  uint16_t,
76  int32_t,
77  uint32_t,
78  int64_t,
79  uint64_t,
80  double,
81  float,
82  bool,
83  char,
84  std::string>;
85  std::vector<std::string> names{"INT (8 bit)",
86  "UNSIGNED (8 bit)",
87  "INT (16 bit)",
88  "UNSIGNED (16 bit)",
89  "INT (32 bit)",
90  "UNSIGNED (32 bit)",
91  "INT (64 bit)",
92  "UNSIGNED (64 bit)",
93  "DOUBLE",
94  "FLOAT",
95  "BOOL",
96  "CHAR",
97  "STRING"};
98 
99  if constexpr (meta::in<types, type>::value)
100  return names[meta::find_index<types, type>::value];
101  else
102  return "UNKNOWN_TYPE";
103  }
104 
109  template <sequence_container_concept container_type>
111  requires !std::is_same_v<container_type, std::string>
113  static std::string get_type_name_as_string(container_type const & )
114  {
115  typename container_type::value_type tmp;
116  return get_type_name_as_string(tmp);
117  }
118 
124  template <typename option_value_type>
125  static std::string option_type_and_list_info(option_value_type const & value)
126  {
127  return ("\\fI" + get_type_name_as_string(value) + "\\fP");
128  }
129 
136  template <typename container_type>
138  requires sequence_container_concept<container_type> && !std::is_same_v<container_type, std::string>
140  static std::string option_type_and_list_info(container_type const & container)
141  {
142  return ("List of \\fI" + get_type_name_as_string(container) + "\\fP's");
143  }
144 
152  static std::string prep_id_for_help(char const short_id, std::string const & long_id)
153  {
154  // Build list item term.
155  std::string term;
156  if (short_id != '\0')
157  term = "\\fB-" + std::string(1, short_id) + "\\fP";
158 
159  if (short_id != '\0' && !long_id.empty())
160  term.append(", ");
161 
162  if (!long_id.empty())
163  term.append("\\fB--" + long_id + "\\fP");
164 
165  return term;
166  }
167 
174  std::string escape_special_xml_chars(std::string const & original)
175  {
176  std::string escaped;
177  escaped.resize(original.size()); // will be at least as long
178 
179  for (auto c : original)
180  {
181  if (c == '"')
182  escaped.append("&quot;");
183  else if (c == '\'')
184  escaped.append("&apos;");
185  else if (c == '&')
186  escaped.append("&amp;");
187  else if (c == '<')
188  escaped.append("&lt;");
189  else if (c == '>')
190  escaped.append("&gt;");
191  else
192  escaped.push_back(c);
193  }
194 
195  return escaped;
196  }
197 
204  static std::string expand_multiple_flags(std::string const & flag_cluster)
205  {
206  std::string tmp;
207  auto it{flag_cluster.begin()};
208 
209  if (flag_cluster[0] == '-')
210  ++it;
211 
212  for (; it != flag_cluster.end() - 1; ++it)
213  tmp.append("-" + std::string(1, *it) + ", ");
214 
215  tmp.erase(tmp.find_last_of(',')); // remove last ', '
216  tmp.append(" and -" + std::string(1, flag_cluster[flag_cluster.size() - 1]));
217 
218  return tmp;
219  }
220 };
221 
222 } // namespace seqan3::detail
Contains some standard validators for (positional) options.
Contains parser related exceptions.
Definition: aligned_sequence_concept.hpp:288
Contains auxiliary information.