2014-10-10 20:17:30 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Directory.cpp Copyright (c) Ladislav Zezula 2014 */
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
/* System-dependent directory functions for CascLib */
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
/* Date Ver Who Comment */
|
|
|
|
|
/* -------- ---- --- ------- */
|
|
|
|
|
/* 29.04.14 1.00 Lad The first version of Directory.cpp */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#define __CASCLIB_SELF__
|
|
|
|
|
#include "../CascLib.h"
|
|
|
|
|
#include "../CascCommon.h"
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Public functions
|
|
|
|
|
|
2020-05-07 14:39:40 +02:00
|
|
|
bool DirectoryExists(LPCTSTR szDirectory)
|
2014-10-10 20:17:30 +02:00
|
|
|
{
|
2021-10-01 13:47:03 +02:00
|
|
|
#ifdef CASCLIB_PLATFORM_WINDOWS
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
DWORD dwAttributes = GetFileAttributes(szDirectory);
|
|
|
|
|
if((dwAttributes != INVALID_FILE_ATTRIBUTES) && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-10-01 13:47:03 +02:00
|
|
|
#else // CASCLIB_PLATFORM_WINDOWS
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
DIR * dir = opendir(szDirectory);
|
2016-04-03 10:21:30 +02:00
|
|
|
|
2014-10-10 20:17:30 +02:00
|
|
|
if(dir != NULL)
|
|
|
|
|
{
|
|
|
|
|
closedir(dir);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 14:39:40 +02:00
|
|
|
bool MakeDirectory(LPCTSTR szDirectory)
|
2019-06-06 16:48:21 +02:00
|
|
|
{
|
2021-10-01 13:47:03 +02:00
|
|
|
#ifdef CASCLIB_PLATFORM_WINDOWS
|
2019-06-06 16:48:21 +02:00
|
|
|
|
|
|
|
|
BOOL bResult = CreateDirectory(szDirectory, NULL);
|
|
|
|
|
return (bResult) ? true : false;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
return (mkdir(szDirectory, 0755) == 0);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-10 20:17:30 +02:00
|
|
|
int ScanIndexDirectory(
|
2019-10-29 19:01:39 +01:00
|
|
|
LPCTSTR szIndexPath,
|
2014-10-10 20:17:30 +02:00
|
|
|
INDEX_FILE_FOUND pfnOnFileFound,
|
|
|
|
|
void * pvContext)
|
|
|
|
|
{
|
2021-10-01 13:47:03 +02:00
|
|
|
#ifdef CASCLIB_PLATFORM_WINDOWS
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
WIN32_FIND_DATA wf;
|
|
|
|
|
HANDLE hFind;
|
2021-06-27 20:20:51 +02:00
|
|
|
TCHAR szSearchMask[MAX_PATH];
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
// Prepare the search mask
|
2021-06-27 20:20:51 +02:00
|
|
|
CombinePath(szSearchMask, _countof(szSearchMask), szIndexPath, _T("*"), NULL);
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
// Prepare directory search
|
|
|
|
|
hFind = FindFirstFile(szSearchMask, &wf);
|
|
|
|
|
if(hFind != INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
// Skip the first file as it's always just "." or ".."
|
|
|
|
|
while(FindNextFile(hFind, &wf))
|
|
|
|
|
{
|
|
|
|
|
// If the found object is a file, pass it to the handler
|
|
|
|
|
if(!(wf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
|
{
|
|
|
|
|
// Let the callback scan the file name
|
2019-10-29 19:01:39 +01:00
|
|
|
pfnOnFileFound(wf.cFileName, pvContext);
|
2014-10-10 20:17:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-03 10:21:30 +02:00
|
|
|
|
2014-10-10 20:17:30 +02:00
|
|
|
// Close the search handle
|
|
|
|
|
FindClose(hFind);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 13:47:03 +02:00
|
|
|
#else // CASCLIB_PLATFORM_WINDOWS
|
2014-10-10 20:17:30 +02:00
|
|
|
|
|
|
|
|
struct dirent * dir_entry;
|
|
|
|
|
DIR * dir;
|
2016-04-03 10:21:30 +02:00
|
|
|
|
2014-10-10 20:17:30 +02:00
|
|
|
dir = opendir(szIndexPath);
|
|
|
|
|
if(dir != NULL)
|
|
|
|
|
{
|
|
|
|
|
while((dir_entry = readdir(dir)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(dir_entry->d_type != DT_DIR)
|
|
|
|
|
{
|
2019-10-29 19:01:39 +01:00
|
|
|
pfnOnFileFound(dir_entry->d_name, pvContext);
|
2016-04-03 10:21:30 +02:00
|
|
|
}
|
2014-10-10 20:17:30 +02:00
|
|
|
}
|
2016-04-03 10:21:30 +02:00
|
|
|
|
2014-10-10 20:17:30 +02:00
|
|
|
closedir(dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
}
|