Skip to content
Snippets Groups Projects
util.hh 3.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • #ifndef __VEREIGN_FS_UTIL_HH
    #define __VEREIGN_FS_UTIL_HH
    
    
    #include <vereign/bytes/buffer.hh>
    
    #include <boost/filesystem/path.hpp>
    #include <string>
    #include <string_view>
    
    namespace vereign::fs {
    
    namespace detail {
    
    auto PathToString(const boost::filesystem::path& path) -> std::string;
    auto StringToPath(std::string_view path) -> boost::filesystem::path;
    
    } // namespace detail
    
    /**
     * A RAII guard that deletes a file upon destruction.
     */
    class RemoveFileGuard {
    public:
      RemoveFileGuard(std::string path);
      ~RemoveFileGuard();
    
      RemoveFileGuard(const RemoveFileGuard&) = delete;
      auto operator=(const RemoveFileGuard&) -> RemoveFileGuard& = delete;
    private:
      std::string path_;
    };
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * A RAII guard that deletes all files and directories recursively.
     */
    
    class RemoveAllGuard {
    public:
      RemoveAllGuard(std::string path);
      ~RemoveAllGuard();
    
      RemoveAllGuard(const RemoveAllGuard&) = delete;
      auto operator=(const RemoveAllGuard&) -> RemoveAllGuard& = delete;
    private:
      std::string path_;
    };
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * Generates a file path usable as temporary file.
     *
     * For a temp file path under the system tmp dir use the other TempFilePath overload.
     *
     * @code
     * auto path = fs::TempFilePath("/tmp/foo", "test_db_");
     * std::cout << path << std::endl;
     *
     * // Output:
     * // /tmp/foo/test_db_bh0vcr0jbz
     * @endcode
     *
     * @param dir The directory of the temp file path.
     * @param prefix A prefix to prepend to the temp file name.
     * @returns a file path usable as temporary file.
     */
    
    auto TempFilePath(std::string_view dir, std::string_view prefix) -> std::string;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
    /**
     * Generates a file path usable as temporary file under the system temporary directory.
     *
     * It tries to detect the correct temp dir under different operation systems.
     * For Linux this is `/tmp` and for Windows this is `C:\Users\<username>\AppData\Local\Temp`.
     *
     * @code
     * auto path = fs::TempFilePath("test_db_");
     * std::cout << path << std::endl;
     *
     * // Output:
     * // /tmp/test_db_bh0vcr0jbz
     * @endcode
     *
     * @param prefix A prefix to prepend to the temp file name.
     * @returns a file path usable as temporary file.
     */
    
    auto TempFilePath(std::string_view prefix) -> std::string;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * Creates a temporary sub directory under the system temporary directory.
     *
     * It tries to detect the correct temp dir under different operation systems.
     * For Linux this is `/tmp` and for Windows this is `C:\Users\<username>\AppData\Local\Temp`.
     *
     * @code
     * auto path = fs::TempDir("test_db_");
     * std::cout << path << std::endl;
     *
     * // Output:
     * // /tmp/test_db_bh0vcr0jbz
     * @endcode
     *
     * @param prefix A prefix to prepend to the temp dir name.
     * @returns a the path of the created temporary directory.
     */
    
    auto TempDir(std::string_view prefix) -> std::string;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * Returns current user home directory.
     *
     * On Windows this is `C:\Users\<username>\AppData\Local`.
     *
     * @returns the user's home directory full path.
     */
    
    auto HomePath() -> std::string;
    
    
    auto ReadFile(std::string_view path) -> bytes::Buffer;
    void WriteFile(std::string_view path, bytes::View data);
    
    
    } // namespace vereign::fs
    
    #endif // __VEREIGN_FS_UTIL_HH