Make file find utils cross-platform

See #17
This commit is contained in:
Alexander Batalov 2022-05-28 14:01:52 +03:00
parent 451e73293b
commit 6ae1afc57c
4 changed files with 45 additions and 51 deletions

View File

@ -1,14 +1,16 @@
#include "file_find.h"
#include <stddef.h>
// 0x4E6380
bool fileFindFirst(const char* path, DirectoryFileFindData* findData)
{
#if defined(_MSC_VER)
#if defined(_WIN32)
findData->hFind = FindFirstFileA(path, &(findData->ffd));
if (findData->hFind == INVALID_HANDLE_VALUE) {
return false;
}
#elif defined(__WATCOMC__)
#else
findData->dir = opendir(path);
if (findData->dir == NULL) {
return false;
@ -19,8 +21,6 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData)
closedir(findData->dir);
return false;
}
#else
#error Not implemented
#endif
return true;
@ -29,18 +29,16 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData)
// 0x4E63A8
bool fileFindNext(DirectoryFileFindData* findData)
{
#if defined(_MSC_VER)
#if defined(_WIN32)
if (!FindNextFileA(findData->hFind, &(findData->ffd))) {
return false;
}
#elif defined(__WATCOMC__)
#else
findData->entry = readdir(findData->dir);
if (findData->entry == NULL) {
closedir(findData->dir);
return false;
}
#else
#error Not implemented
#endif
return true;
@ -51,12 +49,10 @@ bool findFindClose(DirectoryFileFindData* findData)
{
#if defined(_MSC_VER)
FindClose(findData->hFind);
#elif defined(__WATCOMC__)
#else
if (closedir(findData->dir) != 0) {
return false;
}
#else
#error Not implemented
#endif
return true;

View File

@ -1,8 +1,12 @@
#ifndef FILE_FIND_H
#define FILE_FIND_H
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <dirent.h>
#endif
// NOTE: This structure is significantly different from what was in the
// original code. Watcom provides opendir/readdir/closedir implementations,
@ -24,14 +28,12 @@
// original implementation for Watcom (not tested). I'm not sure it will work
// in other compilers, so for now just stick with the error.
typedef struct DirectoryFileFindData {
#if defined(_MSC_VER)
#if defined(_WIN32)
HANDLE hFind;
WIN32_FIND_DATAA ffd;
#elif defined(__WATCOMC__)
#else
DIR* dir;
struct dirent* entry;
#else
#error Not implemented
#endif
} DirectoryFileFindData;
@ -39,4 +41,24 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData);
bool fileFindNext(DirectoryFileFindData* findData);
bool findFindClose(DirectoryFileFindData* findData);
static inline bool fileFindIsDirectory(DirectoryFileFindData* findData)
{
#if defined(_WIN32)
return (findData->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#elif defined(__WATCOMC__)
return (findData->entry->d_attr & _A_SUBDIR) != 0;
#else
return findData->entry->d_type == DT_DIR;
#endif
}
static inline char* fileFindGetName(DirectoryFileFindData* findData)
{
#if defined(_WIN32)
return findData->ffd.cFileName;
#else
return findData->entry->d_name;
#endif
}
#endif /* FILE_FIND_H */

View File

@ -549,18 +549,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList
if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') {
if (fileFindFirst(pattern, &directoryFileFindData)) {
do {
bool isDirectory;
char* entryName;
#if defined(_MSC_VER)
isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
entryName = directoryFileFindData.ffd.cFileName;
#elif defined(__WATCOMC__)
isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0;
entryName = directoryFileFindData.entry->d_name;
#else
#error Not implemented
#endif
bool isDirectory = fileFindIsDirectory(&directoryFileFindData);
char* entryName = fileFindGetName(&directoryFileFindData);
if (isDirectory) {
if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) {
@ -604,18 +594,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList
if (fileFindFirst(path, &directoryFileFindData)) {
do {
bool isDirectory;
char* entryName;
#if defined(_MSC_VER)
isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
entryName = directoryFileFindData.ffd.cFileName;
#elif defined(__WATCOMC__)
isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0;
entryName = directoryFileFindData.entry->d_name;
#else
#error Not implemented
#endif
bool isDirectory = fileFindIsDirectory(&directoryFileFindData);
char* entryName = fileFindGetName(&directoryFileFindData);
if (isDirectory) {
if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) {
@ -642,18 +622,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList
_splitpath(pattern, drive, dir, fileName, extension);
if (fileFindFirst(pattern, &directoryFileFindData)) {
do {
bool isDirectory;
char* entryName;
#if defined(_MSC_VER)
isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
entryName = directoryFileFindData.ffd.cFileName;
#elif defined(__WATCOMC__)
isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0;
entryName = directoryFileFindData.entry->d_name;
#else
#error Not implemented
#endif
bool isDirectory = fileFindIsDirectory(&directoryFileFindData);
char* entryName = fileFindGetName(&directoryFileFindData);
if (isDirectory) {
if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) {

View File

@ -20,5 +20,11 @@ add_library(fpattern STATIC
"${fpattern_SOURCE_DIR}/fpattern.h"
)
if(APPLE)
target_compile_definitions(fpattern PRIVATE
"-Dunix"
)
endif()
set(FPATTERN_LIBRARY "fpattern" PARENT_SCOPE)
set(FPATTERN_INCLUDE_DIR "${fpattern_SOURCE_DIR}" PARENT_SCOPE)