]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-stream.cc
release: 0.0.65
[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         if (outputting_comment) {
56             *os << *cp;
57             if (*cp == '\n') {
58                 outputting_comment=false;
59
60             }
61             continue;
62         }
63         line_len_i_ ++;
64         switch(*cp) 
65             {
66             case '%':
67                 outputting_comment = true;
68                 *os << *cp;
69                 break;
70             case '{':
71                 nest_level++;
72                 *os << *cp;             
73                 break;
74             case '}':
75                 nest_level--;           
76                 *os << *cp;
77                 
78                 if (nest_level < 0) {
79                     delete os;  // we want to see the remains.
80                     assert(nest_level>=0);
81                 }
82                 /* FALLTHROUGH */
83                 
84             case '\n':
85                 break_line();
86                 break;        
87             case ' ':
88                 *os <<  ' ';
89                 if (line_len_i_ > MAXLINELEN) 
90                    break_line(); 
91                 
92                 break;
93             default:
94                 *os << *cp;
95                 break;
96             }
97     }
98     return *this;
99 }
100
101 void
102 Tex_stream::break_line()
103 {
104     *os << "%\n";
105     *os << String(' ', nest_level);
106     line_len_i_ = 0;
107 }
108
109 /* *************************************************************** */