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