From 5aebf2c73896798cd701d006a978a6d471a49f68 Mon Sep 17 00:00:00 2001 From: Marc Sanfacon Date: Mon, 13 Jan 2014 20:08:23 -0500 Subject: [PATCH 1/3] Make it work on MingW --- CMakeLists.txt | 2 ++ src/TmpFiles.cc | 1 + src/pdf2htmlEX.cc | 6 +++++ src/util/path.cc | 1 + src/util/win32.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++ src/util/win32.h | 30 +++++++++++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 src/util/win32.cc create mode 100644 src/util/win32.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9011200..8d4b67c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,6 +206,8 @@ set(PDF2HTMLEX_SRC ${PDF2HTMLEX_SRC} src/util/path.cc src/util/unicode.h src/util/unicode.cc + src/util/win32.h + src/util/win32.cc src/ArgParser.h src/ArgParser.cc src/Base64Stream.h diff --git a/src/TmpFiles.cc b/src/TmpFiles.cc index 2ff4956..1cc9e22 100644 --- a/src/TmpFiles.cc +++ b/src/TmpFiles.cc @@ -14,6 +14,7 @@ #include "TmpFiles.h" #include "Param.h" +#include "util/win32.h" using namespace std; diff --git a/src/pdf2htmlEX.cc b/src/pdf2htmlEX.cc index 33562dd..d683edb 100644 --- a/src/pdf2htmlEX.cc +++ b/src/pdf2htmlEX.cc @@ -35,6 +35,7 @@ #include "util/path.h" #include "util/ffw.h" +#include "util/win32.h" using namespace std; using namespace pdf2htmlEX; @@ -342,8 +343,13 @@ void check_param() int main(int argc, char **argv) { // We need to adjust these directories before parsing the options. +#ifndef _WIN32 param.tmp_dir = "/tmp"; param.data_dir = PDF2HTMLEX_DATA_PATH; +#else + param.data_dir = get_data_dir(argv[0]); + param.tmp_dir = get_tmp_dir(); +#endif parse_options(argc, argv); check_param(); diff --git a/src/util/path.cc b/src/util/path.cc index e0b1b94..15e5200 100644 --- a/src/util/path.cc +++ b/src/util/path.cc @@ -11,6 +11,7 @@ #include #include "path.h" +#include "win32.h" using std::string; diff --git a/src/util/win32.cc b/src/util/win32.cc new file mode 100644 index 0000000..dbd4633 --- /dev/null +++ b/src/util/win32.cc @@ -0,0 +1,57 @@ +/* + * Win32 specific functions + * + * by MarcSanfacon + * 2014.01.13 + */ + +#ifdef _WIN32 + +#include +#include +#include +#include +#include +#include + +#include "win32.h" + +char* mkdtemp(char* temp) +{ + 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; + } + } + + return temp; +} + +namespace pdf2htmlEX { +std::string get_data_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(); +} + +std::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; +} + +} // namespace pdf2htmlEX; + +#endif //_WIN32 + diff --git a/src/util/win32.h b/src/util/win32.h new file mode 100644 index 0000000..2e1f10e --- /dev/null +++ b/src/util/win32.h @@ -0,0 +1,30 @@ +/* + * Win32 specific functions + * + * by MarcSanfacon + * 2014.01.13 + */ + +#ifndef WIN32_H__ +#define WIN32_H__ + +#ifdef _WIN32 + +#include +#include +#include + +char *mkdtemp(char *temp); + +#define mkdir(A, B) _mkdir(A) +#define stat _stat + +namespace pdf2htmlEX { + std::string get_data_dir(char *dir); + std::string get_tmp_dir(); +} // namespace pdf2htmlEX + +#endif //_WIN32 + +#endif //WIN32_H__ + From 94ddd697a689704b3e3d7488fc360682082f1228 Mon Sep 17 00:00:00 2001 From: Marc Sanfacon Date: Tue, 14 Jan 2014 09:39:14 -0500 Subject: [PATCH 2/3] Modifications following code review --- src/pdf2htmlEX.cc | 4 ++-- src/util/win32.cc | 58 ++++++++++++++++++++++++++++------------------- src/util/win32.h | 8 +++---- 3 files changed, 40 insertions(+), 30 deletions(-) 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__ From f0c260d6a0d62755b5daab3aaf7b54e2fddb0b68 Mon Sep 17 00:00:00 2001 From: Marc Sanfacon Date: Wed, 15 Jan 2014 08:29:46 -0500 Subject: [PATCH 3/3] Renamed win32.* -> mingw.* --- CMakeLists.txt | 4 ++-- src/TmpFiles.cc | 5 ++++- src/pdf2htmlEX.cc | 13 ++++++++----- src/util/{win32.cc => mingw.cc} | 9 ++------- src/util/{win32.h => mingw.h} | 6 +++--- src/util/path.cc | 5 ++++- 6 files changed, 23 insertions(+), 19 deletions(-) rename src/util/{win32.cc => mingw.cc} (84%) rename src/util/{win32.h => mingw.h} (86%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d4b67c..0286e98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,8 +206,8 @@ set(PDF2HTMLEX_SRC ${PDF2HTMLEX_SRC} src/util/path.cc src/util/unicode.h src/util/unicode.cc - src/util/win32.h - src/util/win32.cc + src/util/mingw.h + src/util/mingw.cc src/ArgParser.h src/ArgParser.cc src/Base64Stream.h diff --git a/src/TmpFiles.cc b/src/TmpFiles.cc index 1cc9e22..cecd90d 100644 --- a/src/TmpFiles.cc +++ b/src/TmpFiles.cc @@ -14,7 +14,10 @@ #include "TmpFiles.h" #include "Param.h" -#include "util/win32.h" + +#ifdef __MINGW32__ +#include "util/mingw.h" +#endif using namespace std; diff --git a/src/pdf2htmlEX.cc b/src/pdf2htmlEX.cc index 68546d8..1f306c2 100644 --- a/src/pdf2htmlEX.cc +++ b/src/pdf2htmlEX.cc @@ -35,7 +35,10 @@ #include "util/path.h" #include "util/ffw.h" -#include "util/win32.h" + +#ifdef __MINGW32__ +#include "util/mingw.h" +#endif using namespace std; using namespace pdf2htmlEX; @@ -343,12 +346,12 @@ void check_param() int main(int argc, char **argv) { // We need to adjust these directories before parsing the options. -#ifndef __MINGW32__ - param.tmp_dir = "/tmp"; - param.data_dir = PDF2HTMLEX_DATA_PATH; -#else +#if defined(__MINGW32__) param.data_dir = get_exec_dir(argv[0]); param.tmp_dir = get_tmp_dir(); +#else + param.tmp_dir = "/tmp"; + param.data_dir = PDF2HTMLEX_DATA_PATH; #endif parse_options(argc, argv); diff --git a/src/util/win32.cc b/src/util/mingw.cc similarity index 84% rename from src/util/win32.cc rename to src/util/mingw.cc index ee45511..5d75be0 100644 --- a/src/util/win32.cc +++ b/src/util/mingw.cc @@ -14,7 +14,7 @@ #include #include -#include "win32.h" +#include "mingw.h" using namespace std; @@ -55,12 +55,7 @@ string get_tmp_dir() tmp = getenv("TEMP"); } - if (tmp == nullptr) { - cerr << "Error: Cannot find temporary directory. Export TMP/TEMP variable."; - exit(EXIT_FAILURE); - } - - return std::string(tmp) + "/"; + return tmp != nullptr ? string(tmp) + "/" : "/"; } } // namespace pdf2htmlEX; diff --git a/src/util/win32.h b/src/util/mingw.h similarity index 86% rename from src/util/win32.h rename to src/util/mingw.h index a2e6dd0..f7a3b52 100644 --- a/src/util/win32.h +++ b/src/util/mingw.h @@ -5,8 +5,8 @@ * 2014.01.13 */ -#ifndef WIN32_H__ -#define WIN32_H__ +#ifndef MINGW_H__ +#define MINGW_H__ #ifdef __MINGW32__ @@ -24,5 +24,5 @@ namespace pdf2htmlEX { #endif //__MINGW32__ -#endif //WIN32_H__ +#endif //MINGW_H__ diff --git a/src/util/path.cc b/src/util/path.cc index 15e5200..4d451f6 100644 --- a/src/util/path.cc +++ b/src/util/path.cc @@ -11,7 +11,10 @@ #include #include "path.h" -#include "win32.h" + +#ifdef __MINGW32__ +#include "util/mingw.h" +#endif using std::string;