2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
25 #include "memory-stream.hh"
28 TODO: add read support as well.
30 const int Memory_out_stream::block_size_ = 1024;
32 lily_cookie_io_functions_t
33 Memory_out_stream::functions_
36 Memory_out_stream::reader,
37 Memory_out_stream::writer,
38 Memory_out_stream::seeker,
39 Memory_out_stream::cleaner
43 Memory_out_stream::cleaner (void *cookie)
45 Memory_out_stream *stream = (Memory_out_stream *) cookie;
51 Memory_out_stream::Memory_out_stream ()
59 file_ = fopencookie ((void *) this, "w", functions_);
63 Memory_out_stream::~Memory_out_stream ()
72 Memory_out_stream::get_file () const
78 Memory_out_stream::get_length () const
84 Memory_out_stream::get_string () const
90 Memory_out_stream::writer (void *cookie,
94 Memory_out_stream *stream = (Memory_out_stream *) cookie;
96 ssize_t newsize = stream->size_ + (ssize_t) size;
99 while (newsize > stream->buffer_blocks_ * block_size_)
101 stream->buffer_blocks_ *= 2;
102 stream->buffer_blocks_ += 1;
107 stream->buffer_ = (char *) realloc (stream->buffer_,
108 (size_t) (stream->buffer_blocks_ * block_size_));
110 memcpy (stream->buffer_ + stream->size_, buffer, size);
111 stream->size_ = newsize;
113 return (ssize_t) size;
117 Memory_out_stream::reader (void * /* cookie */,
126 Memory_out_stream::seeker (void *,