Skip to content

Filedir checks #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2075,8 +2075,13 @@ class App {
}
if(!config_name_.empty()) {
try {
std::vector<ConfigItem> values = config_formatter_->from_file(config_name_);
_parse_config(values);
auto path_result = detail::check_path(config_name_.c_str());
if(path_result == detail::path_type::file) {
std::vector<ConfigItem> values = config_formatter_->from_file(config_name_);
_parse_config(values);
} else if(config_required_) {
throw FileError::Missing(config_name_);
}
} catch(const FileError &) {
if(config_required_)
throw;
Expand Down
84 changes: 66 additions & 18 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@
#include <memory>
#include <string>

// [CLI11:verbatim]
#if(defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1)
#define CLI11_CPP17
#endif

// C standard library
// Only needed for existence checking
// Could be swapped for filesystem in C++17
#if defined CLI11_CPP17 && defined __has_include && !defined CLI11_HAS_FILESYSTEM
#if __has_include(<filesystem>)
#define CLI11_HAS_FILESYSTEM 1
#endif
#endif

#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
#include <filesystem>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif

// [CLI11:verbatim]

namespace CLI {

Expand Down Expand Up @@ -250,18 +266,54 @@ class CustomValidator : public Validator {
// Therefore, this is in detail.
namespace detail {

/// CLI enumeration of different file types
enum class path_type { nonexistant, file, directory };

#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
/// get the type of the path from a file name
inline path_type check_path(const char *file) {
try {
auto stat = std::filesystem::status(file);
switch(stat.type()) {
case std::filesystem::file_type::none:
case std::filesystem::file_type::not_found:
return path_type::nonexistant;
case std::filesystem::file_type::directory:
return path_type::directory;
default:
return path_type::file;
}
} catch(const std::filesystem::filesystem_error &) {
return path_type::nonexistant;
}
}
#else
/// get the type of the path from a file name
inline path_type check_path(const char *file) {
#if defined(_MSC_VER)
struct __stat64 buffer;
if(_stat64(file, &buffer) == 0) {
return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
}
#else
struct stat buffer;
if(stat(file, &buffer) == 0) {
return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
}
#endif
return path_type::nonexistant;
}
#endif
/// Check for an existing file (returns error message if check fails)
class ExistingFileValidator : public Validator {
public:
ExistingFileValidator() : Validator("FILE") {
func_ = [](std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
bool is_dir = (buffer.st_mode & S_IFDIR) != 0;
if(!exist) {
auto path_result = check_path(filename.c_str());
if(path_result == path_type::nonexistant) {
return "File does not exist: " + filename;
}
if(is_dir) {
if(path_result == path_type::directory) {
return "File is actually a directory: " + filename;
}
return std::string();
Expand All @@ -274,13 +326,11 @@ class ExistingDirectoryValidator : public Validator {
public:
ExistingDirectoryValidator() : Validator("DIR") {
func_ = [](std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
bool is_dir = (buffer.st_mode & S_IFDIR) != 0;
if(!exist) {
auto path_result = check_path(filename.c_str());
if(path_result == path_type::nonexistant) {
return "Directory does not exist: " + filename;
}
if(!is_dir) {
if(path_result == path_type::file) {
return "Directory is actually a file: " + filename;
}
return std::string();
Expand All @@ -293,9 +343,8 @@ class ExistingPathValidator : public Validator {
public:
ExistingPathValidator() : Validator("PATH(existing)") {
func_ = [](std::string &filename) {
struct stat buffer;
bool const exist = stat(filename.c_str(), &buffer) == 0;
if(!exist) {
auto path_result = check_path(filename.c_str());
if(path_result == path_type::nonexistant) {
return "Path does not exist: " + filename;
}
return std::string();
Expand All @@ -308,9 +357,8 @@ class NonexistentPathValidator : public Validator {
public:
NonexistentPathValidator() : Validator("PATH(non-existing)") {
func_ = [](std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
if(exist) {
auto path_result = check_path(filename.c_str());
if(path_result != path_type::nonexistant) {
return "Path already exists: " + filename;
}
return std::string();
Expand Down Expand Up @@ -1017,7 +1065,7 @@ inline std::pair<std::string, std::string> split_program_name(std::string comman
std::pair<std::string, std::string> vals;
trim(commandline);
auto esp = commandline.find_first_of(' ', 1);
while(!ExistingFile(commandline.substr(0, esp)).empty()) {
while(detail::check_path(commandline.substr(0, esp).c_str()) != path_type::file) {
esp = commandline.find_first_of(' ', esp + 1);
if(esp == std::string::npos) {
// if we have reached the end and haven't found a valid file just assume the first argument is the
Expand Down
4 changes: 4 additions & 0 deletions tests/IniTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ TEST(StringBased, SpacesSections) {
EXPECT_EQ("four", output.at(1).inputs.at(0));
}

TEST(StringBased, file_error) {
EXPECT_THROW(std::vector<CLI::ConfigItem> output = CLI::ConfigINI().from_file("nonexist_file"), CLI::FileError);
}

TEST_F(TApp, IniNotRequired) {

TempFile tmpini{"TestIniTmp.ini"};
Expand Down