]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/mudela-stream.cc
release: 0.1.54
[lilypond.git] / mi2mu / mudela-stream.cc
1 //
2 // mudela-stream.cc
3 //
4 // source file of the LilyPond music typesetter
5 //
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
7
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
15 extern String filename_str_g;
16
17 static int const INDENT_i = 8;
18
19 Mudela_stream::Mudela_stream (String filename_str)
20 {
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();
30 }
31
32 Mudela_stream::~Mudela_stream()
33 {
34   delete os_p_;
35   if  (indent_i_)
36     warning (_("lily indent level: ") + String (indent_i_));
37 }
38
39 Mudela_stream&
40 Mudela_stream::operator << (String str)
41 {
42   static String word_sep_str = "{} \t\n";
43   while  (str.length_i())
44     {
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.cut (i, str.length_i());
50       output_wrapped (word);
51     }
52   return *this;
53 }
54
55 Mudela_stream&
56 Mudela_stream::operator << (Mudela_item& mudela_item_r)
57 {
58   mudela_item_r.output (*this);
59   *os_p_ << flush;
60   return *this;
61 }
62
63 void
64 Mudela_stream::handle_pending_indent()
65 {
66   *os_p_ << String ('\t', pending_indent_i_);
67   column_i_ += pending_indent_i_ * INDENT_i;
68   pending_indent_i_ = 0;
69 }
70
71 void
72 Mudela_stream::header()
73 {
74   *os_p_ << _("% Creator: ");
75   if (no_timestamps_b_g)
76     *os_p_ << "GNU LilyPond\n"; 
77   else
78     *os_p_ << mi2mu_version_str() << "\n";
79   *os_p_ << _("% Automatically generated");
80   if (no_timestamps_b_g)
81     *os_p_ << ".\n";
82   else
83     {
84       *os_p_ << _(", at ");
85       time_t t (time (0));
86       *os_p_ << ctime (&t) << "%\n";
87     }
88   *os_p_ << _("% from input file: ");
89   //  *os_p_ << midi_parser_l_g->filename_str_;
90   // ugh
91   *os_p_ << filename_str_g;
92   *os_p_ << "\n\n";
93   // ugh
94   *os_p_ << "\\version \"0.1.9\";\n";
95 }
96
97 void
98 Mudela_stream::open()
99 {
100   os_p_ = new ofstream (filename_str_.ch_C ());
101   if  (!*os_p_)
102     error  (_("can't open: `") + filename_str_ + "\'");
103 }
104
105 void
106 Mudela_stream::output (String str)
107 {
108   for  (int i = 0; i < str.length_i(); i++)
109     {
110       char c = str[ i ];
111       switch  (c)
112         {
113         case '{' :
114         case '<' :
115           handle_pending_indent();
116           if  (column_i_ == indent_i_ * INDENT_i)
117             output ("\t");
118           indent_i_++;
119           *os_p_ << c;
120           column_i_++;
121           break;
122         case '}' :
123         case '>' :
124           assert (indent_i_);
125           indent_i_--;
126           if  (pending_indent_i_)
127             pending_indent_i_--;
128           handle_pending_indent();
129           *os_p_ << c;
130           column_i_++;
131           break;
132         case '%' :
133           handle_pending_indent();
134           comment_mode_b_ = true;
135           *os_p_ << c;
136           column_i_++;
137           break;
138         case '\t' :
139           handle_pending_indent();
140           *os_p_ << c;
141           column_i_ += INDENT_i;
142           break;
143         case '\n' :
144           *os_p_ << endl;
145           pending_indent_i_ = indent_i_;
146           column_i_ = 0;
147           comment_mode_b_ = false;
148           break;
149         default :
150           handle_pending_indent();
151           *os_p_ << c;
152           column_i_++;
153           break;
154         }
155     }
156 }
157
158 void
159 Mudela_stream::output_wrapped (String str)
160 {
161   // enough room left -> doit
162   if  (column_i_ + str.length_i() <= wrap_column_i_)
163     {
164       output (str);
165       return;
166     }
167
168   // we're at BOL already; this will never fit -> doit
169   if  (column_i_ == indent_i_ * INDENT_i)
170     {
171       output (str);
172       return;
173     }
174
175   // ok, let's wrap
176   // preserve comment mode
177   if  (comment_mode_b_)
178     output (String ("\n%"));
179   else
180     output (String ("\n"));
181
182   output (str);
183 }