]> git.donarmstrong.com Git - lilypond.git/blob - flower/memory-stream.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / flower / memory-stream.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include <cassert>
21 #include <cstring>
22 #include <cstdlib>
23 using namespace std;
24
25 #include "memory-stream.hh"
26
27 /*
28   TODO: add read support as well.
29 */
30 const int Memory_out_stream::block_size_ = 1024;
31
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
39 };
40
41 int
42 Memory_out_stream::cleaner (void *cookie)
43 {
44   Memory_out_stream *stream = (Memory_out_stream *) cookie;
45
46   stream->file_ = 0;
47   return 0;
48 }
49
50 Memory_out_stream::Memory_out_stream ()
51 {
52   size_ = 0;
53   buffer_ = 0;
54   buffer_blocks_ = 0;
55   file_ = 0;
56
57 #if 0
58   file_ = fopencookie ((void *) this, "w", functions_);
59 #endif
60 }
61
62 Memory_out_stream::~Memory_out_stream ()
63 {
64   if (file_)
65     fclose (file_);
66
67   free (buffer_);
68 }
69
70 FILE *
71 Memory_out_stream::get_file () const
72 {
73   return file_;
74 }
75
76 ssize_t
77 Memory_out_stream::get_length () const
78 {
79   return size_;
80 }
81
82 char const *
83 Memory_out_stream::get_string () const
84 {
85   return buffer_;
86 }
87
88 ssize_t
89 Memory_out_stream::writer (void *cookie,
90                            char const *buffer,
91                            size_t size)
92 {
93   Memory_out_stream *stream = (Memory_out_stream *) cookie;
94
95   ssize_t newsize = stream->size_ + size;
96
97   bool change = false;
98   while (newsize > stream->buffer_blocks_ * block_size_)
99     {
100       stream->buffer_blocks_ *= 2;
101       stream->buffer_blocks_ += 1;
102       change = true;
103     }
104
105   if (change)
106     stream->buffer_ = (char *) realloc (stream->buffer_,
107                                         stream->buffer_blocks_ * block_size_);
108
109   memcpy (stream->buffer_ + stream->size_, buffer, size);
110   stream->size_ = newsize;
111
112   return size;
113 }
114
115 ssize_t
116 Memory_out_stream::reader (void * /* cookie */,
117                            char * /* buffer */,
118                            size_t /* size */)
119 {
120   assert (false);
121   return 0;
122 }
123
124 int
125 Memory_out_stream::seeker (void *,
126                            off64_t *,
127                            int)
128 {
129   assert (false);
130   return 0;
131 }