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::Seekable *io, const string &fn, const string &filter)
39 for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
40 if(i->get_filename()==fn)
44 packs.push_back(Pack(io, fn));
48 DataFile::Parser parser(*io, fn);
49 Pack::Loader loader(*pack);
53 DataFile::load(*pack, fn);
57 pack->collect_files(pack_files, filter);
58 files.insert(pack_files.begin(), pack_files.end());
59 for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
60 i->second->collect_objects(objects);
63 list<PackSource::FileInfo> PackSource::list_files() const
65 list<FileInfo> result;
66 for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i)
67 result.push_back(i->second->get_info());
71 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
73 ObjectMap::const_iterator i = objects.find(name);
77 // If the object has a keyword, it must match that of the type
78 if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
84 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
87 for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
89 if(!i->second->get_keyword().empty())
91 if(i->second->get_keyword()!=type.get_keyword())
94 else if(!type.match_name(i->first))
97 names.push_back(i->first);
103 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
105 ObjectMap::const_iterator i = objects.find(name);
109 File &file = i->second->get_file();
114 RefPtr<IO::Base> in = file.open();
115 Parser parser(*in, file.get_full_name());
116 if(file.is_collection())
118 Collection::Loader ldr(coll);
122 type.load_item(coll, parser, name);
125 IO::Seekable *PackSource::open(const string &fn) const
127 FileMap::const_iterator i = files.find(fn);
129 return i->second->open().release();
135 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
141 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
145 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
146 fm[i->get_filename()] = &*i;
151 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
152 if(re.match(i->get_filename()))
153 fm[i->get_filename()] = &*i;
158 PackSource::File::File(const Pack &p, const string &fn):
167 RefPtr<IO::Seekable> PackSource::File::open() const
170 // TODO Performance may be poor without buffering
171 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
174 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
175 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
176 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
181 PackSource::FileInfo PackSource::File::get_info() const
184 info.name = filename;
189 string PackSource::File::get_full_name() const
191 return format("%s/%s", pack.get_filename(), filename);
194 void PackSource::File::set_loaded()
199 void PackSource::File::collect_objects(ObjectMap &objs) const
201 for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
202 objs[i->get_name()] = &*i;
206 PackSource::Object::Object(File &f, const string &n, const string &k):
213 PackSource::Pack::Loader::Loader(Pack &p):
214 ObjectLoader<Pack>(p)
216 add("file", &Loader::file);
217 add("base_offset", &Pack::base_offset);
220 void PackSource::Pack::Loader::file(const string &fn)
222 obj.files.push_back(File(obj, fn));
223 load_sub(obj.files.back());
227 PackSource::File::Loader::Loader(File &f):
228 ObjectLoader<File>(f)
230 add("object", &Loader::object);
231 add("slice", &File::offset, &File::length);
234 void PackSource::File::Loader::finish()
238 PackSource::Object ob(obj, obj.filename, string());
239 obj.objects.push_back(ob);
243 void PackSource::File::Loader::object(const string &name, const string &kwd)
245 obj.objects.push_back(PackSource::Object(obj, name, kwd));
246 obj.collection = true;
249 } // namespace DataFile