1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include "collection.h"
4 #include "packsource.h"
11 void PackSource::add_pack_file(const string &fn)
13 add_pack_file(fn, string());
16 void PackSource::add_pack_file(const string &fn, const string &filter)
19 for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
20 if(i->get_filename()==fn)
24 packs.push_back(Pack(fn));
26 DataFile::load(*pack, fn);
30 pack->collect_files(pack_files, filter);
31 for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
32 i->second->collect_objects(objects);
35 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
37 ObjectMap::const_iterator i = objects.find(name);
41 // If the object has a keyword, it must match that of the type
42 if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
48 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
51 for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
53 if(!i->second->get_keyword().empty())
55 if(i->second->get_keyword()!=type.get_keyword())
58 else if(!type.match_name(i->first))
61 names.push_back(i->first);
67 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
69 ObjectMap::const_iterator i = objects.find(name);
73 File &file = i->second->get_file();
78 RefPtr<IO::Base> in = file.open();
79 Parser parser(*in, file.get_full_name());
80 if(file.is_collection())
82 Collection::Loader ldr(coll);
86 type.load_item(coll, parser, name);
90 PackSource::Pack::Pack(const string &fn):
95 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
99 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
100 fm[i->get_filename()] = &*i;
105 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
106 if(re.match(i->get_filename()))
107 fm[i->get_filename()] = &*i;
112 PackSource::File::File(const Pack &p, const string &fn):
121 RefPtr<IO::Base> PackSource::File::open() const
123 RefPtr<IO::BufferedFile> io_file = new IO::BufferedFile(pack.get_filename());
124 io_file->seek(pack.get_base_offset()+offset, IO::S_BEG);
128 string PackSource::File::get_full_name() const
130 return format("%s/%s", pack.get_filename(), filename);
133 void PackSource::File::set_loaded()
138 void PackSource::File::collect_objects(ObjectMap &objs) const
140 for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
141 objs[i->get_name()] = &*i;
145 PackSource::Object::Object(File &f, const string &n, const string &k):
152 PackSource::Pack::Loader::Loader(Pack &p):
153 ObjectLoader<Pack>(p)
155 add("file", &Loader::file);
156 add("base_offset", &Pack::base_offset);
159 void PackSource::Pack::Loader::file(const string &fn)
161 obj.files.push_back(File(obj, fn));
162 load_sub(obj.files.back());
166 PackSource::File::Loader::Loader(File &f):
167 ObjectLoader<File>(f)
169 add("object", &Loader::object);
170 add("slice", &File::offset, &File::length);
173 void PackSource::File::Loader::finish()
177 PackSource::Object ob(obj, obj.filename, string());
178 obj.objects.push_back(ob);
182 void PackSource::File::Loader::object(const string &name, const string &kwd)
184 obj.objects.push_back(PackSource::Object(obj, name, kwd));
185 obj.collection = true;
188 } // namespace DataFile