Commit 4b2d5559 authored by Sebastian Wolf's avatar Sebastian Wolf

Removed obsolete TODOs and added Documentation to fileUtilities

parent 812a5a96
Pipeline #1104 passed with stages
in 16 minutes and 15 seconds
......@@ -29,25 +29,33 @@
#include <fstream>
namespace xerus { namespace misc {
void create_folder_for_file(const std::string& _path);
std::set<std::string> get_files(const std::string& _path);
///@brief Creates all directories in the @a _path of the file, if needed.
void create_directories_for_file(const std::string& _path);
///@brief Returns a set containing all regular files at the given @a _path.
std::set<std::string> get_files(const std::string& _path);
///@brief Checks whether a file at the given @a _path exists.
bool file_exists(const std::string& _path);
bool file_is_empty(const std::string& _filename);
std::string read_file(const std::string& _path);
///@brief Checks whether a file at the given @a _path is empty.
bool file_is_empty(const std::string& _path);
///@brief Reads the complete content of the file at the given @a _path into a string.
std::string read_file(const std::string& _path);
#if __cplusplus >= 201402L
///@brief Opens a reading filestream for the given @a _path.
std::ifstream open_file_read(const std::string& _path);
///@brief Opens a writing filestream for the given @a _path, deleting any preexisting content.
std::ofstream open_file_truncate(const std::string& _path);
///@brief Opens a writing filestream for the given @a _path, appending to any preexisting content.
std::ofstream open_file_append(const std::string& _path);
#endif
#endif
} }
......@@ -48,7 +48,7 @@ namespace xerus {
error += misc::sqr(_reference[i] - _test[i]);
norm += misc::sqr(_reference[i]);
}
} else { // TODO how to use the weights?
} else {
for(size_t i = 0; i < cSize; ++i) {
error += _weights[i]*misc::sqr(_reference[i] - _test[i]);
norm += _weights[i]*misc::sqr(_reference[i]);
......@@ -87,7 +87,7 @@ namespace xerus {
}
} else {
for(size_t i = 0; i < cSize; ++i) {
norm += weights[i]*misc::sqr(measuredValues[i]); // TODO How to use weights
norm += weights[i]*misc::sqr(measuredValues[i]);
}
}
return std::sqrt(norm);
......
......@@ -29,79 +29,79 @@
#include <boost/filesystem.hpp>
namespace xerus { namespace misc {
void create_folder_for_file(const std::string& _path) {
if(_path.find('/') != std::string::npos) {
const std::string folder = _path.substr(0, _path.find_last_of('/'));
boost::filesystem::create_directories(folder);
}
}
std::set<std::string> get_files(const std::string& _path) {
REQUIRE(boost::filesystem::exists(_path) && boost::filesystem::is_directory(_path), "Invalid path " << _path << " (not an existing directory).");
std::set<std::string> files;
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);
}
return files;
}
bool file_exists(const std::string& _path) {
void create_directories_for_file(const std::string& _path) {
if(_path.find('/') != std::string::npos) {
const std::string folder = _path.substr(0, _path.find_last_of('/'));
boost::filesystem::create_directories(folder);
}
}
std::set<std::string> get_files(const std::string& _path) {
REQUIRE(boost::filesystem::exists(_path) && boost::filesystem::is_directory(_path), "Invalid path " << _path << " (not an existing directory).");
std::set<std::string> files;
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);
}
return files;
}
bool file_exists(const std::string& _path) {
return boost::filesystem::exists(_path);
}
bool file_is_empty(const std::string& _filename) {
std::ifstream pFile(_filename);
REQUIRE(pFile.is_open(), "IE: Failed to open file");
return pFile.peek() == std::ifstream::traits_type::eof();
}
bool file_is_empty(const std::string& _path) {
std::ifstream pFile(_path);
REQUIRE(pFile.is_open(), "IE: Failed to open file");
return pFile.peek() == std::ifstream::traits_type::eof();
}
std::string read_file(const std::string& _path) {
REQUIRE(boost::filesystem::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);
std::string contents;
fileStream.seekg(0, std::ios::end);
contents.resize(size_t(fileStream.tellg()));
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], std::streamsize(contents.size()));
return contents;
}
#if __cplusplus >= 201402L
std::ifstream open_file_read(const std::string& _path) {
REQUIRE(boost::filesystem::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;
}
std::ofstream open_file_truncate(const std::string& _path) {
create_folder_for_file(_path);
std::ofstream fileStream(_path, std::ofstream::out | std::ofstream::trunc);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (write) open the file " << _path);
return fileStream;
}
std::ofstream open_file_append(const std::string& _path) {
create_folder_for_file(_path);
std::ofstream fileStream(_path, std::ofstream::out | std::ofstream::app);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (write) open the file " << _path);
return fileStream;
}
#endif
std::string read_file(const std::string& _path) {
REQUIRE(boost::filesystem::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);
std::string contents;
fileStream.seekg(0, std::ios::end);
contents.resize(size_t(fileStream.tellg()));
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], std::streamsize(contents.size()));
return contents;
}
#if __cplusplus >= 201402L
std::ifstream open_file_read(const std::string& _path) {
REQUIRE(boost::filesystem::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;
}
std::ofstream open_file_truncate(const std::string& _path) {
create_directories_for_file(_path);
std::ofstream fileStream(_path, std::ofstream::out | std::ofstream::trunc);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (write) open the file " << _path);
return fileStream;
}
std::ofstream open_file_append(const std::string& _path) {
create_directories_for_file(_path);
std::ofstream fileStream(_path, std::ofstream::out | std::ofstream::app);
CHECK(fileStream.is_open() && !fileStream.fail(), error, "Could not properly (write) open the file " << _path);
return fileStream;
}
#endif
} }
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