| 1 |
#include <ostream> |
| 2 |
#include <iostream> |
| 3 |
#include <memory> |
| 4 |
#include <stdarg.h> |
| 5 |
|
| 6 |
class oformstream: public std::ostream // 6 |
| 7 |
{ |
| 8 |
public: |
| 9 |
|
| 10 |
oformstream(std::streambuf *buf) // 10 |
| 11 |
: |
| 12 |
std::ostream(buf) |
| 13 |
{} |
| 14 |
|
| 15 |
oformstream & |
| 16 |
form(char const *fmt, ...) // 16 |
| 17 |
{ |
| 18 |
va_list list; |
| 19 |
va_start(list, fmt); |
| 20 |
return vform(fmt, list); |
| 21 |
} |
| 22 |
|
| 23 |
oformstream & |
| 24 |
vform(char const *fmt, va_list list) // 24 |
| 25 |
{ |
| 26 |
size_t size = 128; // 26 |
| 27 |
|
| 28 |
while (true) // 28 |
| 29 |
{ |
| 30 |
std::auto_ptr<char> buf(new char[size]);// 30 |
| 31 |
va_list vlist = list; |
| 32 |
int n = // 32 |
| 33 |
vsnprintf (buf.get(), size, fmt, vlist); |
| 34 |
|
| 35 |
if (n > -1 && n < size) // 35 |
| 36 |
{ |
| 37 |
write(buf.get(), n); |
| 38 |
return *this; |
| 39 |
} |
| 40 |
|
| 41 |
if ((size <<= 1) == 0) // 41 |
| 42 |
{ |
| 43 |
std::cerr << "vform() buffer maximum reached\n"; |
| 44 |
exit(1); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
}; |