]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-stream.cc
ba3c9ee11555880dc806fe16f87385f5a687d562
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <fstream.h>
12
13 #include "config.h"
14 #if HAVE_SYS_STAT_H 
15 #include <sys/stat.h>
16 #endif
17
18 #include "main.hh"
19 #include "paper-stream.hh"
20 #include "file-path.hh"
21 #include "debug.hh"
22
23 const int MAXLINELEN = 200;
24
25 #if __GNUC__ > 2
26 ostream *
27 open_file_stream (String filename, std::ios_base::openmode mode)
28 #else
29 ostream *
30 open_file_stream (String filename, int mode)
31 #endif
32 {
33   ostream *os;
34   if ((filename == "-"))
35     os = &cout;
36   else
37     {
38       Path p = split_path (filename);
39       if (!p.dir.empty_b ())
40         if (mkdir (p.dir.ch_C (), 0777) == -1 && errno != EEXIST)
41           error (_f ("can't create directory: `%s'", p.dir));
42       os = new ofstream (filename.ch_C (), mode);
43     }
44   if (!*os)
45     error (_f ("can't open file: `%s'", filename));
46   return os;
47 }
48
49 void
50 close_file_stream (ostream *os)
51 {
52   *os << flush;
53   if (!*os)
54     {
55       warning (_ ("Error syncing file (disk full?)"));
56       exit_status_global = 1;
57     }
58   if (os != &cout)
59     delete os;
60   os = 0;
61 }  
62
63 Paper_stream::Paper_stream (String filename)
64 {
65   os_ = open_file_stream (filename);
66   nest_level = 0;
67   line_len_i_ = 0;
68   outputting_comment_b_=false;
69 }
70
71 Paper_stream::~Paper_stream ()
72 {
73   close_file_stream (os_);
74   assert (nest_level == 0);
75 }
76
77 // print string. don't forget indent.
78 Paper_stream&
79 Paper_stream::operator << (String s)
80 {
81   for (char const *cp = s.ch_C (); *cp; cp++)
82     {
83       if (outputting_comment_b_)
84         {
85           *os_ << *cp;
86           if (*cp == '\n')
87             {
88               outputting_comment_b_=false;
89               line_len_i_ =0;
90             }
91           continue;
92         }
93       line_len_i_ ++;
94       switch (*cp)
95         {
96         case '%':
97           outputting_comment_b_ = true;
98           *os_ << *cp;
99           break;
100         case '{':
101           nest_level++;
102           *os_ << *cp;
103           break;
104         case '}':
105           nest_level--;
106           *os_ << *cp;
107
108           if (nest_level < 0)
109             {
110               delete os_;       // we want to see the remains.
111               assert (nest_level>=0);
112             }
113
114           /* don't break line if not nested; very ugly for ps */
115           if (nest_level == 0)
116             break;
117
118           *os_ << '%';
119           break_line ();
120           break;
121         case '\n':
122           break_line ();
123           break;
124         case ' ':
125           *os_ <<  ' ';
126           if (line_len_i_ > MAXLINELEN)
127             break_line ();
128
129           break;
130         default:
131           *os_ << *cp;
132           break;
133         }
134     }
135   //urg, for debugging only!!
136   *os_ << flush;
137   return *this;
138 }
139
140 void
141 Paper_stream::break_line ()
142 {
143   *os_ << '\n';
144   *os_ << to_str (' ', nest_level);
145   outputting_comment_b_ = false;
146   line_len_i_ = 0;
147 }
148