Initial commit
This commit is contained in:
73
lib/utils.cpp
Normal file
73
lib/utils.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/** @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
|
||||
Reference in New Issue
Block a user