From 6ae1afc57ce4425a6d1956c9ab185e850b4f306a Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 14:01:52 +0300 Subject: [PATCH] Make file find utils cross-platform See #17 --- src/file_find.cc | 18 +++++-------- src/file_find.h | 30 ++++++++++++++++++--- src/xfile.cc | 42 +++++------------------------ third_party/fpattern/CMakeLists.txt | 6 +++++ 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/src/file_find.cc b/src/file_find.cc index 517c340..59c3f13 100644 --- a/src/file_find.cc +++ b/src/file_find.cc @@ -1,14 +1,16 @@ #include "file_find.h" +#include + // 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; diff --git a/src/file_find.h b/src/file_find.h index 0dbb552..90e497e 100644 --- a/src/file_find.h +++ b/src/file_find.h @@ -1,8 +1,12 @@ #ifndef FILE_FIND_H #define FILE_FIND_H +#if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include +#else +#include +#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 */ diff --git a/src/xfile.cc b/src/xfile.cc index e4dd5c3..ed667ae 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -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) { diff --git a/third_party/fpattern/CMakeLists.txt b/third_party/fpattern/CMakeLists.txt index 43f4153..d809812 100644 --- a/third_party/fpattern/CMakeLists.txt +++ b/third_party/fpattern/CMakeLists.txt @@ -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)