lilypond-0.1.16
[lilypond.git] / mi2mu / mudela-stream.cc
blob447daf98e1716f2180e78fe82025a906f9cc3695
1 //
2 // mudela-stream.cc
3 //
4 // source file of the LilyPond music typesetter
5 //
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
8 #include <assert.h>
9 #include <time.h>
10 #include <fstream.h>
11 #include "mi2mu-global.hh"
12 #include "mudela-item.hh"
13 #include "mudela-stream.hh"
15 extern String filename_str_g;
17 static int const INDENT_i = 8;
19 Mudela_stream::Mudela_stream (String filename_str)
21 filename_str_ = filename_str;
22 pending_indent_i_ = 0;
23 os_p_ = 0;
24 indent_i_ = 0;
25 comment_mode_b_ = false;
26 column_i_ = 0;
27 wrap_column_i_ = 68;
28 open();
29 header();
32 Mudela_stream::~Mudela_stream()
34 delete os_p_;
35 if (indent_i_)
36 warning ("lily indent level: " + String (indent_i_));
39 Mudela_stream&
40 Mudela_stream::operator << (String str)
42 static String word_sep_str = "{} \t\n";
43 while (str.length_i())
45 int i = str.index_any_i (word_sep_str) + 1;
46 if (!i)
47 i = str.length_i();
48 String word = str.left_str (i);
49 str = str.mid_str (i, str.length_i());
50 output_wrapped (word);
52 return *this;
55 Mudela_stream&
56 Mudela_stream::operator << (Mudela_item& mudela_item_r)
58 mudela_item_r.output (*this);
59 *os_p_ << flush;
60 return *this;
63 void
64 Mudela_stream::handle_pending_indent()
66 *os_p_ << String ('\t', pending_indent_i_);
67 column_i_ += pending_indent_i_ * INDENT_i;
68 pending_indent_i_ = 0;
71 void
72 Mudela_stream::header()
74 *os_p_ << "% Creator: " << mi2mu_version_str() << "\n";
75 *os_p_ << "% Automatically generated, at ";
76 time_t t (time (0));
77 *os_p_ << ctime (&t);
78 *os_p_ << "% from input file: ";
79 // *os_p_ << midi_parser_l_g->filename_str_;
80 // ugh
81 *os_p_ << filename_str_g;
82 *os_p_ << "\n\n";
83 // ugh
84 *os_p_ << "\\version \"0.1.2\";\n";
87 void
88 Mudela_stream::open()
90 os_p_ = new ofstream (filename_str_.ch_C ());
91 if (!*os_p_)
92 error ("can't open: `" + filename_str_ + "\'");
95 void
96 Mudela_stream::output (String str)
98 for (int i = 0; i < str.length_i(); i++)
100 char c = str[ i ];
101 switch (c)
103 case '{' :
104 case '<' :
105 handle_pending_indent();
106 if (column_i_ == indent_i_ * INDENT_i)
107 output ("\t");
108 indent_i_++;
109 *os_p_ << c;
110 column_i_++;
111 break;
112 case '}' :
113 case '>' :
114 assert (indent_i_);
115 indent_i_--;
116 if (pending_indent_i_)
117 pending_indent_i_--;
118 handle_pending_indent();
119 *os_p_ << c;
120 column_i_++;
121 break;
122 case '%' :
123 handle_pending_indent();
124 comment_mode_b_ = true;
125 *os_p_ << c;
126 column_i_++;
127 break;
128 case '\t' :
129 handle_pending_indent();
130 *os_p_ << c;
131 column_i_ += INDENT_i;
132 break;
133 case '\n' :
134 *os_p_ << endl;
135 pending_indent_i_ = indent_i_;
136 column_i_ = 0;
137 comment_mode_b_ = false;
138 break;
139 default :
140 handle_pending_indent();
141 *os_p_ << c;
142 column_i_++;
143 break;
148 void
149 Mudela_stream::output_wrapped (String str)
151 // enough room left -> doit
152 if (column_i_ + str.length_i() <= wrap_column_i_)
154 output (str);
155 return;
158 // we're at BOL already; this will never fit -> doit
159 if (column_i_ == indent_i_ * INDENT_i)
161 output (str);
162 return;
165 // ok, let's wrap
166 // preserve comment mode
167 if (comment_mode_b_)
168 output (String ("\n%"));
169 else
170 output (String ("\n"));
172 output (str);