]> git.donarmstrong.com Git - lilypond.git/blob - dstream.cc
release: 0.0.2
[lilypond.git] / dstream.cc
1 #include <fstream.h>
2
3 #include "dstream.hh"
4 #include "string.hh"
5 #include "textdb.hh"
6
7
8 /*
9   should use Regexp library.
10   */
11 static String
12 strip_pretty(String pret)
13 {
14     String cl(pret.left(pret.pos('(')-1));
15     int l = cl.lastPos(' ');
16     cl = cl.right(cl.len() -l);
17     return cl;
18 }
19
20 static String
21 strip_member(String pret)
22 {
23     String cl(pret.left(pret.lastPos(':')-2));
24     return cl;
25 }
26
27 Dstream&
28 Dstream::identify_as(String name)
29 {
30     String mem(strip_pretty(name));
31     String cl(strip_member(mem));
32     
33     if(!silent.elt_query(cl))
34         silent[cl] = false;
35     local_silence = silent[cl];
36     if (classname != cl && !local_silence) {
37         classname=cl;
38         *os << "[" << classname << ":]";
39     }
40     return *this;
41 }
42
43 void
44 Dstream::switch_output(String name,bool b)
45 {
46     silent[name] = b;
47 }
48
49 ///
50 Dstream &
51 Dstream::operator<<(String s)
52 {
53     if (local_silence)
54         return *this;
55     
56     for (const char *cp = s  ; *cp; cp++)
57         switch(*cp) 
58             {
59             case '{':
60             case '[':
61             case '(': indentlvl += INDTAB;
62                 *os << *cp;             
63                 break;
64                 
65             case ')':
66             case ']':
67             case '}':
68                 indentlvl -= INDTAB;
69                 *os << *cp              ;
70                 
71                 if  (indentlvl<0) indentlvl = 0;
72                 break;
73                 
74             case '\n':
75                 *os << '\n' << String (' ', indentlvl) << flush;
76                 break;        
77             default:
78                 *os << *cp;
79                 break;
80             }
81     return *this;    
82 }
83
84 /** only output possibility. Delegates all conversion to String class.
85  */
86
87 Dstream::Dstream(ostream &r, const char * cfg_nm )
88 {
89     os = &r;
90     indentlvl = 0;
91     
92     const char * fn =cfg_nm ? cfg_nm : ".dstreamrc";
93     {
94         ifstream ifs(fn);       // can't open
95         if (!ifs)
96             return;
97     }
98     cerr << "(" << fn;
99     Text_db cfg(fn);
100     while (! cfg.eof()){             
101          Text_record  r(  cfg++);
102          assert(r.sz() == 2);
103          silent[r[0]] = r[1].to_bool();
104     }
105     cerr <<")";
106 }
107
108