1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

Modifications following code review

This commit is contained in:
Marc Sanfacon 2014-01-14 09:39:14 -05:00
parent 5aebf2c738
commit 94ddd697a6
3 changed files with 40 additions and 30 deletions

View File

@ -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

View File

@ -5,53 +5,65 @@
* 2014.01.13
*/
#ifdef _WIN32
#ifdef __MINGW32__
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <sstream>
#include <windows.h>
#include <limits.h>
#include <libgen.h>
#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__

View File

@ -8,11 +8,9 @@
#ifndef WIN32_H__
#define WIN32_H__
#ifdef _WIN32
#ifdef __MINGW32__
#include <io.h>
#include <libgen.h>
#include <sstream>
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__