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