24 #ifndef GRAPHLAB_CHARSTREAM
25 #define GRAPHLAB_CHARSTREAM
27 #include <boost/iostreams/stream.hpp>
28 #include <boost/iostreams/categories.hpp>
33 namespace charstream_impl {
35 template <
bool self_deleting>
36 struct resizing_array_sink {
39 resizing_array_sink(
size_t initial = 0) : str(NULL) {
41 str = (
char*)(malloc(initial));
45 buffer_size = initial;
48 resizing_array_sink(
const resizing_array_sink& other) :
49 len(other.len), buffer_size(other.buffer_size) {
51 str = (
char*)(malloc(other.buffer_size));
53 memcpy(str, other.str, len);
59 ~resizing_array_sink() {
60 if( self_deleting && str != NULL) {
73 size_t size()
const {
return len; }
74 char* c_str() {
return str; }
75 const char* c_str()
const {
return str; }
81 void clear(
size_t new_buffer_size) {
83 str = (
char*)realloc(str, new_buffer_size);
84 buffer_size = new_buffer_size;
87 void reserve(
size_t new_buffer_size) {
88 if (new_buffer_size > buffer_size) {
89 str = (
char*)realloc(str, new_buffer_size);
90 buffer_size = new_buffer_size;
97 typedef char char_type;
98 struct category:
public boost::iostreams::device_tag,
99 public boost::iostreams::output,
100 public boost::iostreams::multichar_tag { };
103 inline std::streamsize optimal_buffer_size()
const {
return 0; }
105 inline std::streamsize advance(std::streamsize n) {
106 if (len + n > buffer_size) {
108 buffer_size = 2 * (len + n);
109 str = (
char*)realloc(str, buffer_size);
116 inline std::streamsize write(
const char* s, std::streamsize n) {
117 if (len + n > buffer_size) {
119 buffer_size = 2 * (len + n);
120 str = (
char*)realloc(str, buffer_size);
123 memcpy(str + len, s, n);
128 inline void swap(resizing_array_sink<self_deleting> &other) {
129 std::swap(str, other.str);
130 std::swap(len, other.len);
131 std::swap(buffer_size, other.buffer_size);
152 typedef boost::iostreams::stream< charstream_impl::resizing_array_sink<true> >