DDS  ver. 3.4
BOOST_FILESYSTEM.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // BOOST filesystem library helper
4 //
5 #ifndef BOOST_FILESYSTEM_H_
6 #define BOOST_FILESYSTEM_H_
7 
8 // BOOST
9 #define BOOST_NO_CXX11_SCOPED_ENUMS
10 #include <boost/filesystem.hpp>
11 #undef BOOST_NO_CXX11_SCOPED_ENUMS
12 #include <boost/format.hpp>
13 // MiscCommon
14 #include "MiscUtils.h"
15 // STD
16 #include <chrono>
17 #include <sstream>
18 
19 namespace fs = boost::filesystem;
20 
21 namespace MiscCommon
22 {
23  namespace BOOSTHelper
24  {
25  inline std::string get_temp_dir(const std::string& _prefix)
26  {
27  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
28  std::time_t nowTimeT = std::chrono::system_clock::to_time_t(now);
29  struct std::tm* ptm = std::localtime(&nowTimeT);
30  char buffer[128];
31  std::strftime(buffer, 128, "%Y-%m-%d-%H-%M-%S-", ptm);
32  std::chrono::milliseconds ms =
33  std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
34  std::stringstream timeSS;
35  timeSS << _prefix << "_" << buffer << boost::format("%03i") % (ms.count() % 1000);
36  return timeSS.str();
37  }
38 
47  inline void get_files_by_extension(const fs::path& _root, const std::string& _ext, std::vector<fs::path>& _ret)
48  {
49  if (!fs::exists(_root))
50  return;
51 
52  if (fs::is_directory(_root))
53  {
54  fs::recursive_directory_iterator it(_root);
55  fs::recursive_directory_iterator endit;
56  while (it != endit)
57  {
58  if (fs::is_regular_file(*it) && it->path().extension() == _ext)
59  {
60  _ret.push_back(it->path());
61  }
62  ++it;
63  }
64  }
65  }
66 
74  inline std::string normalize_path(const std::string& _path)
75  {
76  std::string path(_path);
77  MiscCommon::trim_right<std::string>(&path, '/');
78  return path;
79  }
87  inline bool is_file(const std::string& _pathname)
88  {
89  bool is_valid = false;
90 
91  try
92  {
93  fs::path cp(normalize_path(_pathname)); //, fs::native);
94  is_valid = !(fs::is_directory(cp));
95  }
96  catch (const fs::filesystem_error& _ex)
97  {
98  }
99 
100  return is_valid;
101  }
109  inline bool is_directory(const std::string& _pathname)
110  {
111  bool is_valid = false;
112  try
113  {
114  fs::path cp(normalize_path(_pathname)); //, fs::native);
115  is_valid = fs::is_directory(cp);
116  }
117  catch (const fs::filesystem_error& _ex)
118  {
119  }
120 
121  return is_valid;
122  }
123  }; // namespace BOOSTHelper
124 }; // namespace MiscCommon
125 #endif /*BOOST_FILESYSTEM_H_*/
std::string get_temp_dir(const std::string &_prefix)
Definition: BOOST_FILESYSTEM.h:25
bool is_directory(const std::string &_pathname)
the is_directory() function checks whether the pathname represents a directory or not
Definition: BOOST_FILESYSTEM.h:109
bool is_file(const std::string &_pathname)
The is_file() function checks whether the pathname represents a file or not.
Definition: BOOST_FILESYSTEM.h:87
void get_files_by_extension(const fs::path &_root, const std::string &_ext, std::vector< fs::path > &_ret)
The function return a list of files in the deirectory with specified extension.
Definition: BOOST_FILESYSTEM.h:47
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21
std::string normalize_path(const std::string &_path)
The normalize_path() function removes '/' characters at the end of the of the input pathname.
Definition: BOOST_FILESYSTEM.h:74