lilypond-1.5.2
[lilypond.git] / flower / dstream.cc
blobc157fe5049d2ed83daeb4414a4e209e7830106d8
1 /*
2 dstream.cc -- implement Dstream
4 source file of the Flower Library
6 (c) 1996, 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <fstream.h>
9 #include "dstream.hh"
10 #include "dictionary-iter.hh"
11 #include "dictionary.hh"
12 #include "text-db.hh"
13 #include "string-convert.hh"
14 #include "rational.hh"
16 /// amount of indentation for each level.
17 const int INDTAB = 2;
20 should use Regexp library.
22 static String
23 strip_pretty (String pretty_str)
25 int i = pretty_str.index_i ('(');
26 if (i>=0)
27 pretty_str = pretty_str.left_str (i);
29 int l = pretty_str.index_last_i (' '); // strip until last ' '
30 if (l>=0)
31 pretty_str = pretty_str.nomid_str (0,l+1);
32 return pretty_str;
35 static String
36 strip_member (String pret)
38 int l=pret.index_last_i (':')-1;
39 if (l>=0)
40 pret = pret.left_str (l);
41 return pret;
44 Dstream&
45 Dstream::identify_as (String name)
47 if (!os_l_)
48 return *this;
50 String mem (strip_pretty (name));
51 String cl (strip_member (mem));
52 String idx = cl;
54 if (silent_dict_p_->elem_b (mem))
55 idx = mem;
56 else if (silent_dict_p_->elem_b (cl))
57 idx = cl;
58 else
60 (*silent_dict_p_)[idx] = default_silence_b_;
62 local_silence_b_ = (*silent_dict_p_)[idx];
63 if (current_classname_str_ != idx && !local_silence_b_)
65 current_classname_str_=idx;
66 if (! (*silent_dict_p_)["Dstream"])
67 *os_l_ << "[" << current_classname_str_ << ":]"; // messy.
69 return *this;
72 bool
73 Dstream::silent_b (String s) const
75 if (!silent_dict_p_)
76 return 0;
78 if (!silent_dict_p_->elem_b (s))
79 return false;
80 return (*silent_dict_p_)[s];
83 Dstream &
84 Dstream::operator<< (void const *v_l)
86 output (String_convert::pointer_str (v_l));
87 return *this;
90 Dstream &
91 Dstream::operator << (String s)
93 output (s);
94 return *this;
97 Dstream &
98 Dstream::operator << (const char * s)
100 output (String (s));
101 return *this;
104 Dstream &
105 Dstream::operator << (char c)
107 output (to_str (c));
108 return *this;
111 Dstream&
112 Dstream::operator << (Real r)
114 output (to_str (r));
115 return *this;
117 Dstream &
118 Dstream::operator << (Rational c)
120 output (c.str ());
121 return *this;
123 Dstream &
124 Dstream::operator << (int i)
126 output (to_str (i));
127 return *this;
130 void
131 Dstream::output (String s)
133 if (local_silence_b_|| !os_l_)
134 return ;
136 for (char const *cp = s.ch_C (); *cp; cp++)
137 switch (*cp)
139 case '{':
140 case '[':
141 case '(': indent_level_i_ += INDTAB;
142 *os_l_ << *cp;
143 break;
145 case ')':
146 case ']':
147 case '}':
148 indent_level_i_ -= INDTAB;
149 *os_l_ << *cp ;
151 assert (indent_level_i_>=0) ;
152 break;
154 case '\n':
155 *os_l_ << '\n' << to_str (' ', indent_level_i_) << flush;
156 break;
157 default:
158 *os_l_ << *cp;
159 break;
161 return ;
165 Dstream::Dstream (ostream *r, char const * cfg_nm)
167 os_l_ = r;
168 silent_dict_p_ = new Dictionary<bool>;
169 default_silence_b_ = false;
170 indent_level_i_ = 0;
171 if (!os_l_)
172 return;
174 char const * fn =cfg_nm ? cfg_nm : ".dstreamrc";
176 ifstream ifs (fn); // can 't open
177 if (!ifs)
178 return;
181 Text_db cfg (fn);
182 while (!cfg.eof_b ()){
183 Text_record r (cfg++);
184 if (r.size () != 2)
186 r.message (_ ("not enough fields in Dstream init"));
187 continue;
189 (*silent_dict_p_)[r[0]] = r[1] == "1";
192 if ((*silent_dict_p_).elem_b ("Dstream_default_silence"))
193 default_silence_b_ = (*silent_dict_p_)["Dstream_default_silence"];
197 Dstream::~Dstream ()
199 delete silent_dict_p_;
200 assert (!indent_level_i_) ;
203 void
204 Dstream::clear_silence ()
206 delete silent_dict_p_;
207 silent_dict_p_ = 0;