From e918a8cb5b5fa0cb80cebfbf9b92a4eddfbff80b Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sat, 20 Aug 2022 10:55:16 +0200 Subject: [PATCH] fix compilation of svg2png on Windows --- src/bin/svg2png/svg2png.cpp | 88 ++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 21 deletions(-) diff --git a/src/bin/svg2png/svg2png.cpp b/src/bin/svg2png/svg2png.cpp index 06995454..71bc1c08 100644 --- a/src/bin/svg2png/svg2png.cpp +++ b/src/bin/svg2png/svg2png.cpp @@ -25,9 +25,17 @@ #include #include #include "lodepng.h" -#include -#include -#include +#ifdef _WIN32 + #include + #ifndef PATH_MAX + #define PATH_MAX MAX_PATH + #endif +#else + #include + #include + #include + #include +#endif using namespace std; @@ -229,11 +237,10 @@ public: for (auto path : paths) { auto real_path = realFile(path); if (real_path) { - DIR* dir = opendir(real_path); - if (dir) { + if (isDirectory(real_path)) { //load from directory cout << "Trying load from directory \"" << real_path << "\"." << endl; - if ((ret = handleDirectory(real_path, dir))) break; + if ((ret = handleDirectory(real_path))) break; } else if (svgFile(path)) { //load single file @@ -286,6 +293,25 @@ private: return path; } + bool isDirectory(const char* path) + { +#ifdef _WIN32 + DWORD attr = GetFileAttributes(path); + if (attr == INVALID_FILE_ATTRIBUTES) + return false; + + return attr & FILE_ATTRIBUTE_DIRECTORY; +#else + struct stat buf; + + if (stat(path, &buf) != 0) { + return false; + } + + return S_ISDIR(buf.st_mode); +#endif + } + int renderFile(const char* path) { if (!path) return 1; @@ -299,15 +325,42 @@ private: return renderer.render(path, width, height, dst, bgColor); } - int handleDirectory(const string& path, DIR* dir) + int handleDirectory(const string& path) { //open directory - if (!dir) { - dir = opendir(path.c_str()); - if (!dir) { - cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; - return 1; +#ifdef _WIN32 + WIN32_FIND_DATA fd; + HANDLE h = FindFirstFileEx((path + "\\*").c_str(), FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0); + if (h == INVALID_HANDLE_VALUE) { + cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; + return 1; + } + int ret = 0; + do { + if (*fd.cFileName == '.' || *fd.cFileName == '$') continue; + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + string subpath = string(path); + subpath += '\\'; + subpath += fd.cFileName; + ret = handleDirectory(subpath); + if (ret) break; + + } else { + if (!svgFile(fd.cFileName)) continue; + string fullpath = string(path); + fullpath += '\\'; + fullpath += fd.cFileName; + ret = renderFile(fullpath.c_str()); + if (ret) break; } + } while (FindNextFile(h, &fd)); + + FindClose(h); +#else + DIR* dir = opendir(path.c_str()); + if (!dir) { + cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; + return 1; } //list directory @@ -317,29 +370,22 @@ private: if (*entry->d_name == '.' || *entry->d_name == '$') continue; if (entry->d_type == DT_DIR) { string subpath = string(path); -#ifdef _WIN32 - subpath += '\\'; -#else subpath += '/'; -#endif subpath += entry->d_name; - ret = handleDirectory(subpath, nullptr); + ret = handleDirectory(subpath); if (ret) break; } else { if (!svgFile(entry->d_name)) continue; string fullpath = string(path); -#ifdef _WIN32 - fullpath += '\\'; -#else fullpath += '/'; -#endif fullpath += entry->d_name; ret = renderFile(fullpath.c_str()); if (ret) break; } } closedir(dir); +#endif return ret; } };