]> git.donarmstrong.com Git - lilypond.git/blob - m2m/lily-stream.cc
71385ab5e7909eeda936100f03b4262ddfce41a7
[lilypond.git] / m2m / 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 "m2m.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_ = 78;
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         while ( str.length_i() ) {
35                 int max_i = wrap_column_i_ - column_i_;
36                 String line = str.left_str( max_i );
37
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;
46                         
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();
56                 }
57         }       
58         return *this;
59 }
60
61 Lily_stream&
62 Lily_stream::operator <<( Midi_event& midi_event_r )
63 {
64         midi_event_r.output_mudela( *this, false );
65         return *this;
66 }
67
68 void
69 Lily_stream::check_comment( String str )
70 {
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;
75         }
76         if ( str.index_i( '%' ) != -1 )
77                 comment_mode_bo_ = true;
78 }
79
80 void
81 Lily_stream::header()
82 {
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";    
90 }
91
92 void
93 Lily_stream::indent()
94 {
95         indent_i_++;
96         newline();
97 }
98
99 void
100 Lily_stream::newline()
101 {
102         *os_p_ << "\n" << String( '\t', indent_i_ );
103         column_i_ = indent_i_ * 8;
104         comment_mode_bo_ = false;
105 }
106
107 void
108 Lily_stream::open()
109 {
110         os_p_ = new ofstream( filename_str_ );
111         if ( !*os_p_ )
112                 error ( "can't open `" + filename_str_ + "\'", 0 );
113 }
114
115 void
116 Lily_stream::tnedni()
117 {
118         assert( indent_i_ > 0 );
119         indent_i_--;
120         newline();
121 }
122