ImFusion C++ SDK 4.4.0
Filesystem.cpp
#include <ImFusion/Core/Filesystem/Directory.h>
#include <ImFusion/Core/Filesystem/File.h>
#include <ImFusion/Core/Filesystem/Path.h>
using namespace ImFusion;
std::string nativePathAsString = "C:\Program Files\ImFusion Suite";
Filesystem::Path fsPath = nativePathAsString; // will convert native path separators to generic ones
LOG_INFO(fsPath.parentPath()); // logs "C:/Program Files"
IMFUSION_ASSERT(std::string(fsPath) == "C:/Program Files/ImFusion Suite");
Filesystem::Path projectDirectoryPath{"/home/username/Development"};
Filesystem::Path dataDirectoryPath = projectDirectoryPath / "data"; // "/home/username/Development/data"
Filesystem::Path testDataPath = projectDirectoryPath / Path("test"); // "/home/username/Development/data/test"
IMFUSION_ASSERT(projectDirectoryPath != testDataPath);
Filesystem::Path dataDirectoryPath =
Filesystem::Path{"/home/username/Development/./Data/symbolic_link_image.imf"}.makeCanonical();
// => dataDirectoryPath == "/data/real_image.imf"
Filesystem::Path relativePath = dataDirectoryPath.makeRelativeTo("/data");
// => relativePath == "real_image.imf"
Filesystem::Directory d{"/home/username/Data"};
std::vector<Filesystem::Path> dicomFiles = d.scanRecursive([](Path p) { p.getExtension() == ".dcm" });
Filesystem::File file{"/home/username/Data/myimage.imf"};
if (file.exists())
{
std::cout << "Size: " << file.size() << " bytes" << std::endl;
std::cout << "Directory: " << file.parentDirectory(); // -> /home/username/Data
}
Filesystem::File file{"/home/username/Data/zipfile.tar.gz"};
std::cout << "Parent Path: " << file.parentPath() << std::endl; // -> "/home/username/Data"
std::cout << "Name: " << file.name() << std::endl; // -> "zipfile.tar.gz"
std::cout << "Basename: " << file.path().baseName() << std::endl; // -> "zipfile"
std::cout << "Full Basename: " << file.path().fullBaseName() << std::endl; // -> "zipfile.tar"
std::cout << "Extension: " << file.path().extension() << std::endl; // -> "gz"
std::cout << "Full Extension: " << file.path().fullExtension() << std::endl; // -> "tar.gz"
Filesystem::Path savePath{"/home/username/data/project/my file.bin"};
savePath.asDirectory().createRecursive(); // make sure the parent directory actually exists
savePath.asFile().writeBinary(data); // create the file and write data to it
Entity representing a Directory in the filesystem.
Definition Directory.h:28
bool createRecursive() const
Recursively create the directory (including parent directories if needed).
std::vector< Path > scan(const std::function< bool(const Path &)> &filter=nullptr, SortOptions options=SortOption::Name) const
Scans the directory and collects the paths inside that pass the (optional) lambda filter.
std::vector< Path > scanRecursive(const std::function< bool(const Path &)> &filter=nullptr) const
Recursively scans the directory and collects the paths inside that pass the (optional) lambda filter.
Entity representing a file in the filesystem.
Definition File.h:31
const Path & path() const
Gets the path of the file.
Definition File.h:52
bool exists() const
Checks if the file exists and actually is a file.
std::string name() const
Gets the name of the file including the extension.
bool writeBinary(const ByteBufferView &data) const
Write the given data to the file in a binary fashion.
Path parentPath() const
Returns the absolute path to the parent directory, convenience function for path()....
Directory parentDirectory() const
Returns the absolute path to the parent directory, convenience function for Directory(path()....
ByteSize size() const
Gets the size of the file in bytes.
Entity representing a path in the filesystem.
Definition Path.h:58
std::string fullBaseName() const
Gets the base resource name until the last dot (essentially filename - extension).
Path makeRelativeTo(const Path &basePath) const
Returns path made relative to basePath.
std::string extension() const
Gets the last extension of the path not including the dot (essentially filename - fullBaseName).
File asFile() const
Constructs a File instance from this path, convenience function for Filesystem::File(*this).
Directory asDirectory() const
Constructs a Directory instance from this path, convenience function for Filesystem::Directory(*this)...
std::string fullExtension() const
Gets the full extension of the path not including the first dot (essentially filename - baseName).
Path makeCanonical() const
It converts the path into an absolute path, cleans it up by removing and resolving elements such as .
Path parentPath() const
Return this path without the last component (filename).
std::string baseName() const
Gets the base resource name until the first dot (essentially filename - fullExtension).
T endl(T... args)
#define IMFUSION_ASSERT(...)
ImFusion assertion macro, supports two overloads:
Definition Assert.h:102
#define LOG_INFO(...)
Emits a log message of Log::Level::Info, optionally with a category.
Definition Log.h:247
Namespace of the ImFusion SDK.
Definition Changelog.dox:1
Search Tab / S to search, Esc to close