lilypond-0.1.16
[lilypond.git] / lily / tex-stream.cc
bloba9d826c3c77b522e91793dc7e95037a67c551b9b
1 /*
2 tex-stream.cc -- implement Tex_stream
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
8 TODO
10 make an abstract interface to output, operations:
12 move (x,y), put (symbol).
15 #include <fstream.h>
16 #include <time.h>
18 #include "tex.hh"
19 #include "main.hh"
20 #include "tex-stream.hh"
21 #include "debug.hh"
23 const int MAXLINELEN = 200;
25 Tex_stream::Tex_stream (String filename)
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();
35 void
36 Tex_stream::header()
38 *os << "% Creator: " << get_version_str() << "\n";
39 *os << "% Automatically generated, at ";
40 time_t t (time (0));
41 *os << ctime (&t)<<"\n";
43 Tex_stream::~Tex_stream()
45 delete os;
46 assert (nest_level == 0);
49 // print string. don't forget indent.
50 Tex_stream &
51 Tex_stream::operator<<(String s)
54 for (char const *cp = s; *cp; cp++)
56 if (outputting_comment)
58 *os << *cp;
59 if (*cp == '\n')
61 outputting_comment=false;
64 continue;
66 line_len_i_ ++;
67 switch (*cp)
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;
81 if (nest_level < 0)
83 delete os; // we want to see the remains.
84 assert (nest_level>=0);
86 /* FALLTHROUGH */
88 case '\n':
89 break_line();
90 break;
91 case ' ':
92 *os << ' ';
93 if (line_len_i_ > MAXLINELEN)
94 break_line();
96 break;
97 default:
98 *os << *cp;
99 break;
102 return *this;
105 void
106 Tex_stream::break_line()
108 *os << "%\n";
109 *os << String (' ', nest_level);
110 line_len_i_ = 0;
113 /* *************************************************************** */