Skip to content
Snippets Groups Projects
Commit d86d8d27 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Code beautification in FileSystemUtils

parent 1c748664
No related branches found
No related tags found
No related merge requests found
...@@ -15,11 +15,10 @@ ...@@ -15,11 +15,10 @@
#include "FileSystemUtils.h" #include "FileSystemUtils.h"
#include "Exceptions.h" #include "Exceptions.h"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <cassert>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <cassert>
#include <stdexcept> #include <stdexcept>
//! Returns extension of given filename.
std::string FileSystemUtils::extension(const std::string& name) std::string FileSystemUtils::extension(const std::string& name)
{ {
return boost::filesystem::extension(name.c_str()); return boost::filesystem::extension(name.c_str());
...@@ -27,31 +26,30 @@ std::string FileSystemUtils::extension(const std::string& name) ...@@ -27,31 +26,30 @@ std::string FileSystemUtils::extension(const std::string& name)
bool FileSystemUtils::createDirectory(const std::string& dir_name) bool FileSystemUtils::createDirectory(const std::string& dir_name)
{ {
assert(dir_name!=""); assert(dir_name != "");
return boost::filesystem::create_directory(dir_name); return boost::filesystem::create_directory(dir_name);
} }
//! Returns filenames of files in directory
std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name) std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name)
{ {
std::vector<std::string> ret; std::vector<std::string> ret;
if (!boost::filesystem::exists(dir_name)) if (!boost::filesystem::exists(dir_name))
throw std::runtime_error( throw std::runtime_error("FileSystemUtils::filesInDirectory '" + dir_name
"FileSystemUtils::filesInDirectory '" + dir_name + "' does not exist"); + "' does not exist");
boost::filesystem::directory_iterator end_it; // default construction yields past-the-end boost::filesystem::directory_iterator end_it; // default construction yields past-the-end
for ( boost::filesystem::directory_iterator it( dir_name ); for (boost::filesystem::directory_iterator it(dir_name);
it != boost::filesystem::directory_iterator(); ++it ) { it != boost::filesystem::directory_iterator(); ++it) {
if( !boost::filesystem::is_regular_file( it->status() ) ) if (!boost::filesystem::is_regular_file(it->status()))
continue; continue;
ret.push_back( it->path().filename().string() ); ret.push_back(it->path().filename().string());
} }
return ret; return ret;
} }
std::string FileSystemUtils::jointPath(const std::string& spath1, const std::string& spath2) std::string FileSystemUtils::jointPath(const std::string& spath1, const std::string& spath2)
{ {
assert(spath1!=""); assert(spath1 != "");
assert(spath2!=""); assert(spath2 != "");
boost::filesystem::path path1(spath1); boost::filesystem::path path1(spath1);
boost::filesystem::path path2(spath2); boost::filesystem::path path2(spath2);
boost::filesystem::path full_path = path1 / path2; boost::filesystem::path full_path = path1 / path2;
...@@ -59,17 +57,15 @@ std::string FileSystemUtils::jointPath(const std::string& spath1, const std::str ...@@ -59,17 +57,15 @@ std::string FileSystemUtils::jointPath(const std::string& spath1, const std::str
return full_path.string(); return full_path.string();
} }
//! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
std::string FileSystemUtils::filename(const std::string& path) std::string FileSystemUtils::filename(const std::string& path)
{ {
return boost::filesystem::path(path).filename().string(); return boost::filesystem::path(path).filename().string();
} }
//! Returns file names that agree with a regex glob pattern.
std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern) std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern)
{ {
std::vector<std::string> ret; std::vector<std::string> ret;
for (const std::string& fname: filesInDirectory(dir)) for (const std::string& fname : filesInDirectory(dir))
if (boost::regex_match(fname, boost::regex(pattern))) if (boost::regex_match(fname, boost::regex(pattern)))
ret.push_back(fname); ret.push_back(fname);
return ret; return ret;
......
...@@ -23,24 +23,23 @@ ...@@ -23,24 +23,23 @@
namespace FileSystemUtils { namespace FileSystemUtils {
//! Returns extension of given filename. //! Returns extension of given filename.
BA_CORE_API_ std::string extension(const std::string& fname); BA_CORE_API_ std::string extension(const std::string& fname);
//! Creates directory in current directory //! Creates directory in current directory
BA_CORE_API_ bool createDirectory(const std::string& dir_name); BA_CORE_API_ bool createDirectory(const std::string& dir_name);
//! Returns filenames of files in directory //! Returns filenames of files in directory
BA_CORE_API_ std::vector<std::string> filesInDirectory(const std::string& dir_name); BA_CORE_API_ std::vector<std::string> filesInDirectory(const std::string& dir_name);
//! Returns joint path name. //! Returns joint path name.
BA_CORE_API_ std::string jointPath(const std::string& spath1, const std::string& spath2); BA_CORE_API_ std::string jointPath(const std::string& spath1, const std::string& spath2);
//! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz") //! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
BA_CORE_API_ std::string filename(const std::string& path); BA_CORE_API_ std::string filename(const std::string& path);
//! Returns file names that agree with a regex glob pattern. //! Returns file names that agree with a regex glob pattern.
BA_CORE_API_ std::vector<std::string> glob( BA_CORE_API_ std::vector<std::string> glob(const std::string& dir, const std::string& pattern);
const std::string& dir, const std::string& pattern);
} // namespace FileSystemUtils } // namespace FileSystemUtils
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment