lilypond-1.1.5
[lilypond.git] / m2m / lily-stream.cc
blob71385ab5e7909eeda936100f03b4262ddfce41a7
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 "m2m.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_bo_ = false;
18 column_i_ = 0;
19 wrap_column_i_ = 78;
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_ ), 0 );
31 Lily_stream&
32 Lily_stream::operator <<( String str )
34 while ( str.length_i() ) {
35 int max_i = wrap_column_i_ - column_i_;
36 String line = str.left_str( max_i );
38 while ( max_i && ( max_i < line.length_i() )
39 && ( isalnum( line[ max_i ] )
40 || ( line[ max_i ] == '\\' ) ) )
41 max_i--;
42 if ( max_i )
43 line = str.left_str( max_i + 1 );
44 else // cannot break neatly...
45 max_i = wrap_column_i_ - column_i_ - 1;
47 str = str.mid_str( max_i + 1, INT_MAX );
48 *os_p_ << line;
49 check_comment( line );
50 column_i_ += line.length_i();
51 if ( column_i_ >= wrap_column_i_ ) {
52 //brr.
53 if ( comment_mode_bo_ )
54 str = "%" + str;
55 newline();
58 return *this;
61 Lily_stream&
62 Lily_stream::operator <<( Midi_event& midi_event_r )
64 midi_event_r.output_mudela( *this, false );
65 return *this;
68 void
69 Lily_stream::check_comment( String str )
71 int newline_i = str.index_last_i( '\n' );
72 if ( newline_i != -1 ) {
73 str = str.mid_str( newline_i +1, INT_MAX );
74 comment_mode_bo_ = false;
76 if ( str.index_i( '%' ) != -1 )
77 comment_mode_bo_ = true;
80 void
81 Lily_stream::header()
83 *os_p_ << "% Creator: " << version_str() << "\n";
84 *os_p_ << "% Automatically generated, at ";
85 time_t t( time( 0 ) );
86 *os_p_ << ctime( &t );
87 *os_p_ << "% from input file: ";
88 *os_p_ << midi_parser_l_g->filename_str_;
89 *os_p_ << "\n\n";
92 void
93 Lily_stream::indent()
95 indent_i_++;
96 newline();
99 void
100 Lily_stream::newline()
102 *os_p_ << "\n" << String( '\t', indent_i_ );
103 column_i_ = indent_i_ * 8;
104 comment_mode_bo_ = false;
107 void
108 Lily_stream::open()
110 os_p_ = new ofstream( filename_str_ );
111 if ( !*os_p_ )
112 error ( "can't open `" + filename_str_ + "\'", 0 );
115 void
116 Lily_stream::tnedni()
118 assert( indent_i_ > 0 );
119 indent_i_--;
120 newline();