add MakeSureDirectoryPathExistsExA/W

svn path=/trunk/; revision=12945
This commit is contained in:
Gunnar Dalsnes 2005-01-12 07:51:11 +00:00
parent 73c479f923
commit e620ebee7f
3 changed files with 123 additions and 1 deletions

View file

@ -0,0 +1,21 @@
/*
*/
#ifndef ROSRTL_PATH_H__
#define ROSRTL_PATH_H__
#ifdef __cplusplus
extern "C"
{
#endif
BOOL STDCALL MakeSureDirectoryPathExistsExA(LPCSTR DirPath, BOOL FileAtEnd);
BOOL STDCALL MakeSureDirectoryPathExistsExW(LPCWSTR DirPath, BOOL FileAtEnd);
#ifdef __cplusplus
}
#endif
#endif
/* EOF */

View file

@ -0,0 +1,100 @@
#include <windows.h>
#include <string.h>
/***********************************************************************
* MakeSureDirectoryPathExistsExA
*
* If a dir is at the end and the path ends with a backslash, FileAtEnd
* is ignored. If the path doesn't end with a backslash, FileAtEnd is
* used to determine if the last part of the path is a file name or a
* directory.
*
* Path may be absolute or relative to current dir.
*/
BOOL STDCALL MakeSureDirectoryPathExistsExA(LPCSTR DirPath, BOOL FileAtEnd)
{
char Path[MAX_PATH];
char *SlashPos = Path;
char Slash;
BOOL bRes;
strcpy(Path, DirPath);
while((SlashPos=strpbrk(SlashPos+1,"\\/")))
{
Slash = *SlashPos;
*SlashPos = 0;
bRes = CreateDirectoryA(Path, NULL);
if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
*SlashPos = Slash;
if (*(SlashPos+1) == 0) return TRUE;
}
if (!FileAtEnd)
{
bRes = CreateDirectoryA(Path, NULL);
if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
}
return TRUE;
}
/***********************************************************************
* MakeSureDirectoryPathExistsExW
*
* If a dir is at the end and the path ends with a backslash, FileAtEnd
* is ignored. If the path doesn't end with a backslash, FileAtEnd is
* used to determine if the last part of the path is a file name or a
* directory.
*
* Path may be absolute or relative to current dir.
*/
BOOL STDCALL MakeSureDirectoryPathExistsExW(LPCWSTR DirPath, BOOL FileAtEnd)
{
WCHAR Path[MAX_PATH];
WCHAR *SlashPos = Path;
WCHAR Slash;
BOOL bRes;
wcscpy(Path, DirPath);
while((SlashPos=wcspbrk(SlashPos+1,L"\\/")))
{
Slash = *SlashPos;
*SlashPos = 0;
bRes = CreateDirectoryW(Path, NULL);
if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
*SlashPos = Slash;
if (*(SlashPos+1) == 0) return TRUE;
}
if (!FileAtEnd)
{
bRes = CreateDirectoryW(Path, NULL);
if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
return FALSE;
}
}
return TRUE;
}

View file

@ -30,7 +30,8 @@ MISC_OBJECTS = \
misc/qsort.o
FILE_OBJECTS = \
file/sparse.o
file/sparse.o \
file/path.o
RECMUTEX_OBJECTS = recmutex/recmutex.o