]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-stream.cc
patch::: 1.3.113.jcn3
[lilypond.git] / lily / paper-stream.cc
1 /*
2   paper-stream.cc -- implement Paper_stream
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <fstream.h>
10
11 #include "main.hh"
12 #include "paper-stream.hh"
13 #include "debug.hh"
14
15 const int MAXLINELEN = 200;
16
17 ostream*
18 open_file_stream (String filename)
19 {
20   if (filename.length_i () && (filename != "-"))
21     os = new ofstream (filename.ch_C ());
22   else
23     os = new ostream (cout._strbuf);
24   if (!*os)
25     error (_f ("can't open file: `%s'", filename));
26   return os;
27 }
28
29 void
30 close_file_stream (ostream* os)
31 {
32   *os << flush;
33   if (!*os)
34     {
35       warning (_ ("Error syncing file (disk full?)"));
36       exit_status_i_ = 1;
37     }
38   delete os;
39 }  
40
41 Paper_stream::Paper_stream (String filename)
42 {
43   os_ = open_file_stream (filename);
44   nest_level = 0;
45   line_len_i_ = 0;
46   outputting_comment_b_=false;
47 }
48
49 Paper_stream::~Paper_stream ()
50 {
51   close_file_stream (os_);
52   assert (nest_level == 0);
53 }
54
55 // print string. don't forget indent.
56 Paper_stream&
57 Paper_stream::operator << (String s)
58 {
59   for (char const *cp = s.ch_C (); *cp; cp++)
60     {
61       if (outputting_comment_b_)
62         {
63           *os_ << *cp;
64           if (*cp == '\n')
65             {
66               outputting_comment_b_=false;
67               line_len_i_ =0;
68             }
69           continue;
70         }
71       line_len_i_ ++;
72       switch (*cp)
73         {
74         case '%':
75           outputting_comment_b_ = true;
76           *os_ << *cp;
77           break;
78         case '{':
79           nest_level++;
80           *os_ << *cp;
81           break;
82         case '}':
83           nest_level--;
84           *os_ << *cp;
85
86           if (nest_level < 0)
87             {
88               delete os_;       // we want to see the remains.
89               assert (nest_level>=0);
90             }
91
92           /* don't break line if not nested; very ugly for ps */
93           if (nest_level == 0)
94             break;
95
96           *os_ << '%';
97           break_line ();
98           break;
99         case '\n':
100           break_line ();
101           break;
102         case ' ':
103           *os_ <<  ' ';
104           if (line_len_i_ > MAXLINELEN)
105             break_line ();
106
107           break;
108         default:
109           *os_ << *cp;
110           break;
111         }
112     }
113   //urg, for debugging only!!
114   *os_ << flush;
115   return *this;
116 }
117
118 void
119 Paper_stream::break_line ()
120 {
121   *os_ << '\n';
122   *os_ << to_str (' ', nest_level);
123   outputting_comment_b_ = false;
124   line_len_i_ = 0;
125 }
126