From 055e348e8aff8ebc6315bcd37af78adef329b474 Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Sat, 26 Oct 2024 08:49:38 +0100 Subject: [PATCH] Improved exceptions when a file path containing invalid characters is provided. On Windows (under visual studio) if a std::filesystem::path contains ':', extension() returns empty string. There's not an obvious method within std::filesystem to check a path for bad characters. --- include/flamegpu/io/LoggerFactory.h | 5 ++++- include/flamegpu/io/StateReaderFactory.h | 3 +++ include/flamegpu/io/StateWriterFactory.h | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/flamegpu/io/LoggerFactory.h b/include/flamegpu/io/LoggerFactory.h index 1775739c7..ea519b2a0 100644 --- a/include/flamegpu/io/LoggerFactory.h +++ b/include/flamegpu/io/LoggerFactory.h @@ -31,9 +31,12 @@ class LoggerFactory { return std::make_unique(output_path, prettyPrint, truncateFile); } else if (extension == ".json") { return std::make_unique(output_path, prettyPrint, truncateFile); + } else if (extension.empty()) { + THROW exception::InvalidFilePath("Filepath '%s' contains unsuitable characters or lacks a file extension, " + "in LoggerFactory::createLogger().", output_path.c_str()); } THROW exception::UnsupportedFileType("File '%s' is not a type which can be written " - "by StateWriterFactory::createLogger().", + "by LoggerFactory::createLogger().", output_path.c_str()); } }; diff --git a/include/flamegpu/io/StateReaderFactory.h b/include/flamegpu/io/StateReaderFactory.h index 229750624..a5cfd78ca 100644 --- a/include/flamegpu/io/StateReaderFactory.h +++ b/include/flamegpu/io/StateReaderFactory.h @@ -37,6 +37,9 @@ class StateReaderFactory { return new XMLStateReader(); } else if (extension == ".json") { return new JSONStateReader(); + } else if (extension.empty()) { + THROW exception::InvalidFilePath("Filepath '%s' contains unsuitable characters or lacks a file extension, " + "in StateReaderFactory::createLogger().", input.c_str()); } THROW exception::UnsupportedFileType("File '%s' is not a type which can be read " "by StateReaderFactory::createReader().", diff --git a/include/flamegpu/io/StateWriterFactory.h b/include/flamegpu/io/StateWriterFactory.h index 02a3a5f9e..4e1c5d2a8 100644 --- a/include/flamegpu/io/StateWriterFactory.h +++ b/include/flamegpu/io/StateWriterFactory.h @@ -32,6 +32,9 @@ class StateWriterFactory { return new XMLStateWriter(); } else if (extension == ".json") { return new JSONStateWriter(); + } else if (extension.empty()) { + THROW exception::InvalidFilePath("Filepath '%s' contains unsuitable characters or lacks a file extension, " + "in StateWriterFactory::createLogger().", output_file.c_str()); } THROW exception::UnsupportedFileType("File '%s' is not a type which can be written " "by StateWriterFactory::createWriter().",