]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-stream.cc
72d14a69b520e8781f192a2b08454207674f948d
[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--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <fstream.h>
10 #include <time.h>
11
12 #include "main.hh"
13 #include "paper-stream.hh"
14 #include "debug.hh"
15
16 const int MAXLINELEN = 200;
17
18 Paper_stream::Paper_stream (String filename)
19 {
20   if (filename.length_i () && (filename != "-"))
21     os = new ofstream (filename.ch_C ());
22   else
23 //    os = new ostream (cout.ostreambuf ());
24     os = new ostream (cout._strbuf);
25   if (!*os)
26     error (_f ("can't open file: `%s\'", filename));
27   nest_level = 0;
28   line_len_i_ = 0;
29   outputting_comment=false;
30   //  header ();
31 }
32
33 Paper_stream::~Paper_stream ()
34 {
35   *os << flush;
36   if (!*os)
37     {
38       warning (_ ("error syncing file (disk full?)"));
39       exit_status_i_ = 1;
40     }
41   delete os;
42   assert (nest_level == 0);
43 }
44
45 // print string. don't forget indent.
46 Paper_stream&
47 Paper_stream::operator << (Scalar s)
48 {
49   for (char const *cp = s.ch_C (); *cp; cp++)
50     {
51         if (outputting_comment)
52           {
53             *os << *cp;
54             if (*cp == '\n')
55               {
56                 outputting_comment=false;
57
58               }
59             continue;
60           }
61         line_len_i_ ++;
62         switch (*cp)
63             {
64             case '%':
65                 outputting_comment = true;
66                 *os << *cp;
67                 break;
68             case '{':
69                 nest_level++;
70                 *os << *cp;
71                 break;
72             case '}':
73                 nest_level--;
74                 *os << *cp;
75
76                 if (nest_level < 0)
77                   {
78                     delete os;  // we want to see the remains.
79                     assert (nest_level>=0);
80                   }
81
82                 /* don't break line if not nested; very ugly for ps */
83                 if (nest_level == 0)
84                   break;
85
86                 /* FALLTHROUGH */
87
88             case '\n':
89                 break_line ();
90                 break;
91             case ' ':
92                 *os <<  ' ';
93                 if (line_len_i_ > MAXLINELEN)
94                    break_line ();
95
96                 break;
97             default:
98                 *os << *cp;
99                 break;
100               }
101     }
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