DiFfRG
Loading...
Searching...
No Matches
utils.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <iomanip>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include <vector>
9
10// external libraries
11#include <spdlog/sinks/basic_file_sink.h>
12#include <spdlog/spdlog.h>
13
14// DiFfRG
16#include <DiFfRG/common/json.hh>
17#include <DiFfRG/common/math.hh>
19
20namespace DiFfRG
21{
22 using uint = unsigned int;
23
27 template <auto Start, auto End, auto Inc, class F> constexpr void constexpr_for(F &&f)
28 {
29 if constexpr (Start < End) {
30 f(std::integral_constant<decltype(Start), Start>());
32 }
33 }
34
35 std::shared_ptr<spdlog::logger> build_logger(const std::string &name, const std::string &filename);
36
43 std::string strip_name(const std::string &name);
44
51 std::vector<double> string_to_double_array(const std::string &str);
52
56 template <typename T> std::string getWithPrecision(uint precision, T number)
57 {
58 std::stringstream stream;
59 stream << std::fixed << std::setprecision(precision) << number;
60 return stream.str();
61 }
62
68 bool file_exists(const std::string &name);
69
73 template <typename T> std::string to_string_with_digits(const T number, const int digits)
74 {
75 if (digits <= 0) throw std::runtime_error("to_string_with_digits: digits must be > 0");
76 const int d = int(std::ceil(std::log10(number < 0 ? -number : number)));
77 std::stringstream stream;
78 stream << std::fixed << std::setprecision(std::max(digits - d, 0)) << number;
79 return stream.str();
80 }
81
85 std::string make_folder(const std::string &path);
86
90 bool create_folder(const std::string &path_);
91
95 std::string time_format(size_t time_in_seconds);
96
100 std::string time_format_ms(size_t time_in_miliseconds);
101
102 bool has_suffix(const std::string &str, const std::string &suffix);
103} // namespace DiFfRG
Definition complex_math.hh:14
std::string make_folder(const std::string &path)
Add a trailing '/' to a string, in order for it to be in standard form of a folder path.
bool create_folder(const std::string &path_)
Creates the directory path, even if its parent directories should not exist.
std::shared_ptr< spdlog::logger > build_logger(const std::string &name, const std::string &filename)
std::string time_format_ms(size_t time_in_miliseconds)
Nice output from seconds to h/min/s style string.
std::string getWithPrecision(uint precision, T number)
Return number with fixed precision after the decimal point.
Definition utils.hh:56
bool has_suffix(const std::string &str, const std::string &suffix)
bool file_exists(const std::string &name)
Checks if a file exists.
constexpr void constexpr_for(F &&f)
A compile-time for loop, which calls the lambda f of signature void(integer) for each index.
Definition utils.hh:27
std::string time_format(size_t time_in_seconds)
Nice output from seconds to h/min/s style string.
unsigned int uint
Definition utils.hh:22
std::string strip_name(const std::string &name)
Strips all special characters from a string, e.g. for use in filenames.
std::string to_string_with_digits(const T number, const int digits)
Return number with fixed significant digits.
Definition utils.hh:73
std::vector< double > string_to_double_array(const std::string &str)
Takes a string of comma-separated numbers and outputs it as a vector.