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>
13 #include "memory-stream.hh"
17 TODO: add read support as well.
19 const int Memory_out_stream::block_size_ = 1024;
22 Memory_out_stream::functions_ = {
23 Memory_out_stream::reader,
24 Memory_out_stream::writer,
25 Memory_out_stream::seeker,
26 Memory_out_stream::cleaner
30 Memory_out_stream::cleaner (void *cookie)
32 Memory_out_stream *stream = (Memory_out_stream*) cookie;
38 Memory_out_stream::Memory_out_stream ()
43 file_ = fopencookie ((void*) this, "w", functions_);
46 Memory_out_stream::~Memory_out_stream()
55 Memory_out_stream::get_file () const
61 Memory_out_stream::get_length () const
67 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_, stream->buffer_blocks_ * block_size_);
93 memcpy (stream->buffer_ + stream->size_, buffer, size);
94 stream->size_ = newsize;
101 Memory_out_stream::reader (void *cookie,
114 Memory_out_stream::seeker (void*, off64_t *, int whence)