lilypond-1.3.67
[lilypond.git] / lily / paper-stream.cc
blobcaab11e504165cba9cb576816bed4422407a7e35
1 /*
2 paper-stream.cc -- implement Paper_stream
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <fstream.h>
11 #include "main.hh"
12 #include "paper-stream.hh"
13 #include "debug.hh"
15 const int MAXLINELEN = 200;
17 Paper_stream::Paper_stream (String filename)
19 if (filename.length_i () && (filename != "-"))
20 os = new ofstream (filename.ch_C ());
21 else
22 // os = new ostream (cout.ostreambuf ());
23 os = new ostream (cout._strbuf);
24 if (!*os)
25 error (_f ("can't open file: `%s'", filename));
26 nest_level = 0;
27 line_len_i_ = 0;
28 outputting_comment_b_=false;
31 Paper_stream::~Paper_stream ()
33 *os << flush;
34 if (!*os)
36 warning (_ ("Error syncing file (disk full?)"));
37 exit_status_i_ = 1;
39 delete os;
40 assert (nest_level == 0);
43 // print string. don't forget indent.
44 Paper_stream&
45 Paper_stream::operator << (String s)
47 for (char const *cp = s.ch_C (); *cp; cp++)
49 if (outputting_comment_b_)
51 *os << *cp;
52 if (*cp == '\n')
54 outputting_comment_b_=false;
55 line_len_i_ =0;
57 continue;
59 line_len_i_ ++;
60 switch (*cp)
62 case '%':
63 outputting_comment_b_ = true;
64 *os << *cp;
65 break;
66 case '{':
67 nest_level++;
68 *os << *cp;
69 break;
70 case '}':
71 nest_level--;
72 *os << *cp;
74 if (nest_level < 0)
76 delete os; // we want to see the remains.
77 assert (nest_level>=0);
80 /* don't break line if not nested; very ugly for ps */
81 if (nest_level == 0)
82 break;
84 *os << '%';
85 break_line ();
86 break;
87 case '\n':
88 break_line ();
89 break;
90 case ' ':
91 *os << ' ';
92 if (line_len_i_ > MAXLINELEN)
93 break_line ();
95 break;
96 default:
97 *os << *cp;
98 break;
101 //urg, for debugging only!!
102 *os << flush;
103 return *this;
106 void
107 Paper_stream::break_line ()
109 *os << '\n';
110 *os << to_str (' ', nest_level);
111 outputting_comment_b_ = false;
112 line_len_i_ = 0;