From ce669fe46bea11d379a1d40adf3a1807ce44bb32 Mon Sep 17 00:00:00 2001 From: "samuel.hayden" <samuel.haydeb@fhr.frunhofer.de> Date: Fri, 15 Mar 2024 11:29:21 +0100 Subject: [PATCH] Command line parser checks if file path directories exist Will present an error if they do not exist. This means no time is wasted generating the lattice just for it to not be able to be saved --- LatticeDehomogenization/src/ErrorCodes.h | 14 ++++++---- .../src/inputParsing/ParseCommandLine.h | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/LatticeDehomogenization/src/ErrorCodes.h b/LatticeDehomogenization/src/ErrorCodes.h index f840f01..4bde0d1 100644 --- a/LatticeDehomogenization/src/ErrorCodes.h +++ b/LatticeDehomogenization/src/ErrorCodes.h @@ -7,15 +7,17 @@ enum ErrorCodes { NoError = 0, - FileNotFound, - CellSizeNotDefined, - StrutRadiusNotDefined, - CantDetermineLatticeType, - LatticeTypeNotDefined, + FileNotFound, DirectoryNotFound, InvalidExtension, + CellSizeNotDefined, StrutRadiusNotDefined, + + CantDetermineLatticeType, LatticeTypeNotDefined, + InvalidArgument, FailedToMergeCells, FailedToTrimLattice, LatticeIsNull, BoundingBoxIsNull, + + UnknownError, }; constexpr const char* errorcode2string(const ErrorCodes code) @@ -24,6 +26,8 @@ constexpr const char* errorcode2string(const ErrorCodes code) { case NoError: return "NO_ERROR"; case FileNotFound: return "FILE_NOT_FOUND"; + case DirectoryNotFound: return "DIRECTORY_NOT_FOUND"; + case InvalidExtension: return "INVALID_FILE_EXTENSION"; case CellSizeNotDefined: return "CELL_SIZE_NOT_DEFINED"; case StrutRadiusNotDefined: return "STRUT_RADIUS_NOT_DEFINED"; case CantDetermineLatticeType: return "CANT_DETERMINE_LATTICE_TYPE"; diff --git a/LatticeDehomogenization/src/inputParsing/ParseCommandLine.h b/LatticeDehomogenization/src/inputParsing/ParseCommandLine.h index 12987c9..aa7aa52 100644 --- a/LatticeDehomogenization/src/inputParsing/ParseCommandLine.h +++ b/LatticeDehomogenization/src/inputParsing/ParseCommandLine.h @@ -2,6 +2,7 @@ #include <unordered_map> #include <unordered_set> #include <string> +#include <sys/stat.h> #include "ErrorCodes.h" @@ -21,6 +22,23 @@ private: std::vector<ErrorCodes> _errors; std::vector<std::string> _errorMsgs; + bool directoryExists(const std::string& filepath) + { + size_t directoryIdx = filepath.find_last_of('/'); + if (directoryIdx == std::string::npos) + directoryIdx = filepath.find_last_of('\\'); + + // Isnt a directory. Just a file name to be placed in current path + if (directoryIdx == std::string::npos) + return true; + + struct stat sb; + if (stat(filepath.substr(0, directoryIdx).c_str(), &sb) == 0) + return true; // Valid path + else + return false; // directory not found + } + private: static AcceptedKeys s_validKeys; @@ -44,6 +62,14 @@ public: _errorMsgs.emplace_back(key); } } + if (hasKey("input")) + { + if (!directoryExists(getValue("input"))) + { + _errors.emplace_back(ErrorCodes::DirectoryNotFound); + _errorMsgs.emplace_back(getValue("input")); + } + } } inline const std::string& getValue(const std::string& key) const -- GitLab