fix compilation of svg2png on Windows

This commit is contained in:
Vincent Torri 2022-08-20 10:55:16 +02:00 committed by Hermet Park
parent ddb9bbdf0e
commit e918a8cb5b

View file

@ -25,9 +25,17 @@
#include <thorvg.h> #include <thorvg.h>
#include <vector> #include <vector>
#include "lodepng.h" #include "lodepng.h"
#include <dirent.h> #ifdef _WIN32
#include <unistd.h> #include <windows.h>
#include <limits.h> #ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#else
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#endif
using namespace std; using namespace std;
@ -229,11 +237,10 @@ public:
for (auto path : paths) { for (auto path : paths) {
auto real_path = realFile(path); auto real_path = realFile(path);
if (real_path) { if (real_path) {
DIR* dir = opendir(real_path); if (isDirectory(real_path)) {
if (dir) {
//load from directory //load from directory
cout << "Trying load from directory \"" << real_path << "\"." << endl; 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)) { } else if (svgFile(path)) {
//load single file //load single file
@ -286,6 +293,25 @@ private:
return path; 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) int renderFile(const char* path)
{ {
if (!path) return 1; if (!path) return 1;
@ -299,15 +325,42 @@ private:
return renderer.render(path, width, height, dst, bgColor); return renderer.render(path, width, height, dst, bgColor);
} }
int handleDirectory(const string& path, DIR* dir) int handleDirectory(const string& path)
{ {
//open directory //open directory
if (!dir) { #ifdef _WIN32
dir = opendir(path.c_str()); WIN32_FIND_DATA fd;
if (!dir) { HANDLE h = FindFirstFileEx((path + "\\*").c_str(), FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0);
cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; if (h == INVALID_HANDLE_VALUE) {
return 1; 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 //list directory
@ -317,29 +370,22 @@ private:
if (*entry->d_name == '.' || *entry->d_name == '$') continue; if (*entry->d_name == '.' || *entry->d_name == '$') continue;
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
string subpath = string(path); string subpath = string(path);
#ifdef _WIN32
subpath += '\\';
#else
subpath += '/'; subpath += '/';
#endif
subpath += entry->d_name; subpath += entry->d_name;
ret = handleDirectory(subpath, nullptr); ret = handleDirectory(subpath);
if (ret) break; if (ret) break;
} else { } else {
if (!svgFile(entry->d_name)) continue; if (!svgFile(entry->d_name)) continue;
string fullpath = string(path); string fullpath = string(path);
#ifdef _WIN32
fullpath += '\\';
#else
fullpath += '/'; fullpath += '/';
#endif
fullpath += entry->d_name; fullpath += entry->d_name;
ret = renderFile(fullpath.c_str()); ret = renderFile(fullpath.c_str());
if (ret) break; if (ret) break;
} }
} }
closedir(dir); closedir(dir);
#endif
return ret; return ret;
} }
}; };