diff --git a/src/pdf2htmlEX.cc b/src/pdf2htmlEX.cc index d683edb..68546d8 100644 --- a/src/pdf2htmlEX.cc +++ b/src/pdf2htmlEX.cc @@ -343,11 +343,11 @@ void check_param() int main(int argc, char **argv) { // We need to adjust these directories before parsing the options. -#ifndef _WIN32 +#ifndef __MINGW32__ param.tmp_dir = "/tmp"; param.data_dir = PDF2HTMLEX_DATA_PATH; #else - param.data_dir = get_data_dir(argv[0]); + param.data_dir = get_exec_dir(argv[0]); param.tmp_dir = get_tmp_dir(); #endif diff --git a/src/util/win32.cc b/src/util/win32.cc index dbd4633..ee45511 100644 --- a/src/util/win32.cc +++ b/src/util/win32.cc @@ -5,53 +5,65 @@ * 2014.01.13 */ -#ifdef _WIN32 +#ifdef __MINGW32__ #include +#include #include #include -#include -#include -#include +#include +#include #include "win32.h" +using namespace std; + char* mkdtemp(char* temp) { + char *filename = nullptr; if (temp != nullptr) { - bool created = false; - char value[30]; - - srand((unsigned)time(0)); - while (!created) { - int rand_value = (int)((rand() / ((double)RAND_MAX+1.0)) * 1e6); - sprintf(value, "%06d", rand_value); - sprintf(temp + strlen(temp) - 6, "%6.6s", value); - created = _mkdir(temp) == 0; + filename = mktemp(temp); + if (filename != nullptr) { + if (_mkdir(temp) != 0) { + filename = nullptr; + } } } - return temp; + return filename; } namespace pdf2htmlEX { -std::string get_data_dir(char *dir) +string get_exec_dir(char *dir) { // Under Windows, the default data_dir is under /data in the pdf2htmlEX directory - std::stringstream ss; - ss << dirname(dir) << "/data"; - return ss.str(); + string s = dirname(dir); + if (s == ".") { + char* wd(getcwd(nullptr, PATH_MAX)); + s = wd; + free(wd); + } + s += "/data"; + return s; } -std::string get_tmp_dir() +string get_tmp_dir() { // Under Windows, the temp path is not under /tmp, find it. - char temppath[MAX_PATH]; - ::GetTempPath(MAX_PATH, temppath); - return temppath; + char *tmp = getenv("TMP"); + if (tmp == nullptr) { + tmp = getenv("TEMP"); + } + + if (tmp == nullptr) { + cerr << "Error: Cannot find temporary directory. Export TMP/TEMP variable."; + exit(EXIT_FAILURE); + } + + return std::string(tmp) + "/"; } } // namespace pdf2htmlEX; -#endif //_WIN32 +#endif //__MINGW32__ diff --git a/src/util/win32.h b/src/util/win32.h index 2e1f10e..a2e6dd0 100644 --- a/src/util/win32.h +++ b/src/util/win32.h @@ -8,11 +8,9 @@ #ifndef WIN32_H__ #define WIN32_H__ -#ifdef _WIN32 +#ifdef __MINGW32__ #include -#include -#include char *mkdtemp(char *temp); @@ -20,11 +18,11 @@ char *mkdtemp(char *temp); #define stat _stat namespace pdf2htmlEX { - std::string get_data_dir(char *dir); + std::string get_exec_dir(char *dir); std::string get_tmp_dir(); } // namespace pdf2htmlEX -#endif //_WIN32 +#endif //__MINGW32__ #endif //WIN32_H__