X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fcatalogue.cpp;h=7a780116664a5c27ae1f86d073fb1f64b2523d32;hb=508ee4bfcc0f8fb1373fb7af251c59c873ef896f;hp=df59a0ee20ef7e73a5235762ced88a59e23dfd7a;hpb=d15ac13f2e170f155b4bbd124df48400c339b644;p=r2c2.git diff --git a/source/libr2c2/catalogue.cpp b/source/libr2c2/catalogue.cpp index df59a0e..7a78011 100644 --- a/source/libr2c2/catalogue.cpp +++ b/source/libr2c2/catalogue.cpp @@ -1,6 +1,13 @@ -#include +#include +#include #include +#include +#include +#include #include "catalogue.h" +#include "signaltype.h" +#include "terraintype.h" +#include "trackappearance.h" #include "tracktype.h" #include "vehicletype.h" @@ -11,131 +18,96 @@ namespace R2C2 { Catalogue::Catalogue(): scale(1), - gauge(1.524), layout(*this) -{ } - -Catalogue::~Catalogue() -{ - for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i) - delete i->second; - for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) - delete i->second; -} - -float Catalogue::get_rail_elevation() const -{ - return ballast_profile.get_height()+rail_profile.get_height(); -} - -void Catalogue::add_track(TrackType &track) -{ - if(tracks.count(track.get_article_number())) - throw Exception("Duplicate track type"); - - tracks[track.get_article_number()] = &track; - signal_track_added.emit(track); -} - -const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const { - TrackMap::const_iterator i=tracks.find(art_nr); - if(i==tracks.end()) - throw KeyError("Unknown track type"); - - return *i->second; -} - -void Catalogue::add_vehicle(VehicleType &veh) -{ - if(vehicles.count(veh.get_article_number())) - throw Exception("Duplicate vehicle type"); - - vehicles[veh.get_article_number()] = &veh; - signal_vehicle_added.emit(veh); -} - -const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const -{ - VehicleMap::const_iterator i = vehicles.find(art_nr); - if(i==vehicles.end()) - throw KeyError("Unknown vehicle type"); - - return *i->second; + add_type().keyword("track"); + add_type().keyword("signal"); + add_type().suffix(".veh").keyword("vehicle"); + add_type().suffix(".terr").keyword("terrain"); + add_type().suffix("trkapp").keyword("track_appearance"); + + add_source("data/r2c2"); +} + +void Catalogue::add_source(const FS::Path &path) +{ + string ext = FS::extpart(path.str()); + DataFile::CollectionSource *source = 0; + IO::Seekable *init_file = 0; + if(ext==".mdt") + { + DataFile::PackSource *pack_source = new DataFile::PackSource; + pack_source->add_pack_file(path.str()); + source = pack_source; + + list files = pack_source->list_files(); + for(list::const_iterator i=files.begin(); i!=files.end(); ++i) + { + string fext = FS::extpart(i->name); + if(fext==".cat" && !init_file) + init_file = pack_source->open(i->name); + } + } + else if(FS::is_dir(path)) + { + DataFile::DirectorySource *dir_source = new DataFile::DirectorySource; + dir_source->add_directory(path); + + list queue; + queue.push_back(path); + while(!queue.empty()) + { + FS::Path dir = queue.front(); + queue.pop_front(); + list files = FS::list_files(dir); + for(list::const_iterator i=files.begin(); i!=files.end(); ++i) + { + FS::Path full = dir/ *i; + if(FS::is_dir(full)) + { + dir_source->add_directory(full); + queue.push_back(full); + } + + string fext = FS::extpart(*i); + if(fext==".cat" && !init_file) + init_file = new IO::BufferedFile(full.str()); + } + } + + source = dir_source; + } + + sources.push_back(source); + Collection::add_source(*source); + signal_source_added.emit(*source); + + if(init_file) + { + DataFile::Parser parser(*init_file, path.str()); + Loader ldr(*this); + ldr.load(parser); + } + delete init_file; } Catalogue::Loader::Loader(Catalogue &c): - DataFile::BasicLoader(c) + Collection::Loader(c), + cat(c) { - add("ballast_profile", &Loader::ballast_profile); - add("gauge", &Loader::gauge); add("layout", &Loader::layout); - add("rail_profile", &Loader::rail_profile); add("scale", &Loader::scale); - add("track", static_cast(&Loader::track)); - add("track", static_cast(&Loader::track)); - add("track_technique", &Catalogue::track_technique); - add("vehicle", static_cast(&Loader::vehicle)); - add("vehicle", static_cast(&Loader::vehicle)); -} - -void Catalogue::Loader::ballast_profile() -{ - load_sub(obj.ballast_profile); -} - -void Catalogue::Loader::gauge(float g) -{ - obj.gauge = g/1000; - obj.path_profile = Profile(); - obj.path_profile.append_vertex(Vector(0.1*obj.gauge, 0), false); - obj.path_profile.append_vertex(Vector(-0.1*obj.gauge, 0), false); } void Catalogue::Loader::layout() { - load_sub(obj.layout); -} - -void Catalogue::Loader::rail_profile() -{ - load_sub(obj.rail_profile); + load_sub(cat.layout); } void Catalogue::Loader::scale(float n, float d) { - obj.scale = n/d; -} - -void Catalogue::Loader::track(unsigned art_nr) -{ - track(ArticleNumber(art_nr)); -} - -void Catalogue::Loader::track(ArticleNumber art_nr) -{ - if(obj.tracks.count(art_nr)) - throw KeyError("Duplicate track type", art_nr.str()); - - RefPtr trk = new TrackType(art_nr); - load_sub(*trk); - obj.add_track(*trk.release()); -} - -void Catalogue::Loader::vehicle(unsigned art_nr) -{ - vehicle(ArticleNumber(art_nr)); -} - -void Catalogue::Loader::vehicle(ArticleNumber art_nr) -{ - if(obj.vehicles.count(art_nr)) - throw KeyError("Duplicate vehicle type", art_nr.str()); - - RefPtr veh = new VehicleType(art_nr); - load_sub(*veh); - obj.add_vehicle(*veh.release()); + cat.scale = n/d; } } // namespace R2C2