1 #include <msp/strings/format.h>
2 #include "collection.h"
3 #include "packsource.h"
10 void PackSource::add_pack_file(const string &fn)
12 packs.push_back(Pack(fn));
13 Pack &pack = packs.back();
14 DataFile::load(pack, fn);
16 pack.collect_objects(objects);
19 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
21 ObjectMap::const_iterator i = objects.find(name);
25 // If the object has a keyword, it must match that of the type
26 if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
32 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
35 for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
37 if(!i->second->get_keyword().empty())
39 if(i->second->get_keyword()!=type.get_keyword())
42 else if(!type.match_name(i->first))
45 names.push_back(i->first);
51 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
53 ObjectMap::const_iterator i = objects.find(name);
57 File &file = i->second->get_file();
62 RefPtr<IO::Base> in = file.open();
63 Parser parser(*in, file.get_full_name());
64 if(file.is_collection())
66 Collection::Loader ldr(coll);
70 type.load_item(coll, parser, name);
74 PackSource::Pack::Pack(const string &fn):
79 void PackSource::Pack::collect_objects(ObjectMap &objs) const
81 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
82 i->collect_objects(objs);
86 PackSource::File::File(const Pack &p, const string &fn):
95 RefPtr<IO::Base> PackSource::File::open() const
97 RefPtr<IO::BufferedFile> io_file = new IO::BufferedFile(pack.get_filename());
98 io_file->seek(pack.get_base_offset()+offset, IO::S_BEG);
102 string PackSource::File::get_full_name() const
104 return format("%s/%s", pack.get_filename(), filename);
107 void PackSource::File::set_loaded()
112 void PackSource::File::collect_objects(ObjectMap &objs) const
114 for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
115 objs[i->get_name()] = &*i;
119 PackSource::Object::Object(File &f, const string &n, const string &k):
126 PackSource::Pack::Loader::Loader(Pack &p):
127 ObjectLoader<Pack>(p)
129 add("file", &Loader::file);
130 add("base_offset", &Pack::base_offset);
133 void PackSource::Pack::Loader::file(const string &fn)
135 obj.files.push_back(File(obj, fn));
136 load_sub(obj.files.back());
140 PackSource::File::Loader::Loader(File &f):
141 ObjectLoader<File>(f)
143 add("object", &Loader::object);
144 add("slice", &File::offset, &File::length);
147 void PackSource::File::Loader::finish()
151 PackSource::Object ob(obj, obj.filename, string());
152 obj.objects.push_back(ob);
156 void PackSource::File::Loader::object(const string &name, const string &kwd)
158 obj.objects.push_back(PackSource::Object(obj, name, kwd));
159 obj.collection = true;
162 } // namespace DataFile