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