lilypond-1.3.141
[lilypond.git] / midi2ly / lilypond-stream.cc
blobf7ee15839bb118e1dd8614f51c3205b02d5f8f84
1 //
2 // lilypond-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 "midi2ly-global.hh"
12 #include "lilypond-item.hh"
13 #include "lilypond-stream.hh"
14 #include "string-convert.hh"
16 extern String filename_str_g;
18 static int const INDENT_i = 8;
20 Lilypond_stream::Lilypond_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 Lilypond_stream::~Lilypond_stream ()
35 delete os_p_;
36 if (indent_i_)
37 warning (_f ("lily indent level: %d", indent_i_));
40 Lilypond_stream&
41 Lilypond_stream::operator << (char c)
43 *this << to_str (c);
44 return *this;
47 Lilypond_stream&
48 Lilypond_stream::operator << (String s)
50 static String word_sep_str = "{} \t\n";
51 while (s.length_i())
53 int i = s.index_any_i (word_sep_str) + 1;
54 if (!i)
55 i = s.length_i();
56 String word = s.left_str (i);
57 s = s.cut_str (i, s.length_i());
58 output_wrapped (word);
60 return *this;
63 Lilypond_stream&
64 Lilypond_stream::operator << (Lilypond_item& lilypond_item_r)
66 lilypond_item_r.output (*this);
67 *os_p_ << flush;
68 return *this;
71 void
72 Lilypond_stream::handle_pending_indent()
74 *os_p_ << String_convert::char_str ('\t', pending_indent_i_);
75 column_i_ += pending_indent_i_ * INDENT_i;
76 pending_indent_i_ = 0;
79 void
80 Lilypond_stream::header()
82 /* Maybe better not to translate these? */
83 *os_p_ << _ ("% Creator: ");
84 if (no_timestamps_b_g)
85 *os_p_ << "GNU LilyPond\n";
86 else
87 *os_p_ << midi2ly_version_str() << '\n';
88 *os_p_ << _ ("% Automatically generated");
89 if (no_timestamps_b_g)
90 *os_p_ << ".\n";
91 else
93 *os_p_ << _ (", at ");
94 time_t t (time (0));
95 *os_p_ << ctime (&t) << "%\n";
97 *os_p_ << _ ("% from input file: ");
98 // *os_p_ << midi_parser_l_g->filename_str_;
99 // ugh
100 *os_p_ << filename_str_g;
101 *os_p_ << "\n\n";
102 // ugh
103 *os_p_ << "\\version \"1.3.76\";\n";
106 void
107 Lilypond_stream::open()
109 os_p_ = new ofstream (filename_str_.ch_C ());
110 if (!*os_p_)
111 error (_f ("can't open file: `%s'", filename_str_));
114 void
115 Lilypond_stream::output (String str)
117 for (int i = 0; i < str.length_i(); i++)
119 char c = str[ i ];
120 switch (c)
122 case '{' :
123 case '<' :
124 handle_pending_indent();
125 if (column_i_ == indent_i_ * INDENT_i)
126 output ("\t");
127 indent_i_++;
128 *os_p_ << c;
129 column_i_++;
130 break;
131 case '}' :
132 case '>' :
133 assert (indent_i_);
134 indent_i_--;
135 if (pending_indent_i_)
136 pending_indent_i_--;
137 handle_pending_indent();
138 *os_p_ << c;
139 column_i_++;
140 break;
141 case '%' :
142 handle_pending_indent();
143 comment_mode_b_ = true;
144 *os_p_ << c;
145 column_i_++;
146 break;
147 case '\t' :
148 handle_pending_indent();
149 *os_p_ << c;
150 column_i_ += INDENT_i;
151 break;
152 case '\n' :
153 *os_p_ << endl;
154 pending_indent_i_ = indent_i_;
155 column_i_ = 0;
156 comment_mode_b_ = false;
157 break;
158 default :
159 handle_pending_indent();
160 *os_p_ << c;
161 column_i_++;
162 break;
167 void
168 Lilypond_stream::output_wrapped (String str)
170 // enough room left -> doit
171 if (column_i_ + str.length_i() <= wrap_column_i_)
173 output (str);
174 return;
177 // we're at BOL already; this will never fit -> doit
178 if (column_i_ == indent_i_ * INDENT_i)
180 output (str);
181 return;
184 // ok, let's wrap
185 // preserve comment mode
186 if (comment_mode_b_)
187 output (String ("\n%"));
188 else
189 output (String ("\n"));
191 output (str);