ImFusion SDK 4.3
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> itemsShallow = d.scan();
std::vector<Filesystem::Path> itemsDeep = d.scanRecursive();
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).
Entity representing a file in the filesystem.
Definition File.h:31
Entity representing a path in the filesystem.
Definition Path.h:58
Directory asDirectory() const
Constructs a Directory instance from this path, convenience function for Filesystem::Directory(*this)...
Path makeRelativeTo(const Path &basePath) const
Returns path made relative to basePath.
Path parentPath() const
Return this path without the last component (filename).
Path makeCanonical() const
It converts the path into an absolute path, cleans it up by removing and resolving elements such as .
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 Assert.h:7
Search Tab / S to search, Esc to close