diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 87c8c5a0..230c6ebf 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -21,6 +21,10 @@ #include #include +#ifndef _WIN32 +#include +#endif + #ifdef _WIN32 #include // _get_osfhandle and _isatty support @@ -549,6 +553,108 @@ std::string SPDLOG_INLINE getenv(const char *field) #endif } +#ifdef _WIN32 +#ifdef SPDLOG_WCHAR_FILENAMES +SPDLOG_INLINE std::vector get_directory_files(const std::wstring &directory) SPDLOG_NOEXCEPT +{ + std::vector files; + + HANDLE dir; + WIN32_FIND_DATAW file_data; + + if ((dir = FindFirstFileW((directory + L"/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) + return files; + + do + { + const std::wstring file_name = file_data.cFileName; + const std::wstring full_file_name = directory + L"/" + file_name; + const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + + if (file_name[0] == '.') + continue; + + if (is_directory) + continue; + + files.push_back(full_file_name); + } while (FindNextFileW(dir, &file_data)); + + FindClose(dir); + + return files; +} +#else +SPDLOG_INLINE std::vector get_directory_files(const std::string &directory) SPDLOG_NOEXCEPT +{ + std::vector files; + + HANDLE dir; + WIN32_FIND_DATAA file_data; + + if ((dir = FindFirstFileA((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) + return files; + + do + { + const std::string file_name = file_data.cFileName; + const std::string full_file_name = directory + "/" + file_name; + const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + + if (file_name[0] == '.') + continue; + + if (is_directory) + continue; + + files.push_back(full_file_name); + } while (FindNextFileA(dir, &file_data)); + + FindClose(dir); + + return files; +} +#endif +#else +SPDLOG_INLINE std::vector get_directory_files(const std::string &directory) SPDLOG_NOEXCEPT +{ + std::vector files; + + struct dirent* ent = nullptr; + struct stat st; + + DIR *dir = opendir(directory.c_str()); + + if(dir == nullptr) + { + return files; + } + + while ((ent = readdir(dir)) != nullptr) + { + const std::string file_name = ent->d_name; + const std::string full_file_name = directory + "/" + file_name; + + if (file_name[0] == '.') + continue; + + if (::stat(full_file_name.c_str(), &st) == -1) + continue; + + const bool is_directory = (st.st_mode & S_IFDIR) != 0; + + if (is_directory) + continue; + + files.push_back(full_file_name); + } + + closedir(dir); + + return files; +} +#endif + } // namespace os } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index cd586e14..937e5b67 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -102,6 +102,12 @@ SPDLOG_API bool create_dir(filename_t path); // return empty string if field not found SPDLOG_API std::string getenv(const char *field); +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) +SPDLOG_API std::vector get_directory_files(const std::wstring &directory) SPDLOG_NOEXCEPT; +#else +SPDLOG_API std::vector get_directory_files(const std::string &directory) SPDLOG_NOEXCEPT; +#endif + } // namespace os } // namespace details } // namespace spdlog