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