FileSystemHelpers.h#
Fully qualified name: omni/extras/FileSystemHelpers.h
File members: omni/extras/FileSystemHelpers.h
// SPDX-FileCopyrightText: Copyright (c) 2020-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.
#pragma once
#include "../../carb/InterfaceUtils.h"
#include "../../carb/cpp/StringView.h"
#include "../../carb/cpp/ZStringView.h"
#include "../../carb/extras/Path.h"
#include "../../carb/filesystem/FileSystemUtils.h"
#include "../../carb/tokens/TokensUtils.h"
#include <string>
#include <vector>
namespace omni::extras
{
inline carb::filesystem::IFileSystem* getFileSystem()
{
return carb::getCachedInterface<carb::filesystem::IFileSystem>();
}
inline std::vector<std::string> getDirectoryItems(carb::cpp::zstring_view folder)
{
std::vector<std::string> items;
(void)carb::filesystem::detail::forEachDirectoryItem(
getFileSystem(), folder, false, [&items](const carb::filesystem::FileInfo&, carb::cpp::zstring_view path) {
items.emplace_back(path);
return carb::filesystem::WalkAction::eContinue;
});
return items;
}
inline std::vector<std::string> getDirectoryItemsOfType(carb::cpp::zstring_view folder,
carb::filesystem::DirectoryItemType type)
{
using namespace carb;
std::vector<std::string> items;
(void)filesystem::detail::forEachDirectoryItem(
getFileSystem(), folder, false, [&](const filesystem::FileInfo& info, cpp::zstring_view path) {
if (info.type == type)
items.emplace_back(path);
return filesystem::WalkAction::eContinue;
});
return items;
}
inline std::vector<std::string> getSubfolders(carb::cpp::zstring_view folder)
{
return getDirectoryItemsOfType(folder, carb::filesystem::DirectoryItemType::eDirectory);
}
inline std::vector<std::string> getSubfolders(const std::vector<std::string>& folders)
{
std::vector<std::string> allSubFolders;
for (const std::string& folder : folders)
{
const std::vector<std::string> subFolders = getSubfolders(folder);
allSubFolders.insert(allSubFolders.end(), subFolders.begin(), subFolders.end());
}
return allSubFolders;
}
inline std::string resolvePath(carb::cpp::string_view path, carb::cpp::string_view root = {})
{
auto tokens = carb::getCachedInterface<carb::tokens::ITokens>();
auto resolved = carb::tokens::detail::resolveString(tokens, path).value_or("");
carb::extras::Path resultPath{ resolved };
// If relative - relative to the root
if (!root.empty() && resultPath.isRelative())
{
resultPath = carb::extras::Path(root).join(resultPath);
}
return std::move(resultPath).getString();
}
inline std::pair<bool, std::string> readFile(carb::cpp::zstring_view path)
{
using namespace carb;
auto fs = getFileSystem();
if (filesystem::detail::exists(fs, path))
{
if (auto file = filesystem::detail::openFile(fs, path, filesystem::OpenMode::eRead))
{
if (auto size = filesystem::detail::getFileSize(fs, *file))
{
std::string buffer(*size, ' ');
if (auto readBytes = filesystem::detail::readFileChunk(fs, *file, buffer.data(), *size);
readBytes && *readBytes == *size)
return std::make_pair(true, std::move(buffer));
}
else
return std::make_pair(true, std::string{});
}
}
return std::make_pair(false, std::string{});
}
CARB_FILESYSTEM2_DEPRECATED("Use readFile(carb::cpp::zstring_view) instead")
inline std::pair<bool, std::string> readFile(carb::cpp::unbounded_string path)
{
return readFile(carb::cpp::zstring_view(carb::cpp::unsafe_length, path));
}
} // namespace omni::extras