]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/catalogue.cpp
Get rid of some obsolete #includes
[r2c2.git] / source / libr2c2 / catalogue.cpp
index 221a6f0663667cca1665e4388d867e422c16fb5e..7a780116664a5c27ae1f86d073fb1f64b2523d32 100644 (file)
@@ -1,8 +1,13 @@
-#include <msp/core/maputils.h>
-#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"
 
@@ -13,100 +18,96 @@ namespace R2C2 {
 
 Catalogue::Catalogue():
        scale(1),
-       gauge(1.524),
        layout(*this)
-{ }
-
-Catalogue::~Catalogue()
 {
-       for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ++i)
-               delete 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");
 
-float Catalogue::get_rail_elevation() const
-{
-       return ballast_profile.get_height()+rail_profile.get_height();
+       add_source("data/r2c2");
 }
 
-void Catalogue::add(ObjectType &object)
+void Catalogue::add_source(const FS::Path &path)
 {
-       insert_unique(objects, object.get_article_number(), &object);
-       signal_object_added.emit(object);
-}
-
-const ObjectType &Catalogue::get(const ArticleNumber &art_nr) const
-{
-       return *get_item(objects, art_nr);
+       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::ObjectLoader<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("signal", &Loader::signal);
-       add("track", &Loader::track);
-       add("track_technique", &Catalogue::track_technique);
-       add("vehicle", &Loader::vehicle);
-}
-
-void Catalogue::Loader::ballast_profile()
-{
-       load_sub(obj.ballast_profile);
-}
-
-void Catalogue::Loader::gauge(float g)
-{
-       obj.gauge = g/1000;
 }
 
 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::signal(ArticleNumber art_nr)
-{
-       if(obj.objects.count(art_nr))
-               throw key_error(art_nr);
-
-       RefPtr<SignalType> sig = new SignalType(art_nr);
-       load_sub(*sig);
-       obj.add(*sig.release());
-}
-
-void Catalogue::Loader::track(ArticleNumber art_nr)
-{
-       if(obj.objects.count(art_nr))
-               throw key_error(art_nr);
-
-       RefPtr<TrackType> trk = new TrackType(art_nr);
-       load_sub(*trk);
-       obj.add(*trk.release());
-}
-
-void Catalogue::Loader::vehicle(ArticleNumber art_nr)
-{
-       if(obj.objects.count(art_nr))
-               throw key_error(art_nr);
-
-       RefPtr<VehicleType> veh = new VehicleType(art_nr);
-       load_sub(*veh);
-       obj.add(*veh.release());
+       cat.scale = n/d;
 }
 
 } // namespace R2C2