Doc: Update LSR.
[lilypond/mpolesky.git] / flower / memory-stream.cc
blob4ea43c214969a1462cab85a834224a644333292a
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2010 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/>.
20 #include <cassert>
21 #include <cstring>
22 #include <cstdlib>
23 using namespace std;
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_
34 = {
35 Memory_out_stream::reader,
36 Memory_out_stream::writer,
37 Memory_out_stream::seeker,
38 Memory_out_stream::cleaner
41 int
42 Memory_out_stream::cleaner (void *cookie)
44 Memory_out_stream *stream = (Memory_out_stream *) cookie;
46 stream->file_ = 0;
47 return 0;
50 Memory_out_stream::Memory_out_stream ()
52 size_ = 0;
53 buffer_ = 0;
54 buffer_blocks_ = 0;
55 file_ = 0;
57 #if 0
58 file_ = fopencookie ((void *) this, "w", functions_);
59 #endif
62 Memory_out_stream::~Memory_out_stream ()
64 if (file_)
65 fclose (file_);
67 free (buffer_);
70 FILE *
71 Memory_out_stream::get_file () const
73 return file_;
76 ssize_t
77 Memory_out_stream::get_length () const
79 return size_;
82 char const *
83 Memory_out_stream::get_string () const
85 return buffer_;
88 ssize_t
89 Memory_out_stream::writer (void *cookie,
90 char const *buffer,
91 size_t size)
93 Memory_out_stream *stream = (Memory_out_stream *) cookie;
95 ssize_t newsize = stream->size_ + size;
97 bool change = false;
98 while (newsize > stream->buffer_blocks_ * block_size_)
100 stream->buffer_blocks_ *= 2;
101 stream->buffer_blocks_ += 1;
102 change = true;
105 if (change)
106 stream->buffer_ = (char *) realloc (stream->buffer_,
107 stream->buffer_blocks_ * block_size_);
109 memcpy (stream->buffer_ + stream->size_, buffer, size);
110 stream->size_ = newsize;
112 return size;
115 ssize_t
116 Memory_out_stream::reader (void * /* cookie */,
117 char * /* buffer */,
118 size_t /* size */)
120 assert (false);
121 return 0;
125 Memory_out_stream::seeker (void *,
126 off64_t *,
127 int)
129 assert (false);
130 return 0;