pathcreator.cpp 2.1 KB

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