123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #pragma once
- #include <string>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fstream>
- #include <stdint.h>
- #include <stdio.h>
- #include <time.h>
- class PathCreator
- {
- public:
- PathCreator() = default;
- ~PathCreator() = default;
- std::string GetCurPath() {
- return m_current_path;
- }
- bool Mkdir(std::string dirName) {
- uint32_t beginCmpPath = 0;
- uint32_t endCmpPath = 0;
- std::string fullPath = "";
- if('/' != dirName[0])
- {
- fullPath = getcwd(nullptr, 0);
- beginCmpPath = fullPath.size();
- fullPath = fullPath + "/" + dirName;
- }
- else
- {
- //Absolute path
- fullPath = dirName;
- beginCmpPath = 1;
- }
- if (fullPath[fullPath.size() - 1] != '/')
- {
- fullPath += "/";
- }
- endCmpPath = fullPath.size();
- //create dirs;
- for(uint32_t i = beginCmpPath; i < endCmpPath ; i++ )
- {
- if('/' == fullPath[i])
- {
- std::string curPath = fullPath.substr(0, i);
- if(access(curPath.c_str(), F_OK) != 0)
- {
- if(mkdir(curPath.c_str(), /*S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH*/0777) == -1)
- {
- printf("mkdir(%s) failed\n", curPath.c_str());
- return false;
- }
- }
- }
- }
- m_current_path=fullPath;
- return true;
- }
- bool CreateDatePath(std::string root, bool add_time = true) {
- time_t tt;
- time( &tt );
- tt = tt + 8*3600; // transform the time zone
- tm* t= gmtime( &tt );
- char buf[255]={0};
- if (add_time)
- {
- sprintf(buf, "%s/%d%02d%02d-%02d%02d%02d", root.c_str(),
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday,
- t->tm_hour,
- t->tm_min,
- t->tm_sec);
- }
- else
- {
- sprintf(buf, "%s/%d%02d%02d", root.c_str(),
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday);
- }
- return Mkdir(buf);
- }
- static bool IsPathExist(const std::string& path) {
- if (access(path.c_str(), 0) == F_OK) {
- return true;
- }
- return false;
- }
- static bool IsFile(const std::string& path) {
- if (!IsPathExist(path)) {
- printf("%s:%d %s not exist\n", __FILE__, __LINE__, path.c_str());
- return false;
- }
- struct stat buffer;
- return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
- }
- static bool IsFolder(const std::string& path) {
- if (!IsPathExist(path)) {
- return false;
- }
- struct stat buffer;
- return (stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
- }
- protected:
- std::string m_current_path;
- };
|