]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.cpp
Use IO::Slice to constrain access to logical files
[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         Pack *pack = 0;
29         for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
30                 if(i->get_filename()==fn)
31                         pack = &*i;
32         if(!pack)
33         {
34                 packs.push_back(Pack(fn));
35                 pack = &packs.back();
36                 DataFile::load(*pack, fn);
37         }
38
39         FileMap pack_files;
40         pack->collect_files(pack_files, filter);
41         for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
42                 i->second->collect_objects(objects);
43 }
44
45 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
46 {
47         ObjectMap::const_iterator i = objects.find(name);
48         if(i==objects.end())
49                 return false;
50
51         // If the object has a keyword, it must match that of the type
52         if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
53                 return false;
54
55         return true;
56 }
57
58 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
59 {
60         NameList names;
61         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
62         {
63                 if(!i->second->get_keyword().empty())
64                 {
65                         if(i->second->get_keyword()!=type.get_keyword())
66                                 continue;
67                 }
68                 else if(!type.match_name(i->first))
69                         continue;
70
71                 names.push_back(i->first);
72         }
73
74         return names;
75 }
76
77 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
78 {
79         ObjectMap::const_iterator i = objects.find(name);
80         if(i==objects.end())
81                 return;
82
83         File &file = i->second->get_file();
84         if(file.is_loaded())
85                 return;
86         file.set_loaded();
87
88         RefPtr<IO::Base> in = file.open();
89         Parser parser(*in, file.get_full_name());
90         if(file.is_collection())
91         {
92                 Collection::Loader ldr(coll);
93                 ldr.load(parser);
94         }
95         else
96                 type.load_item(coll, parser, name);
97 }
98
99
100 PackSource::Pack::Pack(const string &fn):
101         filename(fn),
102         base_offset(0)
103 { }
104
105 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
106 {
107         if(filter.empty())
108         {
109                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
110                         fm[i->get_filename()] = &*i;
111         }
112         else
113         {
114                 Regex re(filter);
115                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
116                         if(re.match(i->get_filename()))
117                                 fm[i->get_filename()] = &*i;
118         }
119 }
120
121
122 PackSource::File::File(const Pack &p, const string &fn):
123         pack(p),
124         filename(fn),
125         offset(0),
126         length(0),
127         collection(false),
128         loaded(false)
129 { }
130
131 RefPtr<IO::Base> PackSource::File::open() const
132 {
133         IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
134         IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
135         io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
136         return io_slice;
137 }
138
139 string PackSource::File::get_full_name() const
140 {
141         return format("%s/%s", pack.get_filename(), filename);
142 }
143
144 void PackSource::File::set_loaded()
145 {
146         loaded = true;
147 }
148
149 void PackSource::File::collect_objects(ObjectMap &objs) const
150 {
151         for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
152                 objs[i->get_name()] = &*i;
153 }
154
155
156 PackSource::Object::Object(File &f, const string &n, const string &k):
157         file(f),
158         name(n),
159         keyword(k)
160 { }
161
162
163 PackSource::Pack::Loader::Loader(Pack &p):
164         ObjectLoader<Pack>(p)
165 {
166         add("file",        &Loader::file);
167         add("base_offset", &Pack::base_offset);
168 }
169
170 void PackSource::Pack::Loader::file(const string &fn)
171 {
172         obj.files.push_back(File(obj, fn));
173         load_sub(obj.files.back());
174 }
175
176
177 PackSource::File::Loader::Loader(File &f):
178         ObjectLoader<File>(f)
179 {
180         add("object", &Loader::object);
181         add("slice",  &File::offset, &File::length);
182 }
183
184 void PackSource::File::Loader::finish()
185 {
186         if(!obj.collection)
187         {
188                 PackSource::Object ob(obj, obj.filename, string());
189                 obj.objects.push_back(ob);
190         }
191 }
192
193 void PackSource::File::Loader::object(const string &name, const string &kwd)
194 {
195         obj.objects.push_back(PackSource::Object(obj, name, kwd));
196         obj.collection = true;
197 }
198
199 } // namespace DataFile
200 } // namespace Msp