1 #include <msp/io/slice.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include "collection.h"
5 #include "packsource.h"
11 void delete_io(Msp::IO::Base *io)
21 void PackSource::add_pack_file(const string &fn)
23 add_pack_file(fn, string());
26 void PackSource::add_pack_file(const string &fn, const string &filter)
28 add_pack(0, fn, filter);
31 void PackSource::add_pack_io(IO::Seekable &io, const string &fn)
33 add_pack(&io, fn, string());
36 void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter)
38 add_pack(&io, fn, filter);
41 void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter)
44 for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
45 if(i->get_filename()==fn || (io && i->get_io()==io))
49 packs.push_back(Pack(io, fn));
53 DataFile::Parser parser(*io, fn);
54 Pack::Loader loader(*pack);
58 DataFile::load(*pack, fn);
62 pack->collect_files(pack_files, filter);
63 files.insert(pack_files.begin(), pack_files.end());
64 for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
65 i->second->collect_objects(objects);
68 list<PackSource::FileInfo> PackSource::list_files() const
70 list<FileInfo> result;
71 for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i)
72 result.push_back(i->second->get_info());
76 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
78 ObjectMap::const_iterator i = objects.find(name);
82 // If the object has a keyword, it must match that of the type
83 if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
89 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
92 for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
94 if(!i->second->get_keyword().empty())
96 if(i->second->get_keyword()!=type.get_keyword())
99 else if(!type.match_name(i->first))
102 names.push_back(i->first);
108 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
110 ObjectMap::const_iterator i = objects.find(name);
114 File &file = i->second->get_file();
116 RefPtr<IO::Base> in = file.open();
117 Parser parser(*in, file.get_full_name());
118 if(file.is_collection())
120 Collection::Loader ldr(coll);
124 type.load_item(coll, parser, name);
127 IO::Seekable *PackSource::open(const string &fn) const
129 FileMap::const_iterator i = files.find(fn);
131 return i->second->open().release();
137 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
143 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
147 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
148 fm[i->get_filename()] = &*i;
153 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
154 if(re.match(i->get_filename()))
155 fm[i->get_filename()] = &*i;
160 PackSource::File::File(const Pack &p, const string &fn):
168 RefPtr<IO::Seekable> PackSource::File::open() const
171 // TODO Performance may be poor without buffering
172 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
175 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
176 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
177 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
182 PackSource::FileInfo PackSource::File::get_info() const
185 info.name = filename;
190 string PackSource::File::get_full_name() const
192 return format("%s/%s", pack.get_filename(), filename);
195 void PackSource::File::collect_objects(ObjectMap &objs) const
197 for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
198 objs[i->get_name()] = &*i;
202 PackSource::Object::Object(File &f, const string &n, const string &k):
209 PackSource::Pack::Loader::Loader(Pack &p):
210 ObjectLoader<Pack>(p)
212 add("file", &Loader::file);
213 add("base_offset", &Pack::base_offset);
216 void PackSource::Pack::Loader::file(const string &fn)
218 obj.files.push_back(File(obj, fn));
219 load_sub(obj.files.back());
223 PackSource::File::Loader::Loader(File &f):
224 ObjectLoader<File>(f)
226 add("object", &Loader::object);
227 add("slice", &File::offset, &File::length);
230 void PackSource::File::Loader::finish()
234 PackSource::Object ob(obj, obj.filename, string());
235 obj.objects.push_back(ob);
239 void PackSource::File::Loader::object(const string &name, const string &kwd)
241 obj.objects.push_back(PackSource::Object(obj, name, kwd));
242 obj.collection = true;
245 } // namespace DataFile