diff --git a/examples/is_dir/Makefile b/examples/is_dir/Makefile new file mode 100644 index 000000000..799d5c509 --- /dev/null +++ b/examples/is_dir/Makefile @@ -0,0 +1,22 @@ + +.PHONY: all + +all: example + +.PHONY: example + +igl_lib=../../ + +CFLAGS=-g +inc=-I$(igl_lib) +lib= + +example: example.o + g++ $(CFLAGS) -o example example.o $(lib) + rm example.o + +example.o: example.cpp + g++ $(CFLAGS) -c example.cpp -o example.o $(inc) +clean: + rm -f example.o + rm -f example diff --git a/examples/is_dir/README b/examples/is_dir/README new file mode 100644 index 000000000..ecee72a50 --- /dev/null +++ b/examples/is_dir/README @@ -0,0 +1,18 @@ +This is a simple example program that shows how to use the is_dir function of +is_dir.h in the igl library. + + +To Build: + make + +To Run: + ./example [path_1] [path_2] ... [path_n] + +Example Run #1: + Issuing: + ./example . .. example.cpp non-existant-file + should produce: + is_dir(.) --> TRUE + is_dir(..) --> TRUE + is_dir(example.cpp) --> FALSE + is_dir(non-existant-file) --> FALSE diff --git a/examples/is_dir/example.cpp b/examples/is_dir/example.cpp new file mode 100644 index 000000000..e2c01886d --- /dev/null +++ b/examples/is_dir/example.cpp @@ -0,0 +1,18 @@ +#include "is_dir.h" +using namespace igl; +#include + +int main(int argc, char * argv[]) +{ + if(argc <= 1) + { + printf("USAGE:\n ./example [path_1] [path_2] ... [path_n]\n"); + return 1; + } + // loop over arguments + for(int i = 1; i < argc; i++) + { + printf("is_dir(%s) --> %s\n",argv[i],(is_dir(argv[i])?"TRUE":"FALSE")); + } + return 0; +} diff --git a/is_dir.h b/is_dir.h new file mode 100644 index 000000000..545bd5adc --- /dev/null +++ b/is_dir.h @@ -0,0 +1,28 @@ +namespace igl +{ + // Act like php's is_dir function + // http://php.net/manual/en/function.is-dir.php + // Tells whether the given filename is a directory. + // Input: + // filename Path to the file. If filename is a relative filename, it will + // be checked relative to the current working directory. + // Returns TRUE if the filename exists and is a directory, FALSE + // otherwise. + bool is_dir(const char * filename); + +} + +// Implementation +#include +bool igl::is_dir(const char * filename) +{ + struct stat status; + if(stat(filename,&status)!=0) + { + // path does not exist + return false; + } + // Tests whether existing path is a directory + return S_ISDIR(status.st_mode); +} +