]> git.donarmstrong.com Git - lilypond.git/blob - src/inputcommands.cc
release: 0.0.19
[lilypond.git] / src / inputcommands.cc
1 #include "inputcommands.hh"
2 #include "inputcommand.hh"
3 #include "debug.hh"
4 #include "staffcommands.hh"
5 #include "getcommand.hh"
6 #include "command.hh"
7
8 void
9 Commands_at::print() const
10 {
11 #ifndef NPRINT
12     mtor << "Commands_at {";
13     moment_.print();
14     for (PCursor<Input_command *> cc(*this); cc.ok(); cc++) 
15         cc->print();
16     mtor << "}\n";
17 #endif
18 }
19 Real
20 Commands_at::when()
21 {
22     return moment_.when;
23 }
24 Commands_at::Commands_at(Real dt, Commands_at* prev)
25     : moment_(dt, (prev)? &prev->moment_ : 0)
26 {
27     if (prev&& !moment_.whole_in_measure) {
28         bottom().add(get_bar_command());
29     }
30 }
31
32 void
33 Commands_at::add(Input_command *i)
34 {
35     bottom().add(i);
36
37     // should check for other meterchanges here.
38     if (i->args[0] == "METER") { 
39         int l = i->args[1];
40         int o = i->args[2];
41         moment_.set_meter(l,o);
42         bottom().add(get_grouping_command( moment_.one_beat,
43                                            get_default_grouping(l)));
44
45     }
46 }
47
48 Commands_at::Commands_at(Commands_at const&src) :
49     moment_(src.moment_)
50 {
51     IPointerList<Input_command*> &me(*this);
52     const IPointerList<Input_command*> &that(src);
53     
54     PL_copy(me, that);
55 }
56
57 void
58 Commands_at::setpartial(Real p)
59 {
60     moment_.setpartial(p);
61 }
62
63 Real
64 Commands_at::barleft()
65 {
66     return  moment_.barleft();
67 }
68
69 void
70 Commands_at::parse(Staff_commands_at*s)
71 {
72     s->moment_ = moment_;
73     for (PCursor<Input_command *> cc(*this); cc.ok(); cc++) {
74         if (cc->args.sz() &&  cc->args[0] !="") {
75             Command c = **cc;
76             s->add(c);
77             
78         }
79     }
80 }
81 /****************/
82
83 void
84 Input_cursor::find_moment(Real w)
85 {
86     Real last = when();
87     while  (1) {
88         if (! ok() ) {
89             *this = list().bottom();
90             Real dt = (w - when()) <? ptr()->barleft();
91
92             Commands_at * c = new Commands_at(dt, *this);
93             assert(c->when() <= w);
94             add(c);
95         } else if (when() == w ) {
96             return ;
97         } else if (when() > w )
98             break;
99         
100         
101         last = when();
102         next();
103     }
104
105     prev();
106     Real dt = (w - when());
107     Commands_at * c = new Commands_at(dt, *this);
108     add(c);
109     next();
110 }
111
112
113
114 /****************/
115 Input_commands::Input_commands(Input_commands const&src)
116     : ptr(src.ptr)
117 {
118     IPointerList<Commands_at*> &me(*this);
119     const IPointerList<Commands_at*> &that(src);
120     
121     PL_copy(me, that);    
122 }
123
124 Input_commands::Input_commands()
125     :    ptr (bottom())
126 {
127     Commands_at * p = new Commands_at(0,0);    
128     bottom().add(p);    
129     ptr = bottom();    
130 }
131
132 void
133 Input_commands::do_skip(int bars, Real wholes)
134 {
135     while (bars > 0) {
136         Real b = ptr->barleft();
137         ptr.find_moment(ptr->when() + b);
138         bars --;        
139     }
140     if (wholes) {
141         ptr.find_moment(ptr->when() + wholes);
142     }
143 }
144
145
146 void
147 Input_commands::add(Input_command c)
148 {    
149     if (c.args[0] == "PARTIAL") {       
150         ptr->setpartial(c.args[1]);
151     } else if (c.args[0] == "GROUPING") {
152         Input_command *ic = new Input_command(c);
153         ic->args.insert(ptr->moment_.one_beat, 1);
154         ptr->add(ic);
155     } else if (c.args[0] == "METER") {
156         int beats_per_meas = c.args[1];
157         int one_beat = c.args[2];
158         Input_command *ch = get_meterchange_command(beats_per_meas, one_beat);
159         ptr->add(ch);           
160     } else if (c.args[0] == "SKIP") {
161         int bars = c.args[1] ;
162         Real wholes= c.args[2];
163         do_skip(bars, wholes);
164     } else if (c.args[0] == "RESET") {
165         ptr= top();
166     } else {
167         Input_command *ic = new Input_command(c);
168         ptr->add(ic);
169     } 
170     
171 }
172
173 Staff_commands*
174 Input_commands::parse() const
175 {
176     print();
177     Staff_commands*nc = new Staff_commands;
178
179     for (PCursor<Commands_at*> i(*this); i.ok(); i++) {
180
181         Staff_commands_at* s= nc->find(i->when());
182         if (!s){
183             s = new Staff_commands_at(i->moment_);
184             nc->add(s);
185         }
186         if (!i->when()) {   /* all pieces should start with a breakable. */
187             Command c;//(0.0);
188             c.code = INTERPRET;
189             c.args.add("BAR");
190             c.args.add("empty");
191             s->add(c);
192         }
193
194         i->parse(s);
195     }
196     return nc;
197 }
198
199
200 void
201 Input_commands::print() const
202 {
203 #ifndef NPRINT
204     for (PCursor<Commands_at*> cc(*this); cc.ok() ; cc++) {
205         cc->print();
206     }
207 #endif
208 }
209 /****************/
210
211 Real
212 Input_cursor::when()const
213 {
214     return (*this)->when(); 
215 }
216 Input_cursor::Input_cursor(PCursor<Commands_at *>c)
217     : PCursor<Commands_at*>(c)
218 {
219 }