]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.h
Add an API to open files from a collection's sources
[libs/datafile.git] / source / packsource.h
1 #ifndef MSP_DATAFILE_PACKSOURCE_H_
2 #define MSP_DATAFILE_PACKSOURCE_H_
3
4 #include <msp/core/refptr.h>
5 #include "collectionsource.h"
6 #include "objectloader.h"
7
8 namespace Msp {
9 namespace DataFile {
10
11 /**
12 A source that loads data from pack files.  As opposed to plain collection
13 files, pack files are composed from a number of logical files.  They also
14 contain an index of objects contained in the pack and which logical files they
15 are in.  This allows the pack to be loaded in a piecewise manner instead of all
16 at once.
17
18 It's possible for a pack file to contain plain collection files as well.  When
19 an object from such a file is requested, the entire sub-collection it is stored
20 in is loaded.
21 */
22 class PackSource: public CollectionSource
23 {
24 private:
25         class File;
26         struct Object;
27
28         typedef std::map<std::string, const File *> FileMap;
29         typedef std::map<std::string, const Object *> ObjectMap;
30
31         class Pack
32         {
33         public:
34                 class Loader: public ObjectLoader<Pack>
35                 {
36                 public:
37                         Loader(Pack &);
38                 private:
39                         void file(const std::string &);
40                 };
41
42         private:
43                 std::string filename;
44                 unsigned base_offset;
45                 std::list<File> files;
46
47         public:
48                 Pack(const std::string &);
49
50                 const std::string &get_filename() const { return filename; }
51                 unsigned get_base_offset() const { return base_offset; }
52
53                 void collect_files(FileMap &, const std::string &) const;
54         };
55
56         class File
57         {
58         public:
59                 class Loader: public ObjectLoader<File>
60                 {
61                 public:
62                         Loader(File &);
63                 private:
64                         virtual void finish();
65                         void object(const std::string &, const std::string &);
66                 };
67
68         private:
69                 const Pack &pack;
70                 std::string filename;
71                 unsigned offset;
72                 unsigned length;
73                 bool collection;
74                 std::list<Object> objects;
75                 bool loaded;
76
77         public:
78                 File(const Pack &, const std::string &);
79
80                 RefPtr<IO::Seekable> open() const;
81                 const std::string &get_filename() const { return filename; }
82                 std::string get_full_name() const;
83                 bool is_collection() const { return collection; }
84
85                 void set_loaded();
86                 bool is_loaded() const { return loaded; }
87
88                 void collect_objects(ObjectMap &) const;
89         };
90
91         class Object
92         {
93         private:
94                 File &file;
95                 std::string name;
96                 std::string keyword;
97
98         public:
99                 Object(File &, const std::string &, const std::string &);
100
101                 File &get_file() const { return file; }
102                 const std::string &get_name() const { return name; }
103                 const std::string &get_keyword() const { return keyword; }
104         };
105
106         std::list<Pack> packs;
107         FileMap files;
108         ObjectMap objects;
109
110 public:
111         /// Adds a pack file to load objects from.  The index is read immediately.
112         void add_pack_file(const std::string &);
113
114         /** Adds a pack file with a regex to filter logical files.  The index is
115         read on the first call; subsequent calls will use cached data. */
116         void add_pack_file(const std::string &, const std::string &);
117
118         virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
119         virtual NameList get_names(const CollectionItemTypeBase &) const;
120         virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const;
121         virtual IO::Seekable *open(const std::string &) const;
122 };
123
124 } // namespace DataFile
125 } // namespace Msp
126
127 #endif