]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-stream.cc
release: 0.0.42.pre3
[lilypond.git] / lily / tex-stream.cc
1 /*
2   tex-stream.cc -- implement Tex_stream
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include <fstream.h>
10 #include <time.h>
11
12 #include "tex.hh"
13 #include "main.hh"
14 #include "tex-stream.hh"
15 #include "debug.hh"
16
17 Tex_stream::Tex_stream(String filename) 
18 {
19     os = new ofstream(filename);
20     if (!*os)
21         error("can't open `" + filename+"\'");
22     nest_level = 0;
23     outputting_comment=false;
24     header();
25 }
26 void
27 Tex_stream::header()
28 {
29     *os << "% Creator: " << get_version_str();
30     *os << "% Automatically generated, at ";
31     time_t t(time(0));
32     *os << ctime(&t);
33     *os << "% from musical definition: " + infile_str_g + "\n";
34 }
35 Tex_stream::~Tex_stream()
36 {
37     delete os;
38     assert(nest_level == 0);
39 }
40
41 // print string. don't forget indent.
42 Tex_stream &
43 Tex_stream::operator<<(String s)
44 {
45     
46     for (char const *cp = s; *cp; cp++) {
47         if (outputting_comment) {
48             *os << *cp;
49             if (*cp == '\n') {
50                 outputting_comment=false;
51
52             }
53             continue;
54         }
55         switch(*cp) 
56             {
57             case '%':
58                 outputting_comment = true;
59                 *os << *cp;
60                 break;
61             case '{':
62                 nest_level++;
63                 *os << *cp;             
64                 break;
65             case '}':
66                 nest_level--;           
67                 *os << *cp;
68                 
69                 if (nest_level < 0) {
70                     delete os;  // we want to see the remains.
71                     assert(nest_level>=0);
72                 }
73                 /* FALLTHROUGH */
74                 
75             case '\n':
76                 *os << "%\n";
77                 *os << String(' ', nest_level);
78                 break;        
79             default:
80                 *os << *cp;
81                 break;
82             }
83     }
84     return *this;
85 }
86
87
88 /* *************************************************************** */