osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
generic.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
12 //====================================================
13 // Preprocessor settings
14 //====================================================
15 #pragma once
16 #ifndef OSMANIP_UTILITY_GENERIC_HPP
17 #define OSMANIP_UTILITY_GENERIC_HPP
18 
19 //====================================================
20 // Headers
21 //====================================================
22 
23 // STD
24 #include <cmath>
25 #include <cstdlib>
26 #include <sstream>
27 #include <stdexcept>
28 #include <string>
29 #include <string_view>
30 #include <type_traits>
31 #include <vector>
32 
33 namespace osm {
34 
35  //====================================================
36  // Constants
37  //====================================================
38  template <typename T>
39  inline const T null_str = "";
40 
41  template <typename T>
42  inline const T empty_space = " ";
43 
44  //====================================================
45  // Enum classes
46  //====================================================
47  enum class ANSI_SEARCH { first, generic };
48 
49  //====================================================
50  // Functions
51  //====================================================
52  extern std::vector<std::string> split_string(const std::string& input, const std::string& regex);
53  extern std::string getCommandOut(const char* command);
54 
55  //====================================================
56  // except_error_func
57  //====================================================
67  template <typename T_err = std::runtime_error>
68  inline T_err except_error_func(const std::string& beg = "", std::string var = nullptr,
69  const std::string& end = "") {
70  using namespace std::literals::string_literals;
71 
72  static std::ostringstream oss;
73  oss.str("");
74  oss.clear();
75  oss << "\033[31m" << beg << " \""s
76  << "\033[1m" << var << "\033[22m"
77  << "\" "s << end << "\033[39m";
78 
79  return T_err(oss.str());
80  }
81 
82  //====================================================
83  // is_escape
84  //====================================================
96  template <typename T>
97  bool is_escape(const T& str, const ANSI_SEARCH& flag) {
98  if constexpr (std::is_convertible_v<T, std::string_view> && !std::is_same_v<T, std::nullptr_t>) {
99  switch (flag) {
100  case (ANSI_SEARCH::first): {
101  return (std::string_view(str).length() < 7) && (str[0] == '\033');
102  }
103  case (ANSI_SEARCH::generic): {
104  return (std::string_view(str).find('\033') != std::string_view::npos);
105  }
106  }
107  }
108  return false;
109  }
110 
111  //====================================================
112  // roundoff
113  //====================================================
122  template <typename T>
123  inline T roundoff(T value, unsigned char prec) {
124  T pow_10 = std::pow(10.0f, static_cast<T>(prec));
125 
126  return round(value * pow_10) / pow_10;
127  }
128 
129  //====================================================
130  // isFloatingPoint
131  //====================================================
139  template <typename T>
140  inline bool isFloatingPoint(const T&) {
141  return std::is_floating_point<T>::value;
142  }
143 
144  //====================================================
145  // one
146  //====================================================
155  template <typename T>
156  inline T one(const T& iterating_var) {
157  std::vector<T> counter_;
158 
159  if (isFloatingPoint(iterating_var)) {
160  if (counter_.size() < 2) counter_.push_back(iterating_var);
161  if (counter_.size() == 2) return abs(abs(counter_.front()) - abs(counter_.back()));
162  return static_cast<T>(0);
163  }
164 
165  counter_.clear();
166  return 1;
167  }
168 } // namespace osm
169 
170 //====================================================
171 // std::string * int
172 //====================================================
180 inline std::string operator*(const std::string& generic_string, unsigned int integer) {
181  std::string output;
182  output.reserve(integer * generic_string.size());
183 
184  while (integer--) {
185  output += generic_string;
186  }
187 
188  return output;
189 }
190 
191 #endif
std::string operator*(const std::string &generic_string, unsigned int integer)
Function to multiply a string by an integer.
Definition: generic.hpp:180
Definition: canvas.cpp:30
const T empty_space
Definition: generic.hpp:42
bool isFloatingPoint(const T &)
Function to check if an expression (not a type) is a floating point or not. I know this function is a...
Definition: generic.hpp:140
bool is_escape(const T &str, const ANSI_SEARCH &flag)
This method is used to check if an input variable is an ANSI escape sequency or not.
Definition: generic.hpp:97
T_err except_error_func(const std::string &beg="", std::string var=nullptr, const std::string &end="")
Function used to throw customized stdexception error. This function is extremely specific to my purpo...
Definition: generic.hpp:68
T one(const T &iterating_var)
Function to find the incremented unit of a loop. Not easy to understand its purpose without context,...
Definition: generic.hpp:156
std::string getCommandOut(const char *command)
Function used to get the output of a shell command.
Definition: generic.cpp:54
const T null_str
Definition: generic.hpp:39
ANSI_SEARCH
Definition: generic.hpp:47
std::vector< std::string > split_string(const std::string &input, const std::string &regex)
Function used to split a string based on a certain reges.
Definition: generic.cpp:39
T roundoff(T value, unsigned char prec)
Function to round a floating point to n-th decimal place after comma.
Definition: generic.hpp:123