]> git.donarmstrong.com Git - lilypond.git/blob - flower/memory-stream.cc
Web-ja: update introduction
[lilypond.git] / flower / memory-stream.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 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 {
36   Memory_out_stream::reader,
37   Memory_out_stream::writer,
38   Memory_out_stream::seeker,
39   Memory_out_stream::cleaner
40 };
41
42 ssize_t
43 Memory_out_stream::cleaner (void *cookie)
44 {
45   Memory_out_stream *stream = (Memory_out_stream *) cookie;
46
47   stream->file_ = 0;
48   return 0;
49 }
50
51 Memory_out_stream::Memory_out_stream ()
52 {
53   size_ = 0;
54   buffer_ = 0;
55   buffer_blocks_ = 0;
56   file_ = 0;
57
58 #if 0
59   file_ = fopencookie ((void *) this, "w", functions_);
60 #endif
61 }
62
63 Memory_out_stream::~Memory_out_stream ()
64 {
65   if (file_)
66     fclose (file_);
67
68   free (buffer_);
69 }
70
71 FILE *
72 Memory_out_stream::get_file () const
73 {
74   return file_;
75 }
76
77 ssize_t
78 Memory_out_stream::get_length () const
79 {
80   return size_;
81 }
82
83 char const *
84 Memory_out_stream::get_string () const
85 {
86   return buffer_;
87 }
88
89 ssize_t
90 Memory_out_stream::writer (void *cookie,
91                            char const *buffer,
92                            size_t size)
93 {
94   Memory_out_stream *stream = (Memory_out_stream *) cookie;
95
96   ssize_t newsize = stream->size_ + (ssize_t) size;
97
98   bool change = false;
99   while (newsize > stream->buffer_blocks_ * block_size_)
100     {
101       stream->buffer_blocks_ *= 2;
102       stream->buffer_blocks_ += 1;
103       change = true;
104     }
105
106   if (change)
107     stream->buffer_ = (char *) realloc (stream->buffer_,
108                                         (size_t) (stream->buffer_blocks_ * block_size_));
109
110   memcpy (stream->buffer_ + stream->size_, buffer, size);
111   stream->size_ = newsize;
112
113   return (ssize_t) size;
114 }
115
116 ssize_t
117 Memory_out_stream::reader (void * /* cookie */,
118                            char * /* buffer */,
119                            size_t /* size */)
120 {
121   assert (false);
122   return 0;
123 }
124
125 ssize_t
126 Memory_out_stream::seeker (void *,
127                            off64_t *,
128                            size_t)
129 {
130   assert (false);
131   return 0;
132 }