Commit 960cc104dbc2c2a83fe16137d6928b8fbcb14b59

Authored by Henry Fredrick Schreiner
1 parent cf667f28

Adding error messages on failed conversion

Showing 1 changed file with 31 additions and 8 deletions
include/CLI/Validators.hpp
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 // file LICENSE or https://github.com/henryiii/CLI11 for details. 4 // file LICENSE or https://github.com/henryiii/CLI11 for details.
5 5
6 #include <string> 6 #include <string>
  7 +#include <iostream>
7 8
8 9
9 // C standard library 10 // C standard library
@@ -17,25 +18,47 @@ namespace CLI { @@ -17,25 +18,47 @@ namespace CLI {
17 18
18 /// Check for an existing file 19 /// Check for an existing file
19 bool ExistingFile(std::string filename) { 20 bool ExistingFile(std::string filename) {
20 -// std::fstream f(name.c_str());  
21 -// return f.good();  
22 -// Fastest way according to http://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c  
23 struct stat buffer; 21 struct stat buffer;
24 - return (stat(filename.c_str(), &buffer) == 0); 22 + bool exist = stat(filename.c_str(), &buffer) == 0;
  23 + bool is_dir = buffer.st_mode & S_IFDIR;
  24 + if(!exist) {
  25 + std::cerr << "File does not exist: " << filename << std::endl;
  26 + return false;
  27 + } else if (is_dir) {
  28 + std::cerr << "File is actually a directory: " << filename << std::endl;
  29 + return false;
  30 + } else {
  31 + return true;
  32 + }
25 } 33 }
26 34
27 /// Check for an existing directory 35 /// Check for an existing directory
28 bool ExistingDirectory(std::string filename) { 36 bool ExistingDirectory(std::string filename) {
29 struct stat buffer; 37 struct stat buffer;
30 - if(stat(filename.c_str(), &buffer) == 0 && (buffer.st_mode & S_IFDIR) ) 38 + bool exist = stat(filename.c_str(), &buffer) == 0;
  39 + bool is_dir = buffer.st_mode & S_IFDIR;
  40 + if(!exist) {
  41 + std::cerr << "Directory does not exist: " << filename << std::endl;
  42 + return false;
  43 + } else if (is_dir) {
  44 + return true;
  45 + } else {
  46 + std::cerr << "Directory is actually a file: " << filename << std::endl;
31 return true; 47 return true;
32 - return false; 48 + }
33 } 49 }
34 50
  51 +
35 /// Check for a non-existing path 52 /// Check for a non-existing path
36 bool NonexistentPath(std::string filename) { 53 bool NonexistentPath(std::string filename) {
37 - struct stat buffer;  
38 - return stat(filename.c_str(), &buffer) != 0; 54 + struct stat buffer;
  55 + bool exist = stat(filename.c_str(), &buffer) == 0;
  56 + if(!exist) {
  57 + return true;
  58 + } else {
  59 + std::cerr << "Path exists: " << filename << std::endl;
  60 + return false;
  61 + }
39 } 62 }
40 63
41 64