lilypond-0.0.63
[lilypond.git] / lily / tex-stream.cc
blobdfa56b1b4d955bc069869775e2d7aa37cbbfee0e
1 /*
2 tex-stream.cc -- implement Tex_stream
4 source file of the LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include <fstream.h>
10 #include <time.h>
12 #include "tex.hh"
13 #include "main.hh"
14 #include "tex-stream.hh"
15 #include "debug.hh"
17 const int MAXLINELEN = 200;
19 Tex_stream::Tex_stream(String filename)
21 os = new ofstream(filename);
22 if (!*os)
23 error("can't open `" + filename+"\'");
24 nest_level = 0;
25 line_len_i_ = 0;
26 outputting_comment=false;
27 header();
29 void
30 Tex_stream::header()
32 *os << "% Creator: " << get_version_str();
33 *os << "% Automatically generated, at ";
34 time_t t(time(0));
35 *os << ctime(&t)<<"\n";
37 Tex_stream::~Tex_stream()
39 delete os;
40 assert(nest_level == 0);
43 // print string. don't forget indent.
44 Tex_stream &
45 Tex_stream::operator<<(String s)
48 for (char const *cp = s; *cp; cp++) {
49 if (outputting_comment) {
50 *os << *cp;
51 if (*cp == '\n') {
52 outputting_comment=false;
55 continue;
57 line_len_i_ ++;
58 switch(*cp)
60 case '%':
61 outputting_comment = true;
62 *os << *cp;
63 break;
64 case '{':
65 nest_level++;
66 *os << *cp;
67 break;
68 case '}':
69 nest_level--;
70 *os << *cp;
72 if (nest_level < 0) {
73 delete os; // we want to see the remains.
74 assert(nest_level>=0);
76 /* FALLTHROUGH */
78 case '\n':
79 break_line();
80 break;
81 case ' ':
82 *os << ' ';
83 if (line_len_i_ > MAXLINELEN)
84 break_line();
86 break;
87 default:
88 *os << *cp;
89 break;
92 return *this;
95 void
96 Tex_stream::break_line()
98 *os << "%\n";
99 *os << String(' ', nest_level);
100 line_len_i_ = 0;
103 /* *************************************************************** */