]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-stream.cc
a9d826c3c77b522e91793dc7e95037a67c551b9b
[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   TODO
9
10   make an abstract interface to output, operations: 
11
12   move (x,y), put (symbol).
13 */
14
15 #include <fstream.h>
16 #include <time.h>
17
18 #include "tex.hh"
19 #include "main.hh"
20 #include "tex-stream.hh"
21 #include "debug.hh"
22
23 const int MAXLINELEN = 200;
24
25 Tex_stream::Tex_stream (String filename) 
26 {
27   os = new ofstream (filename);
28   if (!*os)
29         error ("can't open `" + filename+"\'");
30   nest_level = 0;
31   line_len_i_ = 0;
32   outputting_comment=false;
33   header();
34 }
35 void
36 Tex_stream::header()
37 {
38   *os << "% Creator: " << get_version_str() << "\n";
39   *os << "% Automatically generated, at ";
40   time_t t (time (0));
41   *os << ctime (&t)<<"\n";
42 }
43 Tex_stream::~Tex_stream()
44 {
45   delete os;
46   assert (nest_level == 0);
47 }
48
49 // print string. don't forget indent.
50 Tex_stream &
51 Tex_stream::operator<<(String s)
52 {
53   
54   for (char const *cp = s; *cp; cp++) 
55     {
56         if (outputting_comment) 
57           {
58             *os << *cp;
59             if (*cp == '\n') 
60               {
61                 outputting_comment=false;
62
63               }
64             continue;
65           }
66         line_len_i_ ++;
67         switch (*cp) 
68             {
69             case '%':
70                 outputting_comment = true;
71                 *os << *cp;
72                 break;
73             case '{':
74                 nest_level++;
75                 *os << *cp;             
76                 break;
77             case '}':
78                 nest_level--;           
79                 *os << *cp;
80                 
81                 if (nest_level < 0) 
82                   {
83                     delete os;  // we want to see the remains.
84                     assert (nest_level>=0);
85                   }
86                 /* FALLTHROUGH */
87                 
88             case '\n':
89                 break_line();
90                 break;        
91             case ' ':
92                 *os <<  ' ';
93                 if (line_len_i_ > MAXLINELEN) 
94                    break_line(); 
95                 
96                 break;
97             default:
98                 *os << *cp;
99                 break;
100               }
101     }
102   return *this;
103 }
104
105 void
106 Tex_stream::break_line()
107 {
108   *os << "%\n";
109   *os << String (' ', nest_level);
110   line_len_i_ = 0;
111 }
112
113 /* *************************************************************** */