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