lilypond-1.1.63
[lilypond.git] / mi2mu / lily-stream.cc
blob91824e20988593150d835ec14a92c8a0c21e757f
1 //
2 // lily-stream.cc
3 //
4 // source file of the LilyPond music typesetter
5 //
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
8 // should i be named Mudela_stream?
10 #include "mi2mu.hh"
12 Lily_stream::Lily_stream( String filename_str )
14 filename_str_ = filename_str;
15 os_p_ = 0;
16 indent_i_ = 0;
17 comment_mode_b_ = false;
18 column_i_ = 0;
19 wrap_column_i_ = 60;
20 open();
21 header();
24 Lily_stream::~Lily_stream()
26 delete os_p_;
27 if ( indent_i_ )
28 warning( "lily indent level: " + String( indent_i_ ));
31 Lily_stream&
32 Lily_stream::operator <<( String str )
34 static String word_sep_str = "{} \t\n";
35 while ( str.length_i() ) {
36 int i = str.index_any_i( word_sep_str ) + 1;
37 if ( !i )
38 i = str.length_i();
39 String word = str.left_str( i );
40 str = str.mid_str( i, str.length_i() );
41 output_wrapped( word );
43 return *this;
46 Lily_stream&
47 Lily_stream::operator <<( Midi_event& midi_event_r )
49 midi_event_r.output_mudela( *this, false );
50 *os_p_ << flush;
51 return *this;
54 void
55 Lily_stream::header()
57 *os_p_ << "% Creator: " << mi2mu_version_str() << "\n";
58 *os_p_ << "% Automatically generated, at ";
59 time_t t( time( 0 ) );
60 *os_p_ << ctime( &t );
61 *os_p_ << "% from input file: ";
62 *os_p_ << midi_parser_l_g->filename_str_;
63 *os_p_ << "\n\n";
64 // ugh
65 *os_p_ << "\\version \"0.1.0\";\n";
68 void
69 Lily_stream::open()
71 os_p_ = new ofstream( filename_str_ );
72 if ( !*os_p_ )
73 error ( "can't open `" + filename_str_ + "\'");
76 void
77 Lily_stream::output( String str )
79 for ( int i = 0; i < str.length_i(); i++ ) {
80 char c = str[ i ];
81 switch ( c ) {
82 case '{' :
83 case '<' :
84 indent_i_++;
85 column_i_++;
86 *os_p_ << c;
87 break;
88 case '}' :
89 case '>' :
90 assert( indent_i_ );
91 indent_i_--;
92 column_i_++;
93 *os_p_ << c;
94 break;
95 case '%' :
96 comment_mode_b_ = true;
97 *os_p_ << c;
98 column_i_++;
99 break;
100 case '\t' :
101 column_i_ += 8;
102 *os_p_ << c;
103 break;
104 case '\n' :
105 *os_p_ << endl;
106 *os_p_ << String( '\t', indent_i_ );
107 column_i_ = indent_i_ * 8;
108 comment_mode_b_ = false;
109 break;
110 default :
111 column_i_++;
112 *os_p_ << c;
113 break;
118 void
119 Lily_stream::output_wrapped( String str )
121 // enough room left -> doit
122 if ( column_i_ + str.length_i() <= wrap_column_i_ ) {
123 output( str );
124 return;
127 // we're at BOL already; this will never fit -> doit
128 if ( column_i_ == indent_i_ * 8 ) {
129 output( str );
130 return;
133 // ok, let's wrap
134 // preserve comment mode
135 if ( comment_mode_b_ )
136 output( String( "\n%" ) );
137 else
138 output( String( "\n" ) );
140 output( str );