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

moving base64/string_formatter out

This commit is contained in:
Lu Wang 2012-11-29 17:50:40 +08:00
parent 35fccdc28c
commit ab28c44034
9 changed files with 124 additions and 93 deletions

View File

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

View File

@ -27,6 +27,7 @@
#include "Param.h"
#include "Preprocessor.h"
#include "util/util.h"
#include "util/string_formatter.h"
#include "util/TmpFiles.h"
/*

View File

@ -12,6 +12,7 @@
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/base64.h"
namespace pdf2htmlEX {

View File

@ -18,6 +18,7 @@
#include "BackgroundRenderer/BackgroundRenderer.h"
#include "util/namespace.h"
#include "util/ffw.h"
#include "util/base64.h"
namespace pdf2htmlEX {

3
src/util/base64.cc Normal file
View File

@ -0,0 +1,3 @@
#include "base64.h"
const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

62
src/util/base64.h Normal file
View File

@ -0,0 +1,62 @@
/*
* Base64 Encoding
*
* by WangLu
* 2012.11.29
*/
#ifndef BASE64_H__
#define BASE64_H__
#include <iostream>
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__

View File

@ -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>((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<char> buf;
int buf_cnt;
};
#endif //STRING_FORMATTER_H__

View File

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

View File

@ -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>((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<char> buf;
int buf_cnt;
};
void create_directories(std::string path);
bool is_truetype_suffix(const std::string & suffix);