std::vector<std::string> getFilesOnDirectory(const std::string& directoryPath, const std::vector<std::string>& extensions) { try { // Check folder exits if (!existDir(directoryPath)) error("Folder " + directoryPath + " does not exist.", __LINE__, __FUNCTION__, __FILE__); // Read images std::vector<std::string> filePaths; for (const auto& file : boost::make_iterator_range(boost::filesystem::directory_iterator{directoryPath}, {})) if (!boost::filesystem::is_directory(file.status())) // Skip directories filePaths.emplace_back(file.path().string()); // Check #files > 0 if (filePaths.empty()) error("No files were found on " + directoryPath, __LINE__, __FUNCTION__, __FILE__); // If specific extensions specified if (!extensions.empty()) { // Read images std::vector<std::string> specificExtensionPaths; specificExtensionPaths.reserve(filePaths.size()); for (const auto& filePath : filePaths) if (extensionIsDesired(getFileExtension(filePath), extensions)) specificExtensionPaths.emplace_back(filePath); std::swap(filePaths, specificExtensionPaths); } // Sort alphabetically std::sort(filePaths.begin(), filePaths.end()); return filePaths; } catch (const std::exception& e) { error(e.what(), __LINE__, __FUNCTION__, __FILE__); return {}; } }
std::vector<std::string> getFilesOnDirectory(const std::string& directoryPath, const std::vector<std::string>& extensions) { try { // Format the path first const auto formatedPath = formatAsDirectory(directoryPath); // Check folder exits if (!existDirectory(formatedPath)) error("Folder " + formatedPath + " does not exist.", __LINE__, __FUNCTION__, __FILE__); // Read all files in folder std::vector<std::string> filePaths; #ifdef _WIN32 auto formatedPathWindows = formatedPath; formatedPathWindows.append("\\*"); WIN32_FIND_DATA data; HANDLE hFind; if ((hFind = FindFirstFile(formatedPathWindows.c_str(), &data)) != INVALID_HANDLE_VALUE) { do filePaths.emplace_back(formatedPath + data.cFileName); while (FindNextFile(hFind, &data) != 0); FindClose(hFind); } #elif defined __unix__ std::shared_ptr<DIR> directoryPtr( opendir(formatedPath.c_str()), [](DIR* formatedPath){ formatedPath && closedir(formatedPath); } ); struct dirent* direntPtr; while ((direntPtr = readdir(directoryPtr.get())) != nullptr) { std::string currentPath = formatedPath + direntPtr->d_name; if ((strncmp(direntPtr->d_name, ".", 1) == 0) || existDirectory(currentPath)) continue; filePaths.emplace_back(currentPath); } #else #error Unknown environment! #endif // Check #files > 0 if (filePaths.empty()) error("No files were found on " + formatedPath, __LINE__, __FUNCTION__, __FILE__); // If specific extensions specified if (!extensions.empty()) { // Read images std::vector<std::string> specificExtensionPaths; specificExtensionPaths.reserve(filePaths.size()); for (const auto& filePath : filePaths) if (extensionIsDesired(getFileExtension(filePath), extensions)) specificExtensionPaths.emplace_back(filePath); std::swap(filePaths, specificExtensionPaths); } // Sort alphabetically std::sort(filePaths.begin(), filePaths.end()); // Return result return filePaths; } catch (const std::exception& e) { error(e.what(), __LINE__, __FUNCTION__, __FILE__); return {}; } }