#ifdef WIN32
#include 
#include 
#else
#include 
#include 
#endif
#include 
#include 
#define MAX_PATH_LEN 256
#ifdef WIN32
#define ACCESS(fileName,accessMode) _access(fileName,accessMode)
#define MKDIR(path) _mkdir(path)
#else
#define ACCESS(fileName,accessMode) access(fileName,accessMode)
#define MKDIR(path) mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
#endif
// 从左到右依次判断文件夹是否存在,不存在就创建
// example: /home/root/mkdir/1/2/3/4/
// 注意:最后一个如果是文件夹的话,需要加上 '\' 或者 '/'
int32_t createDirectory(const std::string &directoryPath)
{
uint32_t dirPathLen = directoryPath.length();
if (dirPathLen > MAX_PATH_LEN)
{
return -;
}
char tmpDirPath[MAX_PATH_LEN] = { };
for (uint32_t i = ; i < dirPathLen; ++i)
{
tmpDirPath[i] = directoryPath[i];
if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
{
if (ACCESS(tmpDirPath, ) != )
{
int32_t ret = MKDIR(tmpDirPath);
if (ret != )
{
return ret;
}
}
}
}
return ;
}
int32_t main(int32_t argc, char *argv[])
{
if (argc == )
{
return createDirectory(argv[]);
}
return ;
}