Files
spark/src/lib/utils/file_utils.cpp
T

48 lines
1.2 KiB
C++

#include "file_utils.h"
#include "bts_assert.h"
#include "lib/os/os.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
namespace bts {
namespace utils {
bool create_if_not_there(std::string const &path) {
if (!boost::filesystem::exists(path)) {
bts_assert_msg(
boost::filesystem::create_directories(path),
"Unable to create directory");
return false;
} else
return true;
}
std::string normalize_data_path(std::string const &input_path) {
// TODO: Remove this method (just need to be 100% sure this substitution
// isn't used in practice)
bts_assert(input_path.find("$data_path") == std::string::npos);
return input_path;
}
std::string read_file(const std::string &path) {
std::string out;
read_file(path, out);
return out;
}
void read_file(const std::string &path, std::string &out) {
std::ifstream ifstr{path};
// Preallocate memory
ifstr.seekg(0, std::ios::end);
out.reserve(ifstr.tellg());
ifstr.seekg(0, std::ios::beg);
out.assign(
(std::istreambuf_iterator<char>(ifstr)),
std::istreambuf_iterator<char>());
}
} // namespace utils
} // namespace bts