Commit 41367f0b authored by Sebastian Wolf's avatar Sebastian Wolf

Minor additions/fixed for misc

parent 3c95be50
......@@ -63,7 +63,6 @@ namespace xerus { namespace misc {
return count;
}
///@brief: Checks whether an arbitary container contains a certain element.
template<template<class, class...> class container_t, class item_t, class... rest_t,
typename std::enable_if<sfinae::has_find<container_t<item_t, rest_t...>, item_t>::value, int>::type = 0>
......@@ -195,7 +194,16 @@ namespace xerus { namespace misc {
inline void erase(container_t<item_t, rest_t...>& _container, const rule_t& _rule) {
_container.erase(std::remove_if(_container.begin(), _container.end(), _rule), _container.end());
}
} } // namespaces xerus::misc
///@brief: Erases a single element specified by @a _rule from the vector @a _vector by swaping with the last item (destroying the order).
template<class rule_t, class item_t, class... rest_t>
inline void swap_erase_single(std::vector<item_t, rest_t...>& _vector, const rule_t& _rule) {
const auto foundItr = std::find_if(_vector.begin(), _vector.end(), _rule);
if(foundItr != _vector.end()) {
if(foundItr+1 != _vector.end()) { std::iter_swap(foundItr, _vector.rbegin()); }
_vector.pop_back();
}
}
}} // namespaces xerus::misc
......@@ -54,8 +54,7 @@ namespace xerus { namespace misc {
for (boost::filesystem::directory_iterator itr(_path), end; itr != end; ++itr) {
if(!boost::filesystem::is_regular_file(itr->path())) { continue;}
const auto file = itr->path().filename().string();
files.emplace(file);
files.emplace(itr->path().filename().string());
}
return files;
......@@ -68,8 +67,7 @@ namespace xerus { namespace misc {
for (boost::filesystem::directory_iterator itr(_path), end; itr != end; ++itr) {
if(!boost::filesystem::is_directory(itr->path())) { continue;}
const auto file = itr->path().filename().string();
files.emplace(file);
files.emplace(itr->path().filename().string());
}
return files;
......@@ -95,7 +93,7 @@ namespace xerus { namespace misc {
std::string read_file(const std::string& _path) {
REQUIRE(file_exists(_path), "File " << _path << " does not exist.");
std::ifstream fileStream(_path, std::ifstream::in);
std::ifstream fileStream(_path, std::ifstream::in | std::ifstream::binary);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (read) open the file " << _path);
std::string contents;
......@@ -112,13 +110,12 @@ namespace xerus { namespace misc {
std::ifstream fileStream(_path, std::ifstream::in);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (read) open the file " << _path);
std::vector<std::string> lines(1);
std::string line;
std::vector<std::string> lines;
while(getline(fileStream, lines.back())) {
lines.emplace_back();
while(getline(fileStream, line)) {
lines.emplace_back(std::move(line));
}
lines.pop_back();
return lines;
}
......@@ -148,7 +145,7 @@ namespace xerus { namespace misc {
#if __cplusplus >= 201402L
std::ifstream open_file_read(const std::string& _path) {
REQUIRE(boost::filesystem::exists(_path), "File " << _path << " does not exist.");
REQUIRE(file_exists(_path), "File " << _path << " does not exist.");
std::ifstream fileStream(_path, std::ifstream::in);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (read) open the file " << _path);
return fileStream;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment