1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

removing boost::filesystem

This commit is contained in:
Lu Wang 2012-09-10 01:18:09 +08:00
parent 52df16011f
commit 8792527ef6
4 changed files with 178 additions and 131 deletions

View File

@ -78,6 +78,7 @@ add_executable(pdf2htmlEX
src/Consts.h
src/Consts.cc
src/util.h
src/util.cc
src/config.h)
target_link_libraries(pdf2htmlEX ${POPPLER_LIBRARIES} ${Boost_LIBRARIES} ${FONTFORGE_LIBRARIES} ${PYTHON_LIBRARIES})

View File

@ -12,7 +12,6 @@
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <goo/GooString.h>
@ -27,7 +26,6 @@
namespace po = boost::program_options;
using namespace std;
using namespace boost::filesystem;
Param param;
@ -64,7 +62,6 @@ po::variables_map parse_options (int argc, char **argv)
("user-password,u", po::value<string>(&param.user_password)->default_value(""), "user password (for encrypted files)")
("dest-dir", po::value<string>(&param.dest_dir)->default_value("."), "destination directory")
("tmp-dir", po::value<string>(&param.tmp_dir)->default_value((temp_directory_path() / "/pdf2htmlEX").string()), "temporary directory")
("first-page,f", po::value<int>(&param.first_page)->default_value(1), "first page to process")
("last-page,l", po::value<int>(&param.last_page)->default_value(numeric_limits<int>::max()), "last page to process")
@ -125,25 +122,14 @@ int main(int argc, char **argv)
}
//prepare the directories
auto user_dirs = {param.dest_dir, param.tmp_dir};
for(auto iter = user_dirs.begin(); iter != user_dirs.end(); ++iter)
{
const auto & p = *iter;
if(equivalent(PDF2HTMLEX_DATA_PATH, p))
{
cerr << "The specified directory \"" << p << "\" is the library path for pdf2htmlEX. Please use another one." << endl;
return -1;
}
}
param.tmp_dir = tmpnam(nullptr);
try
{
create_directories(param.dest_dir);
create_directories(param.tmp_dir);
}
catch (const filesystem_error& err)
catch (...)
{
cerr << err.what() << endl;
return -1;
}
@ -175,7 +161,13 @@ int main(int argc, char **argv)
if(param.output_filename == "")
{
const string s = path(param.input_filename).filename().string();
size_t idx = param.input_filename.rfind('/');
if(idx == string::npos)
idx = 0;
else
++ idx;
const string s =param.input_filename.substr(idx);
if((s.size() >= 4) && (s.compare(s.size() - 4, 4, ".pdf") == 0))
{
param.output_filename = s.substr(0, s.size() - 4) + ".html";

154
src/util.cc Normal file
View File

@ -0,0 +1,154 @@
/*
* Misc functions
*
*
* by WangLu
* 2012.08.10
*/
#include <istream>
#include <ostream>
#include <cmath>
#include <cstdarg>
#include <vector>
#include <GfxState.h>
#include <GfxFont.h>
#include <CharTypes.h>
#include <GlobalParams.h>
#include <Object.h>
// for mkdir
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
using std::cerr;
using std::endl;
using std::string;
bool isLegalUnicode(Unicode u)
{
/*
if((u == 9) || (u == 10) || (u == 13))
return true;
*/
if(u <= 31)
return false;
if((u >= 127) && (u <= 159))
return false;
if((u >= 0xd800) && (u <= 0xdfff))
return false;
return true;
}
Unicode map_to_private(CharCode code)
{
Unicode private_mapping = (Unicode)(code + 0xE000);
if(private_mapping > 0xF8FF)
{
private_mapping = (Unicode)((private_mapping - 0xF8FF) + 0xF0000);
if(private_mapping > 0xFFFFD)
{
private_mapping = (Unicode)((private_mapping - 0xFFFFD) + 0x100000);
if(private_mapping > 0x10FFFD)
{
cerr << "Warning: all private use unicode are used" << endl;
}
}
}
return private_mapping;
}
Unicode unicode_from_font (CharCode code, GfxFont * font)
{
if(!font->isCIDFont())
{
char * cname = dynamic_cast<Gfx8BitFont*>(font)->getCharName(code);
// may be untranslated ligature
if(cname)
{
Unicode ou = globalParams->mapNameToUnicode(cname);
if(isLegalUnicode(ou))
return ou;
}
}
return map_to_private(code);
}
Unicode check_unicode(Unicode * u, int len, CharCode code, GfxFont * font)
{
if(len == 0)
return map_to_private(code);
if(len == 1)
{
if(isLegalUnicode(*u))
return *u;
}
return unicode_from_font(code, font);
}
void outputUnicodes(std::ostream & out, const Unicode * u, int uLen)
{
for(int i = 0; i < uLen; ++i)
{
switch(u[i])
{
case '&':
out << "&amp;";
break;
case '\"':
out << "&quot;";
break;
case '\'':
out << "&apos;";
break;
case '<':
out << "&lt;";
break;
case '>':
out << "&gt;";
break;
default:
{
char buf[4];
auto n = mapUTF8(u[i], buf, 4);
out.write(buf, n);
}
}
}
}
const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void create_directories(string path)
{
size_t idx = path.rfind('/');
if(idx != string::npos)
{
create_directories(path.substr(0, idx));
}
int r = ::mkdir(path.c_str(), S_IRWXU);
if(r != 0)
{
if(errno == EEXIST)
{
struct stat stat_buf;
if((::stat(path.c_str(), &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
return;
}
throw string("Cannot create directory: ") + path;
}
}

View File

@ -10,37 +10,26 @@
#ifndef UTIL_H__
#define UTIL_H__
#include <algorithm>
#include <istream>
#include <ostream>
#include <iostream>
#include <cmath>
#include <cstdarg>
#include <vector>
#include <algorithm>
#include <GfxState.h>
#include <GfxFont.h>
#include <CharTypes.h>
#include <UTF8.h>
#include <GlobalParams.h>
#include <Object.h>
#include "Consts.h"
using std::istream;
using std::ostream;
using std::noskipws;
using std::endl;
using std::flush;
using std::cerr;
using std::floor;
using std::max;
using std::abs;
// mute gcc warning of unused function
namespace
{
template <class T>
void dummy(){ auto _ = &mapUCS2; }
void dummy(){
auto _1 = &mapUCS2;
auto _2 = &mapUTF8;
}
}
static inline bool _equal(double x, double y) { return std::abs(x-y) < EPS; }
@ -61,113 +50,23 @@ static inline long long hash_ref(const Ref * id)
/*
* http://en.wikipedia.org/wiki/HTML_decimal_character_rendering
*/
static inline bool isLegalUnicode(Unicode u)
{
/*
if((u == 9) || (u == 10) || (u == 13))
return true;
*/
bool isLegalUnicode(Unicode u);
if(u <= 31)
return false;
if((u >= 127) && (u <= 159))
return false;
if((u >= 0xd800) && (u <= 0xdfff))
return false;
return true;
}
static inline Unicode map_to_private(CharCode code)
{
Unicode private_mapping = (Unicode)(code + 0xE000);
if(private_mapping > 0xF8FF)
{
private_mapping = (Unicode)((private_mapping - 0xF8FF) + 0xF0000);
if(private_mapping > 0xFFFFD)
{
private_mapping = (Unicode)((private_mapping - 0xFFFFD) + 0x100000);
if(private_mapping > 0x10FFFD)
{
cerr << "Warning: all private use unicode are used" << endl;
}
}
}
return private_mapping;
}
Unicode map_to_private(CharCode code);
/*
* Try to determine the Unicode value directly from the information in the font
*/
static inline Unicode unicode_from_font (CharCode code, GfxFont * font)
{
if(!font->isCIDFont())
{
char * cname = dynamic_cast<Gfx8BitFont*>(font)->getCharName(code);
// may be untranslated ligature
if(cname)
{
Unicode ou = globalParams->mapNameToUnicode(cname);
if(isLegalUnicode(ou))
return ou;
}
}
return map_to_private(code);
}
Unicode unicode_from_font (CharCode code, GfxFont * font);
/*
* We have to use a single Unicode value to reencode fonts
* if we got multi-unicode values, it might be expanded ligature, try to restore it
* if we cannot figure it out at the end, use a private mapping
*/
static inline Unicode check_unicode(Unicode * u, int len, CharCode code, GfxFont * font)
{
if(len == 0)
return map_to_private(code);
Unicode check_unicode(Unicode * u, int len, CharCode code, GfxFont * font);
if(len == 1)
{
if(isLegalUnicode(*u))
return *u;
}
return unicode_from_font(code, font);
}
static inline void outputUnicodes(std::ostream & out, const Unicode * u, int uLen)
{
for(int i = 0; i < uLen; ++i)
{
switch(u[i])
{
case '&':
out << "&amp;";
break;
case '\"':
out << "&quot;";
break;
case '\'':
out << "&apos;";
break;
case '<':
out << "&lt;";
break;
case '>':
out << "&gt;";
break;
default:
{
char buf[4];
auto n = mapUTF8(u[i], buf, 4);
out.write(buf, n);
}
}
}
}
void outputUnicodes(std::ostream & out, const Unicode * u, int uLen);
static inline bool operator < (const GfxRGB & rgb1, const GfxRGB & rgb2)
{
@ -216,8 +115,6 @@ public:
double _[6];
};
// may move inside base64stream when we have to create a util.c
static const char * base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
class base64stream
{
public:
@ -260,6 +157,7 @@ public:
private:
istream * in;
static const char * base64_encoding;
};
static inline ostream & operator << (ostream & out, base64stream & bf) { return bf.dumpto(out); }
@ -307,4 +205,6 @@ private:
int buf_cnt;
};
void create_directories(std::string path);
#endif //UTIL_H__