powerd++  0.4.4
clas.hpp
Go to the documentation of this file.
1 
7 #ifndef _POWERDXX_CLAS_HPP_
8 #define _POWERDXX_CLAS_HPP_
9 
10 #include "types.hpp"
11 #include "errors.hpp"
12 #include "utility.hpp"
13 
14 #include <string> /* std::string */
15 #include <utility> /* std::pair() */
16 
20 namespace clas {
21 
22 using namespace std::literals::string_literals;
23 
42 types::cptime_t load(char const * const str);
43 
62 types::mhz_t freq(char const * const str);
63 
81 types::ms ival(char const * const str);
82 
93 size_t samples(char const * const str);
94 
111 types::decikelvin_t temperature(char const * const str);
112 
121 inline int celsius(types::decikelvin_t const val) {
122  auto const valc = val - 2731;
123  return (valc + (valc >= 0) * 5 - (valc < 0) * 5) / 10;
124 }
125 
140 template <typename T>
141 std::pair<T, T> range(T (& func)(char const * const), char const * const str) {
142  std::pair<T, T> result;
143  std::string first{str};
144 
145  if (first == "") {
146  /* give func() an opportunity to fail */
147  func(str);
148  errors::fail(errors::Exit::ERANGEFMT, 0, "range missing");
149  }
150 
151  auto const sep = first.find(':');
152  if (sep == std::string::npos) {
154  "missing colon separator in range: "s + str);
155  }
156  first.erase(sep);
157  result.first = func(first.c_str());
158  result.second = func(str + sep + 1);
159  return result;
160 }
161 
173 char const * sysctlname(char const * const str);
174 
193 template <typename ... CharTs>
194 char const * formatfields(char const * const fmt, CharTs const ... fields) {
195  using namespace utility::literals;
196  using utility::highlight;
197  using errors::fail;
198  using errors::Exit;
199 
200  auto it = fmt;
201  for (char const expect : {fields ..., '%'}) {
202  for (; it && *it; ++it) {
203  if (it[0] == '%' && it[1] == '%') {
204  ++it;
205  continue;
206  }
207  if (it[0] == '%' && it[1] == expect) {
208  ++it;
209  break;
210  }
211  if (it[0] == '%') {
212  auto const hl = highlight(fmt, it - fmt, 2);
213  fail(Exit::EFORMATFIELD, 0,
214  "unexpected formatting field: "s +
215  "\n\t" + hl.text + "\n\t" + hl.line);
216  }
217  }
218  }
219  return fmt;
220 }
221 
222 } /* namespace clas */
223 
224 #endif /* _POWERDXX_CLAS_HPP_ */
utility::literals
Contains literal operators.
Definition: utility.hpp:147
clas
A collection of functions to process command line arguments.
Definition: clas.hpp:20
types::mhz_t
unsigned int mhz_t
Type for CPU frequencies in MHz.
Definition: types.hpp:40
clas::freq
types::mhz_t freq(char const *const str)
Convert string to frequency in MHz.
Definition: clas.cpp:183
types.hpp
A collection of type aliases.
errors.hpp
Common error handling code.
utility::highlight
Underlined highlight(std::string const &str, ptrdiff_t const offs, ptrdiff_t const len=1)
Underline the given number of characters.
Definition: utility.cpp:12
types::decikelvin_t
int decikelvin_t
Type for temperatures in dK.
Definition: types.hpp:45
clas::formatfields
const char * formatfields(char const *const fmt, CharTs const ... fields)
Sanitise user-provided formatting strings.
Definition: clas.hpp:194
errors::Exit
Exit
Exit codes.
Definition: errors.hpp:22
clas::range
std::pair< T, T > range(T(&func)(char const *const), char const *const str)
Takes a string encoded range of values and returns them.
Definition: clas.hpp:141
types::cptime_t
unsigned long cptime_t
Type for load counting.
Definition: types.hpp:35
clas::load
types::cptime_t load(char const *const str)
Convert string to load in the range [0, 1024].
Definition: clas.cpp:153
errors::fail
void fail(Exit const exitcode, int const err, std::string const &msg)
Throws an Exception instance with the given message.
Definition: errors.hpp:94
errors::Exit::ERANGEFMT
@ ERANGEFMT
A user provided range is missing the separator.
clas::samples
size_t samples(char const *const str)
A string encoded number of samples.
Definition: clas.cpp:240
clas::sysctlname
const char * sysctlname(char const *const str)
Verify that the given string only contains characters allowed in sysctl names.
Definition: clas.cpp:291
types::ms
std::chrono::milliseconds ms
Millisecond type for polling intervals.
Definition: types.hpp:20
clas::celsius
int celsius(types::decikelvin_t const val)
Converts dK into °C for display purposes.
Definition: clas.hpp:121
clas::ival
types::ms ival(char const *const str)
Convert string to time interval in milliseconds.
Definition: clas.cpp:217
utility.hpp
Implements generally useful functions.
clas::temperature
types::decikelvin_t temperature(char const *const str)
Convert string to temperature in dK.
Definition: clas.cpp:262