]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/zone.cpp
30b60e99aa65865a0960afc113e3d5f5fedfbd7f
[r2c2.git] / source / libr2c2 / zone.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include "block.h"
4 #include "layout.h"
5 #include "track.h"
6 #include "zone.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 namespace R2C2 {
12
13 Zone::Zone(Layout &l):
14         TrackChain(l),
15         number(0)
16 {
17         layout.add(*this);
18 }
19
20 Zone::~Zone()
21 {
22         layout.remove(*this);
23 }
24
25 void Zone::set_name(const string &n)
26 {
27         Regex r_name("(.+) ([^ ]+) ([1-9][0-9]*)");
28         RegMatch m = r_name.match(n);
29         if(!m)
30                 throw invalid_argument("Zone::set_name");
31
32         set_name(m[1].str, m[2].str, lexical_cast<unsigned>(m[3].str));
33 }
34
35 void Zone::set_name(const string &g, const string &q, unsigned n)
36 {
37         group = g;
38         qualifier = q;
39         number = n;
40
41         update_name();
42 }
43
44 void Zone::update_name()
45 {
46         string full_name = group;
47         if(!qualifier.empty())
48         {
49                 full_name += ' ';
50                 full_name += qualifier;
51         }
52         if(number)
53                 full_name += format(" %d", number);
54         TrackChain::set_name(full_name);
55 }
56
57 void Zone::save(list<DataFile::Statement> &st) const
58 {
59         st.push_back((DataFile::Statement("group"), group));
60         if(!qualifier.empty())
61                 st.push_back((DataFile::Statement("qualifier"), qualifier));
62         if(number)
63                 st.push_back((DataFile::Statement("number"), number));
64
65         set<unsigned> block_ids;
66         for(TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
67                 block_ids.insert((*i)->get_block().get_id());
68
69         for(set<unsigned>::const_iterator i=block_ids.begin(); i!=block_ids.end(); ++i)
70                 st.push_back((DataFile::Statement("block"), *i));
71 }
72
73 DataFile::Statement Zone::save_reference() const
74 {
75         return (DataFile::Statement("zone"), group, number);
76 }
77
78
79 Zone::Loader::Loader(Zone &z):
80         DataFile::ObjectLoader<Zone>(z)
81 {
82         add("block",     &Loader::block);
83         add("group",     &Zone::group);
84         add("number",    &Zone::number);
85         add("qualifier", &Zone::qualifier);
86 }
87
88 void Zone::Loader::finish()
89 {
90         obj.update_name();
91 }
92
93 void Zone::Loader::block(unsigned b)
94 {
95         Block &blk = obj.layout.get_block(b);
96         const set<Track *> &btracks = blk.get_tracks();
97         obj.tracks.insert(btracks.begin(), btracks.end());
98 }
99
100 } // namespace R2C2