2012-11-29 09:50:40 +00:00
|
|
|
/*
|
|
|
|
* Buffer reusing string formatter
|
|
|
|
*
|
|
|
|
* by WangLu
|
|
|
|
* 2012.11.29
|
|
|
|
*/
|
|
|
|
|
2012-11-29 12:39:30 +00:00
|
|
|
#ifndef STRINGFORMATTER_H__
|
|
|
|
#define STRINGFORMATTER_H__
|
2012-11-29 09:50:40 +00:00
|
|
|
|
2012-11-29 10:16:05 +00:00
|
|
|
namespace pdf2htmlEX {
|
|
|
|
|
2012-11-29 12:39:30 +00:00
|
|
|
class StringFormatter
|
2012-11-29 09:50:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-11-29 12:39:30 +00:00
|
|
|
class GuardedPointer
|
2012-11-29 09:50:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-11-29 12:39:30 +00:00
|
|
|
GuardedPointer(StringFormatter * sf) : sf(sf) { ++(sf->buf_cnt); }
|
|
|
|
GuardedPointer(const GuardedPointer & gp) : sf(gp.sf) { ++(sf->buf_cnt); }
|
|
|
|
~GuardedPointer(void) { --(sf->buf_cnt); }
|
2012-11-29 11:43:19 +00:00
|
|
|
operator char* () const { return &(sf->buf.front()); }
|
2012-11-29 09:50:40 +00:00
|
|
|
private:
|
2012-11-29 12:39:30 +00:00
|
|
|
StringFormatter * sf;
|
2012-11-29 09:50:40 +00:00
|
|
|
};
|
|
|
|
|
2012-11-29 12:39:30 +00:00
|
|
|
StringFormatter() : buf_cnt(0) { buf.reserve(L_tmpnam); }
|
2012-11-29 09:50:40 +00:00
|
|
|
/*
|
|
|
|
* Important:
|
|
|
|
* there is only one buffer, so new strings will replace old ones
|
|
|
|
*/
|
2012-12-11 12:17:36 +00:00
|
|
|
GuardedPointer operator () (const char * format, ...);
|
2012-11-29 09:50:40 +00:00
|
|
|
|
|
|
|
private:
|
2012-11-29 12:39:30 +00:00
|
|
|
friend class GuardedPointer;
|
2012-11-29 09:50:40 +00:00
|
|
|
std::vector<char> buf;
|
|
|
|
int buf_cnt;
|
|
|
|
};
|
|
|
|
|
2012-11-29 10:16:05 +00:00
|
|
|
} //namespace pdf2htmlEX
|
2012-11-29 12:39:30 +00:00
|
|
|
#endif //STRINGFORMATTER_H__
|