]> git.donarmstrong.com Git - lilypond.git/blob - flower/dstream.cc
patch::: 1.3.144.jcn6
[lilypond.git] / flower / dstream.cc
1 /*
2   dstream.cc -- implement Dstream
3
4   source file of the Flower Library
5
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"
15
16 /// amount of indentation for each level.
17 const int INDTAB = 2;
18
19 /*
20   should use Regexp library.
21   */
22 static String
23 strip_pretty (String pretty_str)
24 {
25   int i = pretty_str.index_i ('(');
26   if (i>=0)
27     pretty_str = pretty_str.left_str (i);
28
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;
33 }
34
35 static String
36 strip_member (String pret)
37 {
38   int l=pret.index_last_i (':')-1;
39   if (l>=0)
40     pret = pret.left_str (l);
41   return pret;
42 }
43
44 Dstream&
45 Dstream::identify_as (String name)
46 {
47   if (!os_l_)
48     return *this;
49
50   String mem (strip_pretty (name));
51   String cl (strip_member (mem));
52   String idx = cl;
53
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
59     {
60  (*silent_dict_p_)[idx] = default_silence_b_;
61     }
62   local_silence_b_ = (*silent_dict_p_)[idx];
63   if (current_classname_str_ != idx && !local_silence_b_)
64     {
65       current_classname_str_=idx;
66       if (! (*silent_dict_p_)["Dstream"])
67         *os_l_ << "[" << current_classname_str_ << ":]"; // messy.
68     }
69   return *this;
70 }
71
72 bool
73 Dstream::silent_b (String s) const
74 {
75   if (!silent_dict_p_)
76     return 0;
77   
78   if (!silent_dict_p_->elem_b (s))
79     return false;
80   return (*silent_dict_p_)[s];
81 }
82
83 Dstream &
84 Dstream::operator<< (void const *v_l)
85 {
86   output (String_convert::pointer_str (v_l));
87   return *this;
88 }
89
90 Dstream &
91 Dstream::operator << (String s)
92 {
93   output (s);
94   return *this;
95 }
96
97 Dstream &
98 Dstream::operator << (const char * s)
99 {
100   output (String (s));
101   return *this;
102 }
103
104 Dstream &
105 Dstream::operator << (char c)
106 {
107   output (to_str (c));
108   return *this;
109 }
110
111 Dstream&
112 Dstream::operator << (Real r)
113 {
114   output (to_str (r));
115   return *this;
116 }
117 Dstream &
118 Dstream::operator << (Rational c)
119 {
120   output (c.str ());
121   return *this;
122 }
123 Dstream &
124 Dstream::operator << (int i)
125 {
126   output (to_str (i));
127   return *this;
128 }
129   
130 void
131 Dstream::output (String s)
132 {
133   if (local_silence_b_|| !os_l_)
134     return ;
135
136   for (char const *cp = s.ch_C (); *cp; cp++)
137     switch (*cp)
138       {
139       case '{':
140       case '[':
141       case '(': indent_level_i_ += INDTAB;
142         *os_l_ << *cp;
143         break;
144
145       case ')':
146       case ']':
147       case '}':
148         indent_level_i_ -= INDTAB;
149         *os_l_ << *cp           ;
150
151         assert (indent_level_i_>=0) ;
152         break;
153
154       case '\n':
155         *os_l_ << '\n' << to_str (' ', indent_level_i_) << flush;
156         break;
157       default:
158         *os_l_ << *cp;
159         break;
160       }
161   return ;
162 }
163
164
165 Dstream::Dstream (ostream *r, char const * cfg_nm)
166 {
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;
173
174   char const * fn =cfg_nm ? cfg_nm : ".dstreamrc";
175   {
176     ifstream ifs (fn);  // can 't open
177     if (!ifs)
178       return;
179   }
180
181   Text_db cfg (fn);
182   while (!cfg.eof_b ()){
183     Text_record  r (cfg++);
184     if (r.size () != 2)
185       {
186         r.message (_ ("not enough fields in Dstream init"));
187         continue;
188       }
189  (*silent_dict_p_)[r[0]] = r[1] == "1";
190   }
191
192   if ((*silent_dict_p_).elem_b ("Dstream_default_silence"))
193     default_silence_b_ = (*silent_dict_p_)["Dstream_default_silence"];
194 }
195
196
197 Dstream::~Dstream ()
198 {
199   delete silent_dict_p_;
200   assert (!indent_level_i_) ;
201 }
202
203 void
204 Dstream::clear_silence ()
205 {
206   delete silent_dict_p_;
207   silent_dict_p_ = 0;
208 }
209