10 #include <boost/format.hpp> 11 #include <boost/filesystem.hpp> 15 using namespace se2ez;
19 boost::filesystem::path expandHome(
const boost::filesystem::path &in)
24 throw std::runtime_error(
"HOME Environment variable is not set! Cannot resolve ~ in path.");
28 boost::filesystem::path out;
29 for (
const auto &p : in)
30 out /= (p.string() ==
"~") ? home : p;
35 boost::filesystem::path expandSymlinks(
const boost::filesystem::path &in)
37 boost::filesystem::path out;
38 for (
const auto &p : in)
41 if (boost::filesystem::is_symlink(tmp))
42 out = boost::filesystem::read_symlink(tmp);
50 boost::filesystem::path expandPath(
const boost::filesystem::path &in)
52 boost::filesystem::path out = in;
53 out = expandHome(out);
54 out = expandSymlinks(out);
56 return boost::filesystem::absolute(out);
73 boost::filesystem::path path(path_string);
74 const std::string last = boost::filesystem::extension(path);
75 return isSuffix(extension, last);
81 boost::filesystem::path file = expandPath(path).
string();
83 if (!boost::filesystem::exists(file))
86 return boost::filesystem::canonical(boost::filesystem::absolute(file)).string();
92 if (full_path.
empty())
97 std::ifstream::pos_type size = ifs.
tellg();
98 ifs.seekg(0, std::ios::beg);
101 ifs.read(bytes.
data(), size);
114 while (!feof(pipe.
get()))
116 if (fgets(buffer.
data(), 128, pipe.
get()) !=
nullptr)
117 result += buffer.
data();
127 if (full_path.
empty())
130 if (!isExtension(full_path,
"yml") && !isExtension(full_path,
"yaml"))
159 boost::filesystem::create_directories(dir);
164 boost::filesystem::path path(file);
165 path = expandHome(path);
166 path = expandSymlinks(path);
168 const auto parent = path.parent_path().string();
173 out.
open(path.string(), std::ofstream::out | std::ofstream::trunc);
181 if (full_path.
empty())
184 boost::filesystem::path path(full_path);
185 if (!boost::filesystem::is_directory(path))
188 for (
auto it = boost::filesystem::directory_iterator(path); it != boost::filesystem::directory_iterator();
197 return boost::posix_time::microsec_clock::local_time();
202 boost::char_separator<char> seps(separators.
c_str());
203 boost::tokenizer<boost::char_separator<char>> tokenizer(s, seps);
void sleep(double seconds)
Have this thread sleep for some number of seconds.
const std::string loadFileToString(const std::string &path)
Loads a file to a string.
void createDirectory(const std::string &dir)
Creates a directory.
const std::pair< bool, YAML::Node > loadFileToYAML(const std::string &path)
Loads a file to a YAML node.
void createFile(std::ofstream &out, const std::string &file)
Creates a file and opens an output stream. Creates directories if they do not exist.
boost::posix_time::ptime getDate()
Get the current time (up to milliseconds)
std::vector< std::string > tokenize(const std::string &string, const std::string &separators)
Separates a string into tokens, based upon separators.
std::string format(const std::string &fmt, Args &&... args)
const std::string runCommand(const std::string &cmd)
Runs a command cmd and returns stdout as a string.
bool YAMLToFile(const YAML::Node &node, const std::string &file)
Write the contents of a YAML node out to a potentially new file.
const std::string resolvePath(const std::string &path)
Resolves file paths to their canonical form.
const std::pair< bool, std::vector< std::string > > listDirectory(const std::string &directory)
Lists of the contents of a directory.
T emplace_back(T... args)