闲暇时间,整理下自动创建文件目录的相关流程
bool Inner_IsExistsPath(const char* path)
{
DWORD fa = GetFileAttributes(path);
return (fa != (DWORD)-1);
}
bool Inner_CreatePath(const char* path)
{
return ::CreateDirectory(path, NULL);
}
bool CreateFullPath(const char* fullPath)
{
if (fullPath == NULL || fullPath[0] == 0)
{
return false;
}
int pos = -1;
for (int i = strlen(fullPath) - 1; i >= 0; i--)
{
if (fullPath[i] == '\\' || fullPath[i] == '/')
{
pos = i;
break;
}
}
if (pos == -1)
{
return false;
}
char strPath[MAX_PATH] = { 0 };
strncpy(strPath, fullPath, pos);
strPath[pos + 1] = '\0';
if (Inner_IsExistsPath(strPath))
{
return true;
}
vector<string> vDir;
char * p = strPath;
bool bFind = false;
while (strlen(p) > 0)
{
bFind = false;
for (int i = 0; i < strlen(p); i++)
{
if (p[i] == '\\' || p[i] == '/')
{
p[i] = '\0';
vDir.push_back(p);
p += i + 1;
bFind = true;
break;
}
}
if (!bFind)
{
vDir.push_back(p);
break;
}
}
if (vDir.empty())
{
return false;
}
string currPath = "";
for (int i = 0; i < vDir.size(); i++)
{
if (i == 0)
{
currPath = vDir[i];
}
else
{
currPath += "/" + vDir[i];
}
if (Inner_IsExistsPath(currPath.c_str()))
{
continue;
}
if (!Inner_CreatePath(currPath.c_str()))
{
return false;
}
}
return true;
}
bool Inner_IsExistsPath(const char* path)
{
DWORD fa = GetFileAttributes(path);
return (fa != (DWORD)-1);
}
bool Inner_CreatePath(const char* path)
{
return ::CreateDirectory(path, NULL);
}
bool CreateFullPath(const char* fullPath)
{
if (fullPath == NULL || fullPath[0] == 0)
{
return false;
}
int pos = -1;
for (int i = strlen(fullPath) - 1; i >= 0; i--)
{
if (fullPath[i] == '\\' || fullPath[i] == '/')
{
pos = i;
break;
}
}
if (pos == -1)
{
return false;
}
char strPath[MAX_PATH] = { 0 };
strncpy(strPath, fullPath, pos);
strPath[pos + 1] = '\0';
if (Inner_IsExistsPath(strPath))
{
return true;
}
vector<string> vDir;
char * p = strPath;
bool bFind = false;
while (strlen(p) > 0)
{
bFind = false;
for (int i = 0; i < strlen(p); i++)
{
if (p[i] == '\\' || p[i] == '/')
{
p[i] = '\0';
vDir.push_back(p);
p += i + 1;
bFind = true;
break;
}
}
if (!bFind)
{
vDir.push_back(p);
break;
}
}
if (vDir.empty())
{
return false;
}
string currPath = "";
for (int i = 0; i < vDir.size(); i++)
{
if (i == 0)
{
currPath = vDir[i];
}
else
{
currPath += "/" + vDir[i];
}
if (Inner_IsExistsPath(currPath.c_str()))
{
continue;
}
if (!Inner_CreatePath(currPath.c_str()))
{
return false;
}
}
return true;
}