lilypond-1.3.130
[lilypond.git] / lily / paper-stream.cc
blob86f2ee5de8adef3145a3179a3faf91f3b5b73c02
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 ostream *
18 open_file_stream (String filename)
20 ostream *os;
21 if (filename.length_i () && (filename != "-"))
22 os = new ofstream (filename.ch_C ());
23 else
24 os = new ostream (cout._strbuf);
25 if (!*os)
26 error (_f ("can't open file: `%s'", filename));
27 return os;
30 void
31 close_file_stream (ostream *os)
33 *os << flush;
34 if (!*os)
36 warning (_ ("Error syncing file (disk full?)"));
37 exit_status_i_ = 1;
39 delete os;
42 Paper_stream::Paper_stream (String filename)
44 os_ = open_file_stream (filename);
45 nest_level = 0;
46 line_len_i_ = 0;
47 outputting_comment_b_=false;
50 Paper_stream::~Paper_stream ()
52 close_file_stream (os_);
53 assert (nest_level == 0);
56 // print string. don't forget indent.
57 Paper_stream&
58 Paper_stream::operator << (String s)
60 for (char const *cp = s.ch_C (); *cp; cp++)
62 if (outputting_comment_b_)
64 *os_ << *cp;
65 if (*cp == '\n')
67 outputting_comment_b_=false;
68 line_len_i_ =0;
70 continue;
72 line_len_i_ ++;
73 switch (*cp)
75 case '%':
76 outputting_comment_b_ = true;
77 *os_ << *cp;
78 break;
79 case '{':
80 nest_level++;
81 *os_ << *cp;
82 break;
83 case '}':
84 nest_level--;
85 *os_ << *cp;
87 if (nest_level < 0)
89 delete os_; // we want to see the remains.
90 assert (nest_level>=0);
93 /* don't break line if not nested; very ugly for ps */
94 if (nest_level == 0)
95 break;
97 *os_ << '%';
98 break_line ();
99 break;
100 case '\n':
101 break_line ();
102 break;
103 case ' ':
104 *os_ << ' ';
105 if (line_len_i_ > MAXLINELEN)
106 break_line ();
108 break;
109 default:
110 *os_ << *cp;
111 break;
114 //urg, for debugging only!!
115 *os_ << flush;
116 return *this;
119 void
120 Paper_stream::break_line ()
122 *os_ << '\n';
123 *os_ << to_str (' ', nest_level);
124 outputting_comment_b_ = false;
125 line_len_i_ = 0;