se2ez
io.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef SE2EZ_CORE_IO_
4 #define SE2EZ_CORE_IO_
5 
6 #include <string>
7 #include <utility>
8 #include <fstream>
9 
10 #include <boost/date_time.hpp>
11 
12 #include <yaml-cpp/yaml.h>
13 
14 namespace se2ez
15 {
16  namespace io
17  {
18  /** \brief Resolves file paths to their canonical form.
19  * \param[in] path Path to resolve.
20  * \return The canonical path, or "" on failure.
21  */
22  const std::string resolvePath(const std::string &path);
23 
24  /** \brief Loads a file to a string.
25  * \param[in] path File to load.
26  * \return The loaded file, or "" on failure (file does not exist).
27  */
28  const std::string loadFileToString(const std::string &path);
29 
30  /** \brief Runs a command \a cmd and returns stdout as a string.
31  * \param[in] cmd Command to run.
32  * \return Contents of stdout from \a cmd, or "" on failure.
33  */
34  const std::string runCommand(const std::string &cmd);
35 
36  /** \brief Loads a file to a YAML node.
37  * \param[in] path File to load.
38  * \return A pair, where the first is true on success false on failure, and second is the YAML node.
39  */
41 
42  /** \brief Creates a directory.
43  * \param[in] dir Directory to create.
44  */
45  void createDirectory(const std::string &dir);
46 
47  /** \brief Creates a file and opens an output stream. Creates directories if they do not exist.
48  * \param[out] out Output stream to initialize.
49  * \param[in] file File to create and open.
50  */
51  void createFile(std::ofstream &out, const std::string &file);
52 
53  /** \brief Lists of the contents of a directory.
54  * \param[in] directory Directory to list.
55  * \return A pair of a bool and a vector of strings of filenames of the directories contents. The
56  * first element will be true on success, false on failure. These filenames are absolute paths.
57  */
59 
60  /** \brief Get the current time (up to milliseconds)
61  * \return The time.
62  */
63  boost::posix_time::ptime getDate();
64 
65  /** \brief Separates a \a string into tokens, based upon \a separators.
66  * \param[in] string String to tokenize.
67  * \param[in] separators Separators to split string on.
68  * \return The tokenized string.
69  */
70  std::vector<std::string> tokenize(const std::string &string, const std::string &separators);
71 
72  /** \brief Write the contents of a YAML node out to a potentially new file.
73  * \param[in] node Node to write.
74  * \param[in] file Filename to open.
75  * \return True on success, false otherwise.
76  */
77  bool YAMLToFile(const YAML::Node &node, const std::string &file);
78 
79  /** \brief Have this thread sleep for some number of seconds.
80  * \param[in] seconds Seconds to sleep for.
81  */
82  void sleep(double seconds);
83  } // namespace io
84 } // namespace se2ez
85 
86 #endif
void sleep(double seconds)
Have this thread sleep for some number of seconds.
Definition: io.cpp:207
const std::string loadFileToString(const std::string &path)
Loads a file to a string.
Definition: io.cpp:89
void createDirectory(const std::string &dir)
Creates a directory.
Definition: io.cpp:157
const std::pair< bool, YAML::Node > loadFileToYAML(const std::string &path)
Loads a file to a YAML node.
Definition: io.cpp:123
void createFile(std::ofstream &out, const std::string &file)
Creates a file and opens an output stream. Creates directories if they do not exist.
Definition: io.cpp:162
boost::posix_time::ptime getDate()
Get the current time (up to milliseconds)
Definition: io.cpp:195
std::vector< std::string > tokenize(const std::string &string, const std::string &separators)
Separates a string into tokens, based upon separators.
Definition: io.cpp:200
Main namespace.
Definition: collision.h:11
const std::string runCommand(const std::string &cmd)
Runs a command cmd and returns stdout as a string.
Definition: io.cpp:106
bool YAMLToFile(const YAML::Node &node, const std::string &file)
Write the contents of a YAML node out to a potentially new file.
Definition: io.cpp:143
const std::string resolvePath(const std::string &path)
Resolves file paths to their canonical form.
Definition: io.cpp:79
const std::pair< bool, std::vector< std::string > > listDirectory(const std::string &directory)
Lists of the contents of a directory.
Definition: io.cpp:176