is_dir.h 936 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef IGL_IS_DIR_H
  2. #define IGL_IS_DIR_H
  3. namespace igl
  4. {
  5. // Act like php's is_dir function
  6. // http://php.net/manual/en/function.is-dir.php
  7. // Tells whether the given filename is a directory.
  8. // Input:
  9. // filename Path to the file. If filename is a relative filename, it will
  10. // be checked relative to the current working directory.
  11. // Returns TRUE if the filename exists and is a directory, FALSE
  12. // otherwise.
  13. inline bool is_dir(const char * filename);
  14. }
  15. // Implementation
  16. #include <sys/stat.h>
  17. #ifndef S_ISDIR
  18. #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  19. #endif
  20. #ifndef S_ISREG
  21. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  22. #endif
  23. inline bool igl::is_dir(const char * filename)
  24. {
  25. struct stat status;
  26. if(stat(filename,&status)!=0)
  27. {
  28. // path does not exist
  29. return false;
  30. }
  31. // Tests whether existing path is a directory
  32. return S_ISDIR(status.st_mode);
  33. }
  34. #endif