Sfoglia il codice sorgente

Added is_dir and is_dir example

Former-commit-id: aaf1ee587c797cd22c457c19e4a6343dcf3d5ac3
jalec 14 anni fa
parent
commit
1623a76465
4 ha cambiato i file con 86 aggiunte e 0 eliminazioni
  1. 22 0
      examples/is_dir/Makefile
  2. 18 0
      examples/is_dir/README
  3. 18 0
      examples/is_dir/example.cpp
  4. 28 0
      is_dir.h

+ 22 - 0
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

+ 18 - 0
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

+ 18 - 0
examples/is_dir/example.cpp

@@ -0,0 +1,18 @@
+#include "is_dir.h"
+using namespace igl;
+#include <cstdio>
+
+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;
+}

+ 28 - 0
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 <sys/stat.h>
+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);
+}
+