#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_TEMPORARYDIRECTORYSCOPE_H #define NUCLEX_SUPPORT_TEMPORARYDIRECTORYSCOPE_H #include "Nuclex/Support/Config.h" #include // for std::string #include // for std::vector #include // for std::uint8_t namespace Nuclex { namespace Support { // ------------------------------------------------------------------------------------------- // /// Creates a directory that is deleted when the scope is destroyed /// /// /// This is very useful for unit tests or if you're dealing with a poorly designed /// library that can only read resources from the file system rather than providing /// an abstract IO interface. /// /// /// When the scope is destroyed, it deletes all files inside /// the created temporary directory, include those placed in there by means other /// than the method. /// /// /// /// void test() { /// TemporaryDirectoryScope tempDir(u8"abc"); // custom directory name prefix /// /// // GetPath() can provide you with the absolute path to a file inside /// // the temporary directory (it does not create the requested file itself) /// save_current_settings(tempDir.GetPath(u8"settings.bin")); /// /// // Settings can be loaded into an std::string or std::vector using different /// // overloads provided by the temporary directory scope. /// std::vector savedSettings = tempDir.ReadFile(u8"settings.bin"); /// /// // Similarly, you can also place your own file in the temporary directory /// tempDir.PlaceFile(u8"message.txt", u8"Hello World"); /// /// // The temporary directory and all files in it are deleted here /// } /// /// /// class NUCLEX_SUPPORT_TYPE TemporaryDirectoryScope { /// Reserves and creates a unique temporary directory /// Prefix for the temporary directory name public: NUCLEX_SUPPORT_API TemporaryDirectoryScope( const std::string &namePrefix = u8"tmp" ); /// Deletes the temporary directory again public: NUCLEX_SUPPORT_API ~TemporaryDirectoryScope(); /// Returns the full, absolute path to the temporary directory /// The absolute path of the temporary directory as an UTF-8 string public: NUCLEX_SUPPORT_API const std::string &GetPath() const { return this->path; } /// Returns the absolute path to a file in the temporary directory /// /// Name of the file for which the absolute path will be returned /// /// /// The absolute path of a file with the specified name as an UTF-8 string /// /// /// This method does not create a file. It is inteded to be used when you need to /// obtain an absolute path to pass to some external library that writes a file. /// public: NUCLEX_SUPPORT_API std::string GetPath(const std::string &filename) const; /// Places a file with the specified contents in the temporary directory /// Name of the file that will be created /// String whose contents will be written into the file /// The full path of the newly created file public: NUCLEX_SUPPORT_API std::string PlaceFile( const std::string &name, const std::string &text ) { return PlaceFile( name, reinterpret_cast(text.c_str()), text.length() ); } /// Places a file with the specified contents in the temporary directory /// Name of the file that will be created /// Contents that will be written into the file /// The full path of the newly created file public: NUCLEX_SUPPORT_API std::string PlaceFile( const std::string &name, const std::vector &contents ) { return PlaceFile(name, contents.data(), contents.size()); } /// Places a file with the specified contents in the temporary directory /// Name of the file that will be created /// Memory block containing the new file contents /// Number of bytes that will be written to the file /// The full path of the newly created file public: NUCLEX_SUPPORT_API std::string PlaceFile( const std::string &name, const std::uint8_t *contents, std::size_t byteCount ); /// Reads the whole contents of a file in the temporary directory /// Name of the file that will be read /// A vector containing all of the file's contents public: NUCLEX_SUPPORT_API std::vector ReadFile( const std::string &name ) const { std::vector contents; ReadFile(name, contents); return contents; } /// Reads the whole contents of a file in the temporary directory /// Name of the file that will be read /// A vector to which the file's contents will be appended public: NUCLEX_SUPPORT_API void ReadFile( const std::string &name, std::vector &contents ) const; /// Reads the whole contents of a file in the temporary directory /// Name of the file that will be read /// A string to which the file's contents will be appended public: NUCLEX_SUPPORT_API void ReadFile(const std::string &name, std::string &contents) const; /// The full path to the temporary directory private: std::string path; }; // ------------------------------------------------------------------------------------------- // }} // namespace Nuclex::Support #endif // NUCLEX_SUPPORT_TEMPORARYDIRECTORYSCOPE_H