]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-stream.cc
2f791019ad5c7550a1bbd3669a0d1d7d40e9b2a4
[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--1999 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 Paper_stream::Paper_stream (String filename)
18 {
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=false;
29 }
30
31 Paper_stream::~Paper_stream ()
32 {
33   *os << flush;
34   if (!*os)
35     {
36       warning (_ ("Error syncing file (disk full?)"));
37       exit_status_i_ = 1;
38     }
39   delete os;
40   assert (nest_level == 0);
41 }
42
43 // print string. don't forget indent.
44 Paper_stream&
45 Paper_stream::operator << (Scalar s)
46 {
47   for (char const *cp = s.ch_C (); *cp; cp++)
48     {
49       if (outputting_comment)
50         {
51           *os << *cp;
52           if (*cp == '\n')
53             {
54               outputting_comment=false;
55
56             }
57           continue;
58         }
59       line_len_i_ ++;
60       switch (*cp)
61         {
62         case '%':
63           outputting_comment = true;
64           *os << *cp;
65           break;
66         case '{':
67           nest_level++;
68           *os << *cp;
69           break;
70         case '}':
71           nest_level--;
72           *os << *cp;
73
74           if (nest_level < 0)
75             {
76               delete os;        // we want to see the remains.
77               assert (nest_level>=0);
78             }
79
80           /* don't break line if not nested; very ugly for ps */
81           if (nest_level == 0)
82             break;
83
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 ();
94
95           break;
96         default:
97           *os << *cp;
98           break;
99         }
100     }
101   //urg, for debugging only!!
102   *os << flush;
103   return *this;
104 }
105
106 void
107 Paper_stream::break_line ()
108 {
109   *os << '\n';
110   *os << to_str (' ', nest_level);
111   line_len_i_ = 0;
112 }
113