Project-5/lib/utils.cpp
2023-12-06 11:49:25 +01:00

74 lines
1.5 KiB
C++

/** @file utils.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Implementation of the utils
*
* @bug No known bugs
* */
#include "utils.hpp"
namespace utils {
std::string scientific_format(double d, int width, int prec)
{
std::stringstream ss;
ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
return ss.str();
}
std::string scientific_format(const std::vector<double> &v, int width, int prec)
{
std::stringstream ss;
for (double elem : v) {
ss << scientific_format(elem, width, prec);
}
return ss.str();
}
bool mkpath(std::string path, int mode)
{
std::string cur_dir;
std::string::size_type pos = -1;
struct stat buf;
if (path.back() != '/') {
path += '/';
}
while (true) {
pos++;
pos = path.find('/', pos);
if (pos != std::string::npos) {
cur_dir = path.substr(0, pos);
if (mkdir(cur_dir.c_str(), mode) != 0
&& stat(cur_dir.c_str(), &buf) != 0) {
return -1;
}
}
else {
break;
}
}
return 0;
}
std::string dirname(const std::string &path)
{
return path.substr(0, path.find_last_of("/"));
}
std::string concatpath(const std::string &left, const std::string &right)
{
if (left.back() != '/' and right.front() != '/') {
return left + '/' + right;
}
else {
return left + right;
}
}
} // namespace utils