]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
Add Id tags and copyright notices to files
[r2c2.git] / source / libmarklin / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include "catalogue.h"
11 #include "locotype.h"
12 #include "tracktype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Catalogue::~Catalogue()
20 {
21         for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
22                 delete i->second;
23 }
24
25 TrackType &Catalogue::get_track(unsigned art_nr) const
26 {
27         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
28         if(i==tracks.end())
29                 throw KeyError("Unknown track type");
30
31         return *i->second;
32 }
33
34 LocoType &Catalogue::get_locomotive(unsigned art_nr) const
35 {
36         map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
37         if(i==locos.end())
38                 throw KeyError("Unknown locomotive type");
39
40         return *i->second;
41 }
42
43 void Catalogue::load(const string &fn)
44 {
45         IO::File in(fn.c_str());
46
47         DataFile::Parser parser(in, fn);
48         Loader loader(*this);
49         loader.load(parser);
50 }
51
52
53 Catalogue::Loader::Loader(Catalogue &c):
54         cat(c)
55 {
56         add("locomotive", &Loader::locomotive);
57         add("track", &Loader::track);
58 }
59
60 void Catalogue::Loader::locomotive(unsigned art_no)
61 {
62         map<unsigned, LocoType *>::iterator i=cat.locos.find(art_no);
63         if(i!=cat.locos.end())
64                 throw Exception("Duplicate locomotive number");
65
66         RefPtr<LocoType> loco=new LocoType(art_no);
67         load_sub(*loco);
68         unsigned art_nr=loco->get_article_number();
69         cat.locos[art_nr]=loco.release();
70 }
71
72 void Catalogue::Loader::track(unsigned art_no)
73 {
74         map<unsigned, TrackType *>::iterator i=cat.tracks.find(art_no);
75         if(i!=cat.tracks.end())
76                 throw Exception("Duplicate track number");
77
78         RefPtr<TrackType> trk=new TrackType(art_no);
79         load_sub(*trk);
80         unsigned art_nr=trk->get_article_number();
81         cat.tracks[art_nr]=trk.release();
82 }
83
84 } // namespace Marklin