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