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