]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.cpp
fe9e67db6eabae37ae1b38278d68104b305f2739
[libs/datafile.git] / source / packsource.cpp
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"
6
7 using namespace std;
8
9 namespace {
10
11 void delete_io(Msp::IO::Base *io)
12 {
13         delete io;
14 }
15
16 }
17
18 namespace Msp {
19 namespace DataFile {
20
21 void PackSource::add_pack_file(const string &fn)
22 {
23         add_pack_file(fn, string());
24 }
25
26 void PackSource::add_pack_file(const string &fn, const string &filter)
27 {
28         add_pack(0, fn, filter);
29 }
30
31 void PackSource::add_pack_io(IO::Seekable &io, const string &fn)
32 {
33         add_pack(&io, fn, string());
34 }
35
36 void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter)
37 {
38         add_pack(&io, fn, filter);
39 }
40
41 void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter)
42 {
43         Pack *pack = 0;
44         for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
45                 if(i->get_filename()==fn || (io && i->get_io()==io))
46                         pack = &*i;
47         if(!pack)
48         {
49                 packs.push_back(Pack(io, fn));
50                 pack = &packs.back();
51                 if(io)
52                 {
53                         DataFile::Parser parser(*io, fn);
54                         Pack::Loader loader(*pack);
55                         loader.load(parser);
56                 }
57                 else
58                         DataFile::load(*pack, fn);
59         }
60
61         FileMap pack_files;
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);
66 }
67
68 list<PackSource::FileInfo> PackSource::list_files() const
69 {
70         list<FileInfo> result;
71         for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i)
72                 result.push_back(i->second->get_info());
73         return result;
74 }
75
76 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
77 {
78         ObjectMap::const_iterator i = objects.find(name);
79         if(i==objects.end())
80                 return false;
81
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())
84                 return false;
85
86         return true;
87 }
88
89 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
90 {
91         NameList names;
92         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
93         {
94                 if(!i->second->get_keyword().empty())
95                 {
96                         if(i->second->get_keyword()!=type.get_keyword())
97                                 continue;
98                 }
99                 else if(!type.match_name(i->first))
100                         continue;
101
102                 names.push_back(i->first);
103         }
104
105         return names;
106 }
107
108 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
109 {
110         ObjectMap::const_iterator i = objects.find(name);
111         if(i==objects.end())
112                 return;
113
114         File &file = i->second->get_file();
115         if(file.is_loaded())
116                 return;
117         file.set_loaded();
118
119         RefPtr<IO::Base> in = file.open();
120         Parser parser(*in, file.get_full_name());
121         if(file.is_collection())
122         {
123                 Collection::Loader ldr(coll);
124                 ldr.load(parser);
125         }
126         else
127                 type.load_item(coll, parser, name);
128 }
129
130 IO::Seekable *PackSource::open(const string &fn) const
131 {
132         FileMap::const_iterator i = files.find(fn);
133         if(i!=files.end())
134                 return i->second->open().release();
135
136         return 0;
137 }
138
139
140 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
141         filename(fn),
142         io(i),
143         base_offset(0)
144 { }
145
146 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
147 {
148         if(filter.empty())
149         {
150                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
151                         fm[i->get_filename()] = &*i;
152         }
153         else
154         {
155                 Regex re(filter);
156                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
157                         if(re.match(i->get_filename()))
158                                 fm[i->get_filename()] = &*i;
159         }
160 }
161
162
163 PackSource::File::File(const Pack &p, const string &fn):
164         pack(p),
165         filename(fn),
166         offset(0),
167         length(0),
168         collection(false),
169         loaded(false)
170 { }
171
172 RefPtr<IO::Seekable> PackSource::File::open() const
173 {
174         if(pack.get_io())
175                 // TODO Performance may be poor without buffering
176                 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
177         else
178         {
179                 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
180                 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
181                 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
182                 return io_slice;
183         }
184 }
185
186 PackSource::FileInfo PackSource::File::get_info() const
187 {
188         FileInfo info;
189         info.name = filename;
190         info.size = length;
191         return info;
192 }
193
194 string PackSource::File::get_full_name() const
195 {
196         return format("%s/%s", pack.get_filename(), filename);
197 }
198
199 void PackSource::File::set_loaded()
200 {
201         loaded = true;
202 }
203
204 void PackSource::File::collect_objects(ObjectMap &objs) const
205 {
206         for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
207                 objs[i->get_name()] = &*i;
208 }
209
210
211 PackSource::Object::Object(File &f, const string &n, const string &k):
212         file(f),
213         name(n),
214         keyword(k)
215 { }
216
217
218 PackSource::Pack::Loader::Loader(Pack &p):
219         ObjectLoader<Pack>(p)
220 {
221         add("file",        &Loader::file);
222         add("base_offset", &Pack::base_offset);
223 }
224
225 void PackSource::Pack::Loader::file(const string &fn)
226 {
227         obj.files.push_back(File(obj, fn));
228         load_sub(obj.files.back());
229 }
230
231
232 PackSource::File::Loader::Loader(File &f):
233         ObjectLoader<File>(f)
234 {
235         add("object", &Loader::object);
236         add("slice",  &File::offset, &File::length);
237 }
238
239 void PackSource::File::Loader::finish()
240 {
241         if(!obj.collection)
242         {
243                 PackSource::Object ob(obj, obj.filename, string());
244                 obj.objects.push_back(ob);
245         }
246 }
247
248 void PackSource::File::Loader::object(const string &name, const string &kwd)
249 {
250         obj.objects.push_back(PackSource::Object(obj, name, kwd));
251         obj.collection = true;
252 }
253
254 } // namespace DataFile
255 } // namespace Msp