From 8671792b3630d108a5f6da7da08d1a2e8ef2784c Mon Sep 17 00:00:00 2001 From: Erik Smistad Date: Fri, 8 Dec 2023 10:03:04 +0100 Subject: [PATCH 1/4] Handle windows slashes in path in MetaImageExporter --- source/FAST/Exporters/FileExporter.cpp | 4 ++++ .../Exporters/Tests/MetaImageExporterTests.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/source/FAST/Exporters/FileExporter.cpp b/source/FAST/Exporters/FileExporter.cpp index 559ff675f..5c85a2d0b 100644 --- a/source/FAST/Exporters/FileExporter.cpp +++ b/source/FAST/Exporters/FileExporter.cpp @@ -3,7 +3,11 @@ namespace fast { void FileExporter::setFilename(std::string filename) { +#ifdef WIN32 + m_filename = replace(filename, "\\", "/"); +#else m_filename = filename; +#endif setModified(true); } diff --git a/source/FAST/Exporters/Tests/MetaImageExporterTests.cpp b/source/FAST/Exporters/Tests/MetaImageExporterTests.cpp index 9a246ccc4..7cee5764e 100644 --- a/source/FAST/Exporters/Tests/MetaImageExporterTests.cpp +++ b/source/FAST/Exporters/Tests/MetaImageExporterTests.cpp @@ -19,6 +19,20 @@ TEST_CASE("No input given to the MetaImageExporter", "[fast][MetaImageExporter]" CHECK_THROWS(exporter->update()); } + +#ifdef WIN32 +TEST_CASE("Windows path slashes used in MetaImageExporter", "[fast][MetaImageExporter]") { + auto image = Image::create(32, 32, TYPE_UINT8, 1); // Create dummy image + createDirectories("mhd_test"); + auto exporter = MetaImageExporter::create("mhd_test\\image.mhd") + ->connect(image); + CHECK_NOTHROW(exporter->run()); + + auto importer = MetaImageImporter::create("mhd_test/image.mhd"); + CHECK_NOTHROW(importer->run()); +} +#endif + TEST_CASE("Write a 2D image with the MetaImageExporter", "[fast][MetaImageExporter]") { // Create some metadata Vector3f spacing; From 8fb80790055c365600b0f8f881a213506acff5e8 Mon Sep 17 00:00:00 2001 From: Erik Smistad Date: Fri, 8 Dec 2023 10:30:23 +0100 Subject: [PATCH 2/4] Fixed some issues with createDirectories() --- source/FAST/Tests/UtilityTests.cpp | 24 ++++++++++++++++++++++++ source/FAST/Utility.cpp | 10 +++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/source/FAST/Tests/UtilityTests.cpp b/source/FAST/Tests/UtilityTests.cpp index 483b4f3e9..0eba707b5 100644 --- a/source/FAST/Tests/UtilityTests.cpp +++ b/source/FAST/Tests/UtilityTests.cpp @@ -34,6 +34,10 @@ TEST_CASE("Split", "[split][utility]") { CHECK(parts[0] == "\"Wee sad"); CHECK(parts[1] == "asd asd "); CHECK(parts[2] == "asdasd\""); + + parts = split("test", "/"); + REQUIRE(parts.size() == 1); + CHECK(parts[0] == "test"); } TEST_CASE("Replace", "[replace][utility]") { @@ -65,4 +69,24 @@ TEST_CASE("getFileName", "[getFileName][utility][fast]") { CHECK(getFileName(str) == "asd_asd.txt"); str = "asd asd.txt"; CHECK(getFileName(str) == "asd asd.txt"); +} + +TEST_CASE("createDirectories", "[createDirectories][utility][fast]") { + std::string name = "create_directories_test"; + CHECK_NOTHROW(createDirectories(name)); + CHECK(isDir(name)); + + std::string name2 = "create_directories_test2/test2/"; + CHECK_NOTHROW(createDirectories(name2)); + CHECK(isDir(name2)); + + std::string name3 = "create_directories_test3/test3/test3/"; + CHECK_NOTHROW(createDirectories(name3)); + CHECK(isDir(name3)); + +#ifdef WIN32 + std::string name4 = "create_directories_test4\\test4\\"; + CHECK_NOTHROW(createDirectories(name4)); + CHECK(isDir(name4)); +#endif } \ No newline at end of file diff --git a/source/FAST/Utility.cpp b/source/FAST/Utility.cpp index 8c8a5f66c..5440c4588 100644 --- a/source/FAST/Utility.cpp +++ b/source/FAST/Utility.cpp @@ -905,14 +905,10 @@ void createDirectories(std::string path) { } directories = filteredDirectories; -#ifdef _WIN32 - std::string currentPath = directories[0]; -#else - std::string currentPath = "/" + directories[0]; -#endif + std::string currentPath = ""; // Create each directory needed - for(int i = 1; i < directories.size(); ++i) { - currentPath += "/" + directories[i]; + for(int i = 0; i < directories.size(); ++i) { + currentPath += directories[i] + "/"; try { createDirectory(currentPath); } catch(ExistException &e) { From ab02508475647bb0fe6e350d86573e3d19c3cc1c Mon Sep 17 00:00:00 2001 From: Erik Smistad Date: Fri, 8 Dec 2023 11:06:30 +0100 Subject: [PATCH 3/4] Linux fix for previous commit --- source/FAST/Utility.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/source/FAST/Utility.cpp b/source/FAST/Utility.cpp index 5440c4588..c184727a8 100644 --- a/source/FAST/Utility.cpp +++ b/source/FAST/Utility.cpp @@ -888,6 +888,8 @@ void createDirectory(std::string path) { void createDirectories(std::string path) { + if(path.empty()) + return; // Replace \ with / so that this will work on windows path = replace(path, "\\", "/"); std::vector directories = split(path, "/"); @@ -906,6 +908,11 @@ void createDirectories(std::string path) { directories = filteredDirectories; std::string currentPath = ""; +#ifdef WIN32 +#else + if(path[0] == "/") + currentPath = "/"; +#endif // Create each directory needed for(int i = 0; i < directories.size(); ++i) { currentPath += directories[i] + "/"; From 02aa851d407770163a0b3234a420299a0efce62d Mon Sep 17 00:00:00 2001 From: Erik Smistad Date: Fri, 8 Dec 2023 11:14:12 +0100 Subject: [PATCH 4/4] Fix compile error in previous commit --- source/FAST/Utility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/FAST/Utility.cpp b/source/FAST/Utility.cpp index c184727a8..028e1156f 100644 --- a/source/FAST/Utility.cpp +++ b/source/FAST/Utility.cpp @@ -910,7 +910,7 @@ void createDirectories(std::string path) { std::string currentPath = ""; #ifdef WIN32 #else - if(path[0] == "/") + if(path[0] == '/') currentPath = "/"; #endif // Create each directory needed