pathcreator.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include <string>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <fstream>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <time.h>
  10. class PathCreator
  11. {
  12. public:
  13. PathCreator() = default;
  14. ~PathCreator() = default;
  15. std::string GetCurPath() {
  16. return m_current_path;
  17. }
  18. bool Mkdir(std::string dirName) {
  19. uint32_t beginCmpPath = 0;
  20. uint32_t endCmpPath = 0;
  21. std::string fullPath = "";
  22. if('/' != dirName[0])
  23. {
  24. fullPath = getcwd(nullptr, 0);
  25. beginCmpPath = fullPath.size();
  26. fullPath = fullPath + "/" + dirName;
  27. }
  28. else
  29. {
  30. //Absolute path
  31. fullPath = dirName;
  32. beginCmpPath = 1;
  33. }
  34. if (fullPath[fullPath.size() - 1] != '/')
  35. {
  36. fullPath += "/";
  37. }
  38. endCmpPath = fullPath.size();
  39. //create dirs;
  40. for(uint32_t i = beginCmpPath; i < endCmpPath ; i++ )
  41. {
  42. if('/' == fullPath[i])
  43. {
  44. std::string curPath = fullPath.substr(0, i);
  45. if(access(curPath.c_str(), F_OK) != 0)
  46. {
  47. if(mkdir(curPath.c_str(), /*S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH*/0777) == -1)
  48. {
  49. printf("mkdir(%s) failed\n", curPath.c_str());
  50. return false;
  51. }
  52. }
  53. }
  54. }
  55. m_current_path=fullPath;
  56. return true;
  57. }
  58. bool CreateDatePath(std::string root, bool add_time = true) {
  59. time_t tt;
  60. time( &tt );
  61. tt = tt + 8*3600; // transform the time zone
  62. tm* t= gmtime( &tt );
  63. char buf[255]={0};
  64. if (add_time)
  65. {
  66. sprintf(buf, "%s/%d%02d%02d-%02d%02d%02d", root.c_str(),
  67. t->tm_year + 1900,
  68. t->tm_mon + 1,
  69. t->tm_mday,
  70. t->tm_hour,
  71. t->tm_min,
  72. t->tm_sec);
  73. }
  74. else
  75. {
  76. sprintf(buf, "%s/%d%02d%02d", root.c_str(),
  77. t->tm_year + 1900,
  78. t->tm_mon + 1,
  79. t->tm_mday);
  80. }
  81. return Mkdir(buf);
  82. }
  83. static bool IsPathExist(const std::string& path) {
  84. if (access(path.c_str(), 0) == F_OK) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. static bool IsFile(const std::string& path) {
  90. if (!IsPathExist(path)) {
  91. printf("%s:%d %s not exist\n", __FILE__, __LINE__, path.c_str());
  92. return false;
  93. }
  94. struct stat buffer;
  95. return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
  96. }
  97. static bool IsFolder(const std::string& path) {
  98. if (!IsPathExist(path)) {
  99. return false;
  100. }
  101. struct stat buffer;
  102. return (stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
  103. }
  104. protected:
  105. std::string m_current_path;
  106. };