]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-stream.cc
Handle unwritable midi file.
[lilypond.git] / lily / midi-stream.cc
1 /*
2   midi-stream.cc -- implement Midi_stream
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "midi-stream.hh"
10
11 #include "stream.hh"
12 #include "string-convert.hh"
13 #include "main.hh"
14 #include "misc.hh"
15 #include "midi-item.hh"
16 #include "warn.hh"
17 #include "scm-option.hh"
18
19 Midi_stream::Midi_stream (String file_name)
20 {
21   file_name_string_ = file_name;
22   out_file_ = fopen (file_name.to_str0 (), "wb");
23   if (!out_file_)
24     error (_f ("can't open for write: %s: %s", file_name, strerror (errno)));
25 }
26
27 Midi_stream::~Midi_stream ()
28 {
29   fclose (out_file_);
30 }
31
32 Midi_stream &
33 Midi_stream::operator << (String str)
34 {
35   size_t sz = sizeof (Byte);
36   size_t n = str.length ();
37   size_t written = fwrite (str.get_bytes (),
38                            sz, n, out_file_);
39
40   if (written != sz * n)
41     warning (_ ("could not write file: `%s'"));
42
43   return *this;
44 }
45
46 Midi_stream &
47 Midi_stream::operator << (Midi_item const &midi_c_r)
48 {
49   String str = midi_c_r.to_string ();
50
51   // ugh, should have separate debugging output with Midi*::print routines
52   if (midi_debug_global_b)
53     {
54       str = String_convert::bin2hex (str) + "\n";
55       for (int i = str.index ("0a"); i >= 0; i = str.index ("0a"))
56         {
57           str[i] = '\n';
58           str[i + 1] = '\t';
59         }
60     }
61
62   return operator << (str);
63 }
64
65 Midi_stream&
66 Midi_stream::operator << (int i)
67 {
68   // output binary string ourselves
69   *this << Midi_item::i2varint_string (i);
70   return *this;
71 }
72