|
23 | 23 | #include <sstream> |
24 | 24 | #include <stdexcept> |
25 | 25 | #include <sys/stat.h> |
26 | | -#include <dlfcn.h> |
| 26 | +#include <dlfcn.h> // not available on Windows, conda install dlfcn-win32 |
| 27 | + |
| 28 | +// libgen.h is not available on Windows |
| 29 | +#ifdef _WIN32 |
| 30 | +#include <string> |
| 31 | +#include <algorithm> |
| 32 | + |
| 33 | +// A simple replacement for dirname() on Windows. |
| 34 | +inline std::string win_dirname(const std::string& path) |
| 35 | +{ |
| 36 | + // Find the last occurrence of either '\' or '/' |
| 37 | + size_t pos = path.find_last_of("\\/"); |
| 38 | + if (pos == std::string::npos) |
| 39 | + return "."; // No directory found, return current directory |
| 40 | + return path.substr(0, pos); |
| 41 | +} |
| 42 | + |
| 43 | +// Overload to match the expected C-style interface if needed: |
| 44 | +inline char* dirname(char* path) |
| 45 | +{ |
| 46 | + static std::string dir; |
| 47 | + dir = win_dirname(std::string(path)); |
| 48 | + return const_cast<char*>(dir.c_str()); |
| 49 | +} |
| 50 | +#else |
27 | 51 | #include <libgen.h> |
| 52 | +#endif |
| 53 | + |
| 54 | +// S_ISDIR, PATH_MAX, realpath, etc. |
| 55 | +#ifdef _WIN32 |
| 56 | +#include <windows.h> |
| 57 | +#include <stdlib.h> // for _fullpath |
| 58 | +#include <sys/stat.h> // for _stat |
| 59 | +#include <direct.h> // for _getcwd, etc. |
| 60 | + |
| 61 | +// Define S_ISDIR using the Windows _S_IFDIR flag from sys/stat.h. |
| 62 | +#ifndef S_ISDIR |
| 63 | +#define S_ISDIR(mode) (((mode) & _S_IFDIR) == _S_IFDIR) |
| 64 | +#endif |
| 65 | + |
| 66 | +// Use MAX_PATH from <windows.h> as PATH_MAX if not defined. |
| 67 | +#ifndef PATH_MAX |
| 68 | +#define PATH_MAX MAX_PATH |
| 69 | +#endif |
| 70 | + |
| 71 | +// Define realpath in terms of _fullpath. |
| 72 | +// _fullpath returns a pointer to the resolved path in the buffer you provide. |
| 73 | +// The usage should be nearly equivalent. |
| 74 | +#define realpath(N, R) _fullpath((R), (N), PATH_MAX) |
| 75 | +#endif |
| 76 | + |
28 | 77 |
|
29 | 78 | #include <diffpy/version.hpp> |
30 | 79 | #include <diffpy/runtimepath.hpp> |
|
0 commit comments