2 memory-stream.cc -- implement Memory_out_stream
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
14 #include "memory-stream.hh"
18 TODO: add read support as well.
20 const int Memory_out_stream::block_size_ = 1024;
23 Memory_out_stream::functions_ = {
24 Memory_out_stream::reader,
25 Memory_out_stream::writer,
26 Memory_out_stream::seeker,
27 Memory_out_stream::cleaner
31 Memory_out_stream::cleaner (void *cookie)
33 Memory_out_stream *stream = (Memory_out_stream*) cookie;
39 Memory_out_stream::Memory_out_stream ()
44 file_ = fopencookie ((void*) this, "w", functions_);
47 Memory_out_stream::~Memory_out_stream ()
56 Memory_out_stream::get_file () const
62 Memory_out_stream::get_length () const
68 Memory_out_stream::get_string () const
74 Memory_out_stream::writer (void *cookie,
78 Memory_out_stream *stream = (Memory_out_stream*) cookie;
80 ssize_t newsize = stream->size_ + size;
83 while (newsize > stream->buffer_blocks_ * block_size_)
85 stream->buffer_blocks_ *= 2;
86 stream->buffer_blocks_ += 1;
91 stream->buffer_ = (char*) realloc (stream->buffer_,
92 stream->buffer_blocks_ * block_size_);
94 memcpy (stream->buffer_ + stream->size_, buffer, size);
95 stream->size_ = newsize;
101 Memory_out_stream::reader (void *cookie,
114 Memory_out_stream::seeker (void *, off64_t *, int whence)