]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-stream.cc
c7a48ad706d1ee04456b214e7bf65f7e6ac34b18
[lilypond.git] / lily / midi-stream.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Jan Nieuwenhuizen <janneke@gnu.org>
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 "midi-stream.hh"
21
22 #include <cerrno>
23 using namespace std;
24
25 #include "international.hh"
26 #include "main.hh"
27 #include "midi-chunk.hh"
28 #include "misc.hh"
29 #include "program-option.hh"
30 #include "stream.hh"
31 #include "string-convert.hh"
32 #include "warn.hh"
33
34 Midi_stream::Midi_stream (string file_name)
35 {
36   file_name_string_ = file_name;
37   out_file_ = fopen (file_name.c_str (), "wb");
38   if (!out_file_)
39     error (_f ("cannot open for write: %s: %s", file_name, strerror (errno)));
40 }
41
42 Midi_stream::~Midi_stream ()
43 {
44   fclose (out_file_);
45 }
46
47 void
48 Midi_stream::write (string str)
49 {
50   size_t sz = sizeof (Byte);
51   size_t n = str.length ();
52   size_t written = fwrite (str.data (), sz, n, out_file_);
53
54   if (written != sz * n)
55     warning (_f ("cannot write to file: `%s'", str.data ()));
56 }
57
58 void
59 Midi_stream::write (Midi_chunk const &midi)
60 {
61   string str = midi.to_string ();
62
63   return write (str);
64 }
65