filesystem.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "sys.hpp" // for libcwd
  2. #include "debug.hpp" // for libcwd
  3. #include <sys/stat.h>
  4. #include <iostream>
  5. #include "stringutils.h"
  6. #include "filesystem.hpp"
  7. using namespace std;
  8. int fexist(const char *filename )
  9. {
  10. Dout(dc::utils, "fexist(const char *filename )");
  11. Dout(dc::utils, "filename=" << filename);
  12. struct stat buffer ;
  13. if ( stat( filename, &buffer ) == 0) return 1 ;
  14. return 0 ;
  15. }
  16. int FileSize(const char *filename )
  17. {
  18. struct stat buffer ;
  19. if ( stat( filename, &buffer ) != 0) {
  20. return 0 ;
  21. } else {
  22. return buffer.st_size;
  23. }
  24. }
  25. /** @brief create directory recursively
  26. *
  27. * usage: see MakeDirRecursively(const char *dirname)
  28. *
  29. *
  30. * @param Directory vector of hierarchy of subdirectories
  31. * @param true if absolute path, false if relative path
  32. * @return
  33. */
  34. bool MakeDirRecursively(vector<string> Directory, bool AbsolutePath)
  35. {
  36. string dirstring;
  37. if (AbsolutePath) {
  38. dirstring += "/";
  39. }
  40. for (vector<string>::iterator it=Directory.begin(); it!= Directory.end();++it) {
  41. dirstring += *it;
  42. dirstring += "/";
  43. }
  44. // cout << "Dirstring = "<< dirstring << "\n";
  45. if (mkdir(dirstring.c_str(),16877) != -1) {
  46. return true;
  47. } else {
  48. if (Directory.size() > 1) {
  49. vector<string> ParentDir(Directory);
  50. ParentDir.pop_back();
  51. if (MakeDirRecursively(ParentDir, AbsolutePath)) {
  52. return (mkdir(dirstring.c_str(),16877) != -1);
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. /** @brief create a directory. If parent directory doesn't exist, this will be created.
  59. *
  60. * @param dirname Name of directory to be created
  61. * @return
  62. */
  63. bool MakeDirRecursively(const char *dirname)
  64. {
  65. vector<string> Directory;
  66. bool AbsolutePath = ((dirname[0] == '/') || (dirname[0] == '\\'));
  67. Tokenize(dirname, Directory, "/\\");
  68. return MakeDirRecursively(Directory, AbsolutePath);
  69. }