]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-stream.cc
release: 0.0.44
[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)<<"\n";
33 }
34 Tex_stream::~Tex_stream()
35 {
36     delete os;
37     assert(nest_level == 0);
38 }
39
40 // print string. don't forget indent.
41 Tex_stream &
42 Tex_stream::operator<<(String s)
43 {
44     
45     for (char const *cp = s; *cp; cp++) {
46         if (outputting_comment) {
47             *os << *cp;
48             if (*cp == '\n') {
49                 outputting_comment=false;
50
51             }
52             continue;
53         }
54         switch(*cp) 
55             {
56             case '%':
57                 outputting_comment = true;
58                 *os << *cp;
59                 break;
60             case '{':
61                 nest_level++;
62                 *os << *cp;             
63                 break;
64             case '}':
65                 nest_level--;           
66                 *os << *cp;
67                 
68                 if (nest_level < 0) {
69                     delete os;  // we want to see the remains.
70                     assert(nest_level>=0);
71                 }
72                 /* FALLTHROUGH */
73                 
74             case '\n':
75                 *os << "%\n";
76                 *os << String(' ', nest_level);
77                 break;        
78             default:
79                 *os << *cp;
80                 break;
81             }
82     }
83     return *this;
84 }
85
86
87 /* *************************************************************** */