]> git.donarmstrong.com Git - lilypond.git/blob - midi2ly/lilypond-stream.cc
1f476649a6fd81b09d8fec5e65265a7244a76a82
[lilypond.git] / midi2ly / lilypond-stream.cc
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>
7
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"
15
16 extern String filename_str_g;
17
18 static int const INDENT_i = 8;
19
20 Lilypond_stream::Lilypond_stream (String filename_str)
21 {
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();
31 }
32
33 Lilypond_stream::~Lilypond_stream ()
34 {
35   delete os_p_;
36   if  (indent_i_)
37     warning (_f ("lily indent level: %d", indent_i_));
38 }
39
40 Lilypond_stream&
41 Lilypond_stream::operator << (char c)
42 {
43   *this << to_str (c);
44   return *this;
45 }
46
47 Lilypond_stream&
48 Lilypond_stream::operator << (String s)
49 {
50   static String word_sep_str = "{} \t\n";
51   while  (s.length_i())
52     {
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);
59     }
60   return *this;
61 }
62
63 Lilypond_stream&
64 Lilypond_stream::operator << (Lilypond_item& lilypond_item_r)
65 {
66   lilypond_item_r.output (*this);
67   *os_p_ << flush;
68   return *this;
69 }
70
71 void
72 Lilypond_stream::handle_pending_indent()
73 {
74   *os_p_ << String_convert::char_str ('\t', pending_indent_i_);
75   column_i_ += pending_indent_i_ * INDENT_i;
76   pending_indent_i_ = 0;
77 }
78
79 void
80 Lilypond_stream::header()
81 {
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
92     {
93       *os_p_ << _ (", at ");
94       time_t t (time (0));
95       *os_p_ << ctime (&t) << "%\n";
96     }
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.4.0\"\n";
104 }
105
106 void
107 Lilypond_stream::open()
108 {
109   os_p_ = new ofstream (filename_str_.ch_C ());
110   if  (!*os_p_)
111     error (_f ("can't open file: `%s'",  filename_str_));
112 }
113
114 void
115 Lilypond_stream::output (String str)
116 {
117   for  (int i = 0; i < str.length_i(); i++)
118     {
119       char c = str[ i ];
120       switch  (c)
121         {
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;
163         }
164     }
165 }
166
167 void
168 Lilypond_stream::output_wrapped (String str)
169 {
170   // enough room left -> doit
171   if  (column_i_ + str.length_i() <= wrap_column_i_)
172     {
173       output (str);
174       return;
175     }
176
177   // we're at BOL already; this will never fit -> doit
178   if  (column_i_ == indent_i_ * INDENT_i)
179     {
180       output (str);
181       return;
182     }
183
184   // ok, let's wrap
185   // preserve comment mode
186   if  (comment_mode_b_)
187     output (String ("\n%"));
188   else
189     output (String ("\n"));
190
191   output (str);
192 }