lilypond-1.1.63
[lilypond.git] / mi2mu / mudela-stream.cc
blob5bdfd11e253333ae1489c022e7bd1ac13c2f5742
1 //
2 // mudela-stream.cc
3 //
4 // source file of the LilyPond music typesetter
5 //
6 // (c) 1997--1998, 1998 Jan Nieuwenhuizen <janneke@gnu.org>
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"
14 #include "string-convert.hh"
16 extern String filename_str_g;
18 static int const INDENT_i = 8;
20 Mudela_stream::Mudela_stream (String filename_str)
22 filename_str_ = filename_str;
23 pending_indent_i_ = 0;
24 os_p_ = 0;
25 indent_i_ = 0;
26 comment_mode_b_ = false;
27 column_i_ = 0;
28 wrap_column_i_ = 68;
29 open();
30 header();
33 Mudela_stream::~Mudela_stream()
35 delete os_p_;
36 if (indent_i_)
37 warning (_f ("lily indent level: %d", indent_i_));
40 Mudela_stream&
41 Mudela_stream::operator << (Scalar s)
43 static String word_sep_str = "{} \t\n";
44 while (s.length_i())
46 int i = s.index_any_i (word_sep_str) + 1;
47 if (!i)
48 i = s.length_i();
49 String word = s.left_str (i);
50 s = s.cut_str (i, s.length_i());
51 output_wrapped (word);
53 return *this;
56 Mudela_stream&
57 Mudela_stream::operator << (Mudela_item& mudela_item_r)
59 mudela_item_r.output (*this);
60 *os_p_ << flush;
61 return *this;
64 void
65 Mudela_stream::handle_pending_indent()
67 *os_p_ << String_convert::char_str ('\t', pending_indent_i_);
68 column_i_ += pending_indent_i_ * INDENT_i;
69 pending_indent_i_ = 0;
72 void
73 Mudela_stream::header()
75 *os_p_ << _ ("% Creator: ");
76 if (no_timestamps_b_g)
77 *os_p_ << "GNU LilyPond\n";
78 else
79 *os_p_ << mi2mu_version_str() << '\n';
80 *os_p_ << _ ("% Automatically generated");
81 if (no_timestamps_b_g)
82 *os_p_ << ".\n";
83 else
85 *os_p_ << _ (", at ");
86 time_t t (time (0));
87 *os_p_ << ctime (&t) << "%\n";
89 *os_p_ << _ ("% from input file: ");
90 // *os_p_ << midi_parser_l_g->filename_str_;
91 // ugh
92 *os_p_ << filename_str_g;
93 *os_p_ << "\n\n";
94 // ugh
95 *os_p_ << "\\version \"1.0.14\";\n";
98 void
99 Mudela_stream::open()
101 os_p_ = new ofstream (filename_str_.ch_C ());
102 if (!*os_p_)
103 error (_f ("can't open file: `%s\'", filename_str_));
106 void
107 Mudela_stream::output (String str)
109 for (int i = 0; i < str.length_i(); i++)
111 char c = str[ i ];
112 switch (c)
114 case '{' :
115 case '<' :
116 handle_pending_indent();
117 if (column_i_ == indent_i_ * INDENT_i)
118 output ("\t");
119 indent_i_++;
120 *os_p_ << c;
121 column_i_++;
122 break;
123 case '}' :
124 case '>' :
125 assert (indent_i_);
126 indent_i_--;
127 if (pending_indent_i_)
128 pending_indent_i_--;
129 handle_pending_indent();
130 *os_p_ << c;
131 column_i_++;
132 break;
133 case '%' :
134 handle_pending_indent();
135 comment_mode_b_ = true;
136 *os_p_ << c;
137 column_i_++;
138 break;
139 case '\t' :
140 handle_pending_indent();
141 *os_p_ << c;
142 column_i_ += INDENT_i;
143 break;
144 case '\n' :
145 *os_p_ << endl;
146 pending_indent_i_ = indent_i_;
147 column_i_ = 0;
148 comment_mode_b_ = false;
149 break;
150 default :
151 handle_pending_indent();
152 *os_p_ << c;
153 column_i_++;
154 break;
159 void
160 Mudela_stream::output_wrapped (String str)
162 // enough room left -> doit
163 if (column_i_ + str.length_i() <= wrap_column_i_)
165 output (str);
166 return;
169 // we're at BOL already; this will never fit -> doit
170 if (column_i_ == indent_i_ * INDENT_i)
172 output (str);
173 return;
176 // ok, let's wrap
177 // preserve comment mode
178 if (comment_mode_b_)
179 output (String ("\n%"));
180 else
181 output (String ("\n"));
183 output (str);