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