Using fmt::format to format exception messages, using .extension() method to retrieve an extension

This commit is contained in:
Vladyslav Baranovskyi
2025-02-12 14:38:38 +02:00
parent 1f9154b46c
commit fdbf36c040
2 changed files with 12 additions and 18 deletions

View File

@@ -6,6 +6,7 @@
#include "MeshWriter.hpp" #include "MeshWriter.hpp"
#include "Base/Utils.hpp" #include "Base/Utils.hpp"
#include "Extensions/FmtFormatter.hpp"
#include "IO/MemMappedFile.hpp" #include "IO/MemMappedFile.hpp"
#include "Scene/Geometry.hpp" #include "Scene/Geometry.hpp"
#include "Scene/Vertex.hpp" #include "Scene/Vertex.hpp"
@@ -107,7 +108,7 @@ namespace OpenVulkano::Scene
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.is_open()) [[unlikely]] if (!file.is_open()) [[unlikely]]
throw std::runtime_error("Failed to open file '" + filePath.string() + "' for writing!"); throw std::runtime_error(fmt::format("Failed to open file '{}' for writing!", filePath));
WriteObjContents(geometry, "", file); WriteObjContents(geometry, "", file);
file.close(); file.close();
@@ -117,7 +118,7 @@ namespace OpenVulkano::Scene
{ {
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.is_open()) [[unlikely]] if (!file.is_open()) [[unlikely]]
throw std::runtime_error("Failed to open file '" + filePath.string() + "' for writing!"); throw std::runtime_error(fmt::format("Failed to open file '{}' for writing!", filePath));
WriteUsdContents(file, geometry); WriteUsdContents(file, geometry);
file.close(); file.close();
} }
@@ -190,7 +191,7 @@ namespace OpenVulkano::Scene
if (result != aiReturn_SUCCESS) if (result != aiReturn_SUCCESS)
{ {
throw std::runtime_error("Unable to write STL file to " + filePath.string() + ": " + exporter.GetErrorString()); throw std::runtime_error(fmt::format("Unable to write STL file to {}: {}", filePath, exporter.GetErrorString()));
} }
#else #else
throw std::runtime_error("Unable to export to STL: Assimp is not available!"); throw std::runtime_error("Unable to export to STL: Assimp is not available!");
@@ -226,7 +227,7 @@ namespace OpenVulkano::Scene
if (result != aiReturn_SUCCESS) if (result != aiReturn_SUCCESS)
{ {
throw std::runtime_error("Unable to write a fbx file to " + fbxPath.string() + ": " + exporter.GetErrorString()); throw std::runtime_error(fmt::format("Unable to write a fbx file to {}: {}", fbxPath, exporter.GetErrorString()));
} }
#else #else
throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!");

View File

@@ -7,6 +7,7 @@
#include "MeshLoader.hpp" #include "MeshLoader.hpp"
#include "Scene/Geometry.hpp" #include "Scene/Geometry.hpp"
#include "Base/Logger.hpp" #include "Base/Logger.hpp"
#include "Extensions/FmtFormatter.hpp"
#if __has_include("assimp/Importer.hpp") #if __has_include("assimp/Importer.hpp")
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/scene.h> #include <assimp/scene.h>
@@ -26,14 +27,6 @@
#include "value-pprint.hh" #include "value-pprint.hh"
#include "value-types.hh" #include "value-types.hh"
namespace
{
static bool ends_with(std::string_view str, std::string_view suffix)
{
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
}
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void MeshLoader::ParseAssimpFile(Geometry *geometry, const std::filesystem::path& file) void MeshLoader::ParseAssimpFile(Geometry *geometry, const std::filesystem::path& file)
@@ -46,8 +39,8 @@ namespace OpenVulkano::Scene
aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices | aiProcess_OptimizeGraph; aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices | aiProcess_OptimizeGraph;
const aiScene* scene = importer.ReadFile(file.string().c_str(), flags); const aiScene* scene = importer.ReadFile(file.string().c_str(), flags);
if (!scene) throw std::runtime_error("Failed to load file \"" + file.string() + "\" Error: " + importer.GetErrorString()); if (!scene) throw std::runtime_error(fmt::format("Failed to load file \"{}\" Error: {}", file, importer.GetErrorString()));
if (!scene->HasMeshes()) throw std::runtime_error("File \"" + file.string() + "\" does not have any meshes"); if (!scene->HasMeshes()) throw std::runtime_error(fmt::format("File \"{}\" does not have any meshes", file));
if (scene->mNumMeshes > 1) Logger::DATA->warn("File {0} contains more than one mesh. Only first one will be loaded", file.string()); if (scene->mNumMeshes > 1) Logger::DATA->warn("File {0} contains more than one mesh. Only first one will be loaded", file.string());
aiMesh *mesh = scene->mMeshes[0]; aiMesh *mesh = scene->mMeshes[0];
@@ -99,7 +92,7 @@ namespace OpenVulkano::Scene
auto result = tinyusdz::LoadUSDFromFile(file.string().c_str(), &stage, &warning, &err); auto result = tinyusdz::LoadUSDFromFile(file.string().c_str(), &stage, &warning, &err);
if (!result) if (!result)
{ {
throw std::runtime_error("Failed to load USD file: " + file.string()); throw std::runtime_error(fmt::format("Failed to load USD file: {}", file));
} }
for (auto &prim : stage.root_prims()) for (auto &prim : stage.root_prims())
@@ -148,15 +141,15 @@ namespace OpenVulkano::Scene
} }
} }
} }
throw std::runtime_error("No mesh found inside a xform in USD file: " + file.string()); throw std::runtime_error(fmt::format("No mesh found inside a xform in USD file: {}", file));
} }
Geometry* MeshLoader::LoadFromFile(const std::filesystem::path& file) Geometry* MeshLoader::LoadFromFile(const std::filesystem::path& file)
{ {
Geometry* geometry = new Geometry(); Geometry* geometry = new Geometry();
std::string fileStr = file.string(); const std::filesystem::path ext = file.extension();
if (ends_with(fileStr, ".usd") || ends_with(fileStr, ".usda") || ends_with(fileStr, ".usdc") || ends_with(fileStr, ".usdz")) if (ext == ".usd" || ext == ".usda" || ext == ".usdc" || ext == ".usdz")
{ {
ParseUSDFile(geometry, file); ParseUSDFile(geometry, file);
} }