]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-stream.cc
release: 1.1.29
[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                 /* FALLTHROUGH */
85
86             case '\n':
87                 break_line ();
88                 break;
89             case ' ':
90                 *os <<  ' ';
91                 if (line_len_i_ > MAXLINELEN)
92                    break_line ();
93
94                 break;
95             default:
96                 *os << *cp;
97                 break;
98               }
99     }
100   //urg, for debugging only!!
101   *os << flush;
102   return *this;
103 }
104
105 void
106 Paper_stream::break_line ()
107 {
108   *os << "%\n";
109   *os << to_str (' ', nest_level);
110   line_len_i_ = 0;
111 }
112