]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/lily-stream.cc
da6890aa9756bdbd6ab7aeaa37653ff880dddc23
[lilypond.git] / mi2mu / lily-stream.cc
1 //
2 // lily-stream.cc
3 //
4 // source file of the LilyPond music typesetter
5 //
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
7
8 // should i be named Mudela_stream?
9
10 #include "mi2mu.hh"
11
12 Lily_stream::Lily_stream( String filename_str )
13 {
14         filename_str_ = filename_str;
15         os_p_ = 0;
16         indent_i_ = 0;
17         comment_mode_bo_ = false;
18         column_i_ = 0;
19         wrap_column_i_ = 60;
20         open();
21         header();
22 }
23
24 Lily_stream::~Lily_stream()
25 {
26         delete os_p_;
27         if ( indent_i_ )
28                 warning( "lily indent level: " + String( indent_i_ ), 0 );
29 }
30
31 Lily_stream&
32 Lily_stream::operator <<( String str )
33 {
34         static String nobreak_str = "\\`'_-.^<>*@";
35         while ( str.length_i() ) {
36                 int max_i = wrap_column_i_ - column_i_ - 1;
37                 int i = str.length_i() - 1 <? max_i;
38                 int nl_i = str.left_str( i + 1 ).index_i( '\n' );
39                 if ( nl_i != -1 ) {
40                         i = nl_i - 1;
41                         str = str.nomid_str( nl_i, 1 );
42                 }
43
44                 if ( ( i != str.length_i() - 1 ) && ( nl_i == -1 ) ) {
45                         while ( i && ( isalnum( str[ i ] ) 
46                                 || ( nobreak_str.index_i( str[ i ] ) != -1 ) ) )
47                                 i--;
48
49                         if ( !i ) { // no room left
50                                 if ( column_i_ > 8 * indent_i_ ) {
51                                         newline();
52                                         if ( comment_mode_bo_ && ( str[ 0 ] != '%' ) )
53                                                 str = '%' + str;
54                                         continue;
55                                 }
56                                 else { // cannot break neatly...
57                                         i = max_i;
58                                 }
59                         }
60                 }
61                                 
62                 String line = str.left_str( i + 1 ); 
63                 str = str.mid_str( i + 1, INT_MAX );
64                 *os_p_ << line;
65                 column_i_ += line.length_i();
66                 if ( nl_i != -1 )
67                          newline();
68                 else
69                         check_comment( line );
70                 if ( ( str.length_i() && ( nl_i == -1 ) ) || ( column_i_ >= wrap_column_i_ ) ) {
71                         //brr.
72                         if ( comment_mode_bo_ )
73                                 str = "%" + str;
74                         newline();
75                 }
76         }       
77         return *this;
78 }
79
80 Lily_stream&
81 Lily_stream::operator <<( Midi_event& midi_event_r )
82 {
83         midi_event_r.output_mudela( *this, false );
84         *os_p_ << flush;
85         return *this;
86 }
87
88 void
89 Lily_stream::check_comment( String str )
90 {
91         int newline_i = str.index_last_i( '\n' );
92         if ( newline_i != -1 ) {
93                 str = str.mid_str( newline_i +1, INT_MAX );
94                 comment_mode_bo_ = false;
95         }
96         if ( str.index_i( '%' ) != -1 )
97                 comment_mode_bo_ = true;
98 }
99
100 void
101 Lily_stream::header()
102 {
103         *os_p_ << "% Creator: " << version_str() << "\n";
104         *os_p_ << "% Automatically generated, at ";
105         time_t t( time( 0 ) );
106         *os_p_ << ctime( &t );
107         *os_p_ << "% from input file: ";
108         *os_p_ << midi_parser_l_g->filename_str_;
109         *os_p_ << "\n\n";    
110 }
111
112 void
113 Lily_stream::indent()
114 {
115         indent_i_++;
116         newline();
117 }
118
119 void
120 Lily_stream::newline()
121 {
122         *os_p_ << endl << String( '\t', indent_i_ );
123         column_i_ = indent_i_ * 8;
124         comment_mode_bo_ = false;
125 }
126
127 void
128 Lily_stream::open()
129 {
130         os_p_ = new ofstream( filename_str_ );
131         if ( !*os_p_ )
132                 error ( "can't open `" + filename_str_ + "\'", 0 );
133 }
134
135 void
136 Lily_stream::tnedni()
137 {
138         assert( indent_i_ > 0 );
139         indent_i_--;
140         newline();
141 }
142