diff --git a/CMakeLists.txt b/CMakeLists.txt index 068bcee..66a2cd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,11 +165,14 @@ add_executable(pdf2htmlEX src/BackgroundRenderer/CairoBackgroundRenderer.cc src/util/ArgParser.h src/util/ArgParser.cc + src/util/base64.h + src/util/base64.cc src/util/const.h src/util/const.cc src/util/ffw.h src/util/ffw.c src/util/namespace.h + src/util/string_formatter.h src/util/TmpFiles.h src/util/TmpFiles.cc src/util/unicode.h diff --git a/src/HTMLRenderer/HTMLRenderer.h b/src/HTMLRenderer/HTMLRenderer.h index 2922a8e..9ff3b11 100644 --- a/src/HTMLRenderer/HTMLRenderer.h +++ b/src/HTMLRenderer/HTMLRenderer.h @@ -27,6 +27,7 @@ #include "Param.h" #include "Preprocessor.h" #include "util/util.h" +#include "util/string_formatter.h" #include "util/TmpFiles.h" /* diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index b3fa713..5c79e43 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -12,6 +12,7 @@ #include "HTMLRenderer.h" #include "util/namespace.h" +#include "util/base64.h" namespace pdf2htmlEX { diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index a8253bc..ab83699 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -18,6 +18,7 @@ #include "BackgroundRenderer/BackgroundRenderer.h" #include "util/namespace.h" #include "util/ffw.h" +#include "util/base64.h" namespace pdf2htmlEX { diff --git a/src/util/base64.cc b/src/util/base64.cc new file mode 100644 index 0000000..51b4a7e --- /dev/null +++ b/src/util/base64.cc @@ -0,0 +1,3 @@ +#include "base64.h" + +const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/src/util/base64.h b/src/util/base64.h new file mode 100644 index 0000000..efebac8 --- /dev/null +++ b/src/util/base64.h @@ -0,0 +1,62 @@ +/* + * Base64 Encoding + * + * by WangLu + * 2012.11.29 + */ + +#ifndef BASE64_H__ +#define BASE64_H__ + +#include + +class base64stream +{ +public: + + base64stream(std::istream & in) : in(&in) { } + base64stream(std::istream && in) : in(&in) { } + + std::ostream & dumpto(std::ostream & out) + { + unsigned char buf[3]; + while(in->read((char*)buf, 3)) + { + out << base64_encoding[(buf[0] & 0xfc)>>2] + << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)] + << base64_encoding[((buf[1] & 0x0f)<<2) | ((buf[2] & 0xc0)>>6)] + << base64_encoding[(buf[2] & 0x3f)]; + } + auto cnt = in->gcount(); + if(cnt > 0) + { + for(int i = cnt; i < 3; ++i) + buf[i] = 0; + + out << base64_encoding[(buf[0] & 0xfc)>>2] + << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)]; + + if(cnt > 1) + { + out << base64_encoding[(buf[1] & 0x0f)<<2]; + } + else + { + out << '='; + } + out << '='; + } + + return out; + } + +private: + std::istream * in; + static const char * base64_encoding; +}; + +static inline std::ostream & operator << (std::ostream & out, base64stream & bf) { return bf.dumpto(out); } +static inline std::ostream & operator << (std::ostream & out, base64stream && bf) { return bf.dumpto(out); } + + +#endif //BASE64_H__ diff --git a/src/util/string_formatter.h b/src/util/string_formatter.h new file mode 100644 index 0000000..64da233 --- /dev/null +++ b/src/util/string_formatter.h @@ -0,0 +1,53 @@ +/* + * Buffer reusing string formatter + * + * by WangLu + * 2012.11.29 + */ + +#ifndef STRING_FORMATTER_H__ +#define STRING_FORMATTER_H__ + +class string_formatter +{ +public: + class guarded_pointer + { + public: + guarded_pointer(string_formatter * sf) : sf(sf) { ++(sf->buf_cnt); } + ~guarded_pointer(void) { --(sf->buf_cnt); } + operator char* () { return &(sf->buf.front()); } + private: + string_formatter * sf; + }; + + string_formatter() : buf_cnt(0) { buf.reserve(L_tmpnam); } + /* + * Important: + * there is only one buffer, so new strings will replace old ones + */ + guarded_pointer operator () (const char * format, ...) { + assert((buf_cnt == 0) && "string_formatter: buffer is reused!"); + + va_list vlist; + va_start(vlist, format); + int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); + va_end(vlist); + if(l >= (int)buf.capacity()) + { + buf.reserve(std::max((long)(l+1), (long)buf.capacity() * 2)); + va_start(vlist, format); + l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); + va_end(vlist); + } + assert(l >= 0); // we should fail when vsnprintf fail + assert(l < (int)buf.capacity()); + return guarded_pointer(this); + } +private: + friend class guarded_pointer; + std::vector buf; + int buf_cnt; +}; + +#endif //STRING_FORMATTER_H__ diff --git a/src/util/util.cc b/src/util/util.cc index 606b33d..feb09ea 100644 --- a/src/util/util.cc +++ b/src/util/util.cc @@ -54,9 +54,6 @@ void _tm_multiply(double * tm_left, const double * tm_right) tm_left[5] += old[1] * tm_right[4] + old[3] * tm_right[5]; } - -const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - void create_directories(string path) { if(path.empty()) return; diff --git a/src/util/util.h b/src/util/util.h index d10ef2e..e9011a9 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -92,96 +92,6 @@ public: } }; -class base64stream -{ -public: - - base64stream(std::istream & in) : in(&in) { } - base64stream(std::istream && in) : in(&in) { } - - std::ostream & dumpto(std::ostream & out) - { - unsigned char buf[3]; - while(in->read((char*)buf, 3)) - { - out << base64_encoding[(buf[0] & 0xfc)>>2] - << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)] - << base64_encoding[((buf[1] & 0x0f)<<2) | ((buf[2] & 0xc0)>>6)] - << base64_encoding[(buf[2] & 0x3f)]; - } - auto cnt = in->gcount(); - if(cnt > 0) - { - for(int i = cnt; i < 3; ++i) - buf[i] = 0; - - out << base64_encoding[(buf[0] & 0xfc)>>2] - << base64_encoding[((buf[0] & 0x03)<<4) | ((buf[1] & 0xf0)>>4)]; - - if(cnt > 1) - { - out << base64_encoding[(buf[1] & 0x0f)<<2]; - } - else - { - out << '='; - } - out << '='; - } - - return out; - } - -private: - std::istream * in; - static const char * base64_encoding; -}; - -static inline std::ostream & operator << (std::ostream & out, base64stream & bf) { return bf.dumpto(out); } -static inline std::ostream & operator << (std::ostream & out, base64stream && bf) { return bf.dumpto(out); } - -class string_formatter -{ -public: - class guarded_pointer - { - public: - guarded_pointer(string_formatter * sf) : sf(sf) { ++(sf->buf_cnt); } - ~guarded_pointer(void) { --(sf->buf_cnt); } - operator char* () { return &(sf->buf.front()); } - private: - string_formatter * sf; - }; - - string_formatter() : buf_cnt(0) { buf.reserve(L_tmpnam); } - /* - * Important: - * there is only one buffer, so new strings will replace old ones - */ - guarded_pointer operator () (const char * format, ...) { - assert((buf_cnt == 0) && "string_formatter: buffer is reused!"); - - va_list vlist; - va_start(vlist, format); - int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); - va_end(vlist); - if(l >= (int)buf.capacity()) - { - buf.reserve(std::max((long)(l+1), (long)buf.capacity() * 2)); - va_start(vlist, format); - l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); - va_end(vlist); - } - assert(l >= 0); // we should fail when vsnprintf fail - assert(l < (int)buf.capacity()); - return guarded_pointer(this); - } -private: - friend class guarded_pointer; - std::vector buf; - int buf_cnt; -}; - void create_directories(std::string path); bool is_truetype_suffix(const std::string & suffix);