6 #ifndef TURI_CHARSTREAM 7 #define TURI_CHARSTREAM 9 #include <boost/iostreams/stream.hpp> 10 #include <boost/iostreams/categories.hpp> 15 namespace charstream_impl {
17 template <
bool self_deleting>
18 struct resizing_array_sink {
21 resizing_array_sink(
size_t initial = 0) : str(NULL) {
23 str = (
char*)(malloc(initial));
27 buffer_size = initial;
30 resizing_array_sink(
const resizing_array_sink& other) :
31 len(other.len), buffer_size(other.buffer_size) {
33 str = (
char*)(malloc(other.buffer_size));
35 memcpy(str, other.str, len);
41 ~resizing_array_sink() {
42 if( self_deleting && str != NULL) {
55 size_t size()
const {
return len; }
56 char* c_str() {
return str; }
57 const char* c_str()
const {
return str; }
63 void clear(
size_t new_buffer_size) {
65 str = (
char*)realloc(str, new_buffer_size);
66 buffer_size = new_buffer_size;
69 void reserve(
size_t new_buffer_size) {
70 if (new_buffer_size > buffer_size) {
71 str = (
char*)realloc(str, new_buffer_size);
72 buffer_size = new_buffer_size;
79 typedef char char_type;
80 struct category:
public boost::iostreams::device_tag,
81 public boost::iostreams::output,
82 public boost::iostreams::multichar_tag { };
85 inline std::streamsize optimal_buffer_size()
const {
return 0; }
87 inline std::streamsize advance(std::streamsize n) {
88 if (len + n > buffer_size) {
90 buffer_size = 2 * (len + n);
91 str = (
char*)realloc(str, buffer_size);
98 inline std::streamsize write(
const char* s, std::streamsize n) {
99 if (len + n > buffer_size) {
101 buffer_size = 2 * (len + n);
102 str = (
char*)realloc(str, buffer_size);
105 memcpy(str + len, s, n);
110 inline void swap(resizing_array_sink<self_deleting> &other) {
111 std::swap(str, other.str);
112 std::swap(len, other.len);
113 std::swap(buffer_size, other.buffer_size);
134 typedef boost::iostreams::stream< charstream_impl::resizing_array_sink<true> >
boost::iostreams::stream< charstream_impl::resizing_array_sink< true > > charstream