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