Commit a70657b7 authored by Sebastian Wolf's avatar Sebastian Wolf

Minor improvements to file and string utilities

parent ff68be30
Pipeline #1259 passed with stages
in 17 minutes and 49 seconds
......@@ -27,6 +27,7 @@
#include <set>
#include <string>
#include <fstream>
#include <vector>
// #include "standard.h"
......@@ -44,12 +45,18 @@ namespace xerus { namespace misc {
///@brief Checks whether a file at the given @a _path exists.
bool file_exists(const std::string& _path);
///@brief Checks whether a directory at the given @a _path exists.
bool directory_exists(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);
///@brief Reads all lines of the file at the given @a _path into a vector of strings.
std::vector<std::string> read_file_lines(const std::string& _path);
/// @brief Resolves 'folder/..' occurences in pathnames.
std::string /*XERUS_warn_unused*/ normalize_pathname(const std::string& _name);
......
......@@ -77,6 +77,12 @@ namespace xerus { namespace misc {
bool file_exists(const std::string& _path) {
REQUIRE(!boost::filesystem::exists(_path) || boost::filesystem::is_regular_file(_path), "Path '" << _path << "' exists but is not a regular file.");
return boost::filesystem::exists(_path);
}
bool directory_exists(const std::string& _path) {
REQUIRE(!boost::filesystem::exists(_path) || boost::filesystem::is_directory(_path), "Path '" << _path << "' exists but is not a directory.");
return boost::filesystem::exists(_path);
}
......@@ -88,7 +94,7 @@ namespace xerus { namespace misc {
std::string read_file(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);
......@@ -101,6 +107,22 @@ namespace xerus { namespace misc {
}
std::vector<std::string> read_file_lines(const std::string& _path) {
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);
std::vector<std::string> lines(1);
while(getline(fileStream, lines.back())) {
lines.emplace_back();
}
lines.pop_back();
return lines;
}
std::string normalize_pathname(const std::string &_name) {
std::vector<std::string> oldpath = explode(_name,'/');
std::vector<std::string *> newpath;
......
......@@ -44,14 +44,22 @@ namespace xerus {
std::vector<std::string> explode(const std::string& _string, const char _delim) {
std::vector<std::string> result;
std::istringstream iss(_string);
const std::string::size_type length = _string.length();
std::string::size_type pos, lastPos = 0;
std::string token;
while(std::getline(iss, token, _delim)) {
result.push_back(std::move(token));
}
while(lastPos < length + 1) {
pos = _string.find(_delim, lastPos);
if(pos == std::string::npos) { pos = length; }
if(pos != lastPos) {
result.emplace_back(_string, lastPos, pos-lastPos);
}
return result;
lastPos = pos + 1;
}
return result;
}
......
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