Ver código fonte

Added more php-like file permissions/existence checkers. And an example which tests them against their php cousins.

Former-commit-id: d54442de083649152e494353377b46022ec3c53c
jalec 13 anos atrás
pai
commit
8fc2d636da

+ 22 - 0
examples/path_tests/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

+ 20 - 0
examples/path_tests/README

@@ -0,0 +1,20 @@
+This is a simple example program that shows how to use the is_dir, is_file,
+file_exists, is_readable, is_writable, functions of the igl library
+
+
+To Build:
+  make
+
+To Run:
+  ./example [path_1] [path_2] ... [path_n]
+
+Example Run #1:
+  Issuing:
+    touch not-writable
+    touch not-readable
+    chmod u-r not-readable 
+    chmod u-w not-writable 
+    ./example       / . .. example.cpp non-existant-file not-readable not-writable > output
+    php example.php / . .. example.cpp non-existant-file not-readable not-writable > php-output
+    diff php-output output
+  should produce no differences

+ 26 - 0
examples/path_tests/example.cpp

@@ -0,0 +1,26 @@
+#include "file_exists.h"
+#include "is_dir.h"
+#include "is_file.h"
+#include "is_readable.h"
+#include "is_writable.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("file_exists(%s) --> %s\n",argv[i],(file_exists(argv[i])?"TRUE":"FALSE"));
+    printf("is_dir(%s) --> %s\n",argv[i],(is_dir(argv[i])?"TRUE":"FALSE"));
+    printf("is_file(%s) --> %s\n",argv[i],(is_file(argv[i])?"TRUE":"FALSE"));
+    printf("is_readable(%s) --> %s\n",argv[i],(is_readable(argv[i])?"TRUE":"FALSE"));
+    printf("is_writable(%s) --> %s\n",argv[i],(is_writable(argv[i])?"TRUE":"FALSE"));
+  }
+  return 0;
+}

+ 15 - 0
examples/path_tests/example.php

@@ -0,0 +1,15 @@
+<?php
+  if($argc <= 1)
+  {
+    printf("USAGE:\n  php example.php [path_1] [path_2] ... [path_n]\n");
+    return 1;
+  }
+  for($i = 1; $i < $argc; $i++)
+  {
+    printf("file_exists(%s) --> %s\n",$argv[$i],(file_exists($argv[$i])?"TRUE":"FALSE"));
+    printf("is_dir(%s) --> %s\n",$argv[$i],(is_dir($argv[$i])?"TRUE":"FALSE"));
+    printf("is_file(%s) --> %s\n",$argv[$i],(is_file($argv[$i])?"TRUE":"FALSE"));
+    printf("is_readable(%s) --> %s\n",$argv[$i],(is_readable($argv[$i])?"TRUE":"FALSE"));
+    printf("is_writable(%s) --> %s\n",$argv[$i],(is_writable($argv[$i])?"TRUE":"FALSE"));
+  }
+?>

+ 23 - 0
file_exists.h

@@ -0,0 +1,23 @@
+#ifndef IGL_FILE_EXISTS_H
+#define IGL_FILE_EXISTS_H
+namespace igl
+{
+  // Check if a file or directory exists like PHP's file_exists function:
+  // http://php.net/manual/en/function.file-exists.php
+  // Input:
+  //   filename  path to file
+  // Returns true if file exists and is readable and false if file doesn't
+  // exist or *is not readable*
+  inline bool file_exists(const char * filename);
+}
+
+
+// Implementation
+#include <sys/stat.h>
+
+inline bool igl::file_exists(const char* filename)
+{
+  struct stat status;
+  return (stat(filename,&status)==0);
+}
+#endif

+ 5 - 2
is_dir.h

@@ -1,3 +1,5 @@
+#ifndef IGL_IS_DIR_H
+#define IGL_IS_DIR_H
 namespace igl
 {
   // Act like php's is_dir function
@@ -8,13 +10,13 @@ namespace igl
   //     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);
+  inline bool is_dir(const char * filename);
 
 }
 
 // Implementation
 #include <sys/stat.h>
-bool igl::is_dir(const char * filename)
+inline bool igl::is_dir(const char * filename)
 {
   struct stat status;
   if(stat(filename,&status)!=0)
@@ -26,3 +28,4 @@ bool igl::is_dir(const char * filename)
   return S_ISDIR(status.st_mode);
 }
 
+#endif

+ 31 - 0
is_file.h

@@ -0,0 +1,31 @@
+#ifndef IGL_IS_FILE_H
+#define IGL_IS_FILE_H
+namespace igl
+{
+  // Act like php's is_file function
+  // http://php.net/manual/en/function.is-file.php
+  // Tells whether the given filename is a regular file.
+  // 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 regular file, FALSE
+  // otherwise.
+  inline bool is_file(const char * filename);
+
+}
+
+// Implementation
+#include <sys/stat.h>
+inline bool igl::is_file(const char * filename)
+{
+  struct stat status;
+  if(stat(filename,&status)!=0)
+  {
+    // path does not exist
+    return false;
+  }
+  // Tests whether existing path is a regular file
+  return S_ISREG(status.st_mode);
+}
+
+#endif

+ 47 - 0
is_readable.h

@@ -0,0 +1,47 @@
+#ifndef IGL_IS_READABLE_H
+#define IGL_IS_READABLE_H
+namespace igl
+{
+  // Check if a file is reabable like PHP's is_readable function:
+  // http://www.php.net/manual/en/function.is-readable.php
+  // Input:
+  //   filename  path to file
+  // Returns true if file exists and is readable and false if file doesn't
+  // exist or *is not readable*
+  inline bool is_readable(const char * filename);
+}
+
+
+// Implementation
+#include <sys/stat.h>
+#include <unistd.h>
+
+inline bool igl::is_readable(const char* filename)
+{
+  // Check if file already exists
+  struct stat status;
+  if(stat(filename,&status)!=0)
+  {
+    return false;
+  }
+
+  // Get current users uid and gid
+  uid_t this_uid = getuid();
+  gid_t this_gid = getgid();
+
+  // Dealing with owner
+  if( this_uid == status.st_uid )
+  {
+    return S_IRUSR & status.st_mode;
+  }
+
+  // Dealing with group member
+  if( this_gid == status.st_gid )
+  {
+    return S_IRGRP & status.st_mode;
+  }
+
+  // Dealing with other
+  return S_IROTH & status.st_mode;
+}
+#endif

+ 47 - 0
is_writable.h

@@ -0,0 +1,47 @@
+#ifndef IGL_IS_WRITABLE_H
+#define IGL_IS_WRITABLE_H
+namespace igl
+{
+  // Check if a file exists *and* is writable like PHP's is_writable function:
+  // http://www.php.net/manual/en/function.is-writable.php
+  // Input:
+  //   filename  path to file
+  // Returns true if file exists and is writable and false if file doesn't
+  // exist or *is not writable*
+  inline bool is_writable(const char * filename);
+}
+
+
+// Implementation
+#include <sys/stat.h>
+#include <unistd.h>
+
+inline bool igl::is_writable(const char* filename)
+{
+  // Check if file already exists
+  struct stat status;
+  if(stat(filename,&status)!=0)
+  {
+    return false;
+  }
+
+  // Get current users uid and gid
+  uid_t this_uid = getuid();
+  gid_t this_gid = getgid();
+
+  // Dealing with owner
+  if( this_uid == status.st_uid )
+  {
+    return S_IWUSR & status.st_mode;
+  }
+
+  // Dealing with group member
+  if( this_gid == status.st_gid )
+  {
+    return S_IWGRP & status.st_mode;
+  }
+
+  // Dealing with other
+  return S_IWOTH & status.st_mode;
+}
+#endif