summaryrefslogtreecommitdiff
path: root/flower/memory-stream.cc
blob: b034f85e5e8b35622121d70bbf3b9c83612e581f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>

  LilyPond is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LilyPond is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <cassert>
#include <cstring>
#include <cstdlib>
using namespace std;

#include "memory-stream.hh"

/*
  TODO: add read support as well.
*/
const int Memory_out_stream::block_size_ = 1024;

lily_cookie_io_functions_t
Memory_out_stream::functions_
=
{
  Memory_out_stream::reader,
  Memory_out_stream::writer,
  Memory_out_stream::seeker,
  Memory_out_stream::cleaner
};

ssize_t
Memory_out_stream::cleaner (void *cookie)
{
  Memory_out_stream *stream = (Memory_out_stream *) cookie;

  stream->file_ = 0;
  return 0;
}

Memory_out_stream::Memory_out_stream ()
{
  size_ = 0;
  buffer_ = 0;
  buffer_blocks_ = 0;
  file_ = 0;

#if 0
  file_ = fopencookie ((void *) this, "w", functions_);
#endif
}

Memory_out_stream::~Memory_out_stream ()
{
  if (file_)
    fclose (file_);

  free (buffer_);
}

FILE *
Memory_out_stream::get_file () const
{
  return file_;
}

ssize_t
Memory_out_stream::get_length () const
{
  return size_;
}

char const *
Memory_out_stream::get_string () const
{
  return buffer_;
}

ssize_t
Memory_out_stream::writer (void *cookie,
                           char const *buffer,
                           size_t size)
{
  Memory_out_stream *stream = (Memory_out_stream *) cookie;

  ssize_t newsize = stream->size_ + (ssize_t) size;

  bool change = false;
  while (newsize > stream->buffer_blocks_ * block_size_)
    {
      stream->buffer_blocks_ *= 2;
      stream->buffer_blocks_ += 1;
      change = true;
    }

  if (change)
    stream->buffer_ = (char *) realloc (stream->buffer_,
                                        (size_t) (stream->buffer_blocks_ * block_size_));

  memcpy (stream->buffer_ + stream->size_, buffer, size);
  stream->size_ = newsize;

  return (ssize_t) size;
}

ssize_t
Memory_out_stream::reader (void * /* cookie */,
                           char * /* buffer */,
                           size_t /* size */)
{
  assert (false);
  return 0;
}

ssize_t
Memory_out_stream::seeker (void *,
                           off64_t *,
                           size_t)
{
  assert (false);
  return 0;
}