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