-#include <msp/core/refptr.h>
+#include <msp/datafile/directorysource.h>
+#include <msp/datafile/packsource.h>
#include <msp/datafile/parser.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
#include "catalogue.h"
+#include "signaltype.h"
+#include "terraintype.h"
+#include "trackappearance.h"
#include "tracktype.h"
#include "vehicletype.h"
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<TrackType>().keyword("track");
+ add_type<SignalType>().keyword("signal");
+ add_type<VehicleType>().suffix(".veh").keyword("vehicle");
+ add_type<TerrainType>().suffix(".terr").keyword("terrain");
+ add_type<TrackAppearance>().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<DataFile::PackSource::FileInfo> files = pack_source->list_files();
+ for(list<DataFile::PackSource::FileInfo>::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<FS::Path> queue;
+ queue.push_back(path);
+ while(!queue.empty())
+ {
+ FS::Path dir = queue.front();
+ queue.pop_front();
+ list<string> files = FS::list_files(dir);
+ for(list<string>::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<Catalogue>(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<void (Loader::*)(unsigned)>(&Loader::track));
- add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
- add("track_technique", &Catalogue::track_technique);
- add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
- add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&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<TrackType> 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<VehicleType> veh = new VehicleType(art_nr);
- load_sub(*veh);
- obj.add_vehicle(*veh.release());
+ cat.scale = n/d;
}
} // namespace R2C2