]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.h
Export file metadata from PackSource
[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 public:
25         struct FileInfo
26         {
27                 std::string name;
28                 unsigned size;
29         };
30
31 private:
32         class File;
33         class Object;
34
35         typedef std::map<std::string, const File *> FileMap;
36         typedef std::map<std::string, const Object *> ObjectMap;
37
38         class Pack
39         {
40         public:
41                 class Loader: public ObjectLoader<Pack>
42                 {
43                 public:
44                         Loader(Pack &);
45                 private:
46                         void file(const std::string &);
47                 };
48
49         private:
50                 std::string filename;
51                 unsigned base_offset;
52                 std::list<File> files;
53
54         public:
55                 Pack(const std::string &);
56
57                 const std::string &get_filename() const { return filename; }
58                 unsigned get_base_offset() const { return base_offset; }
59
60                 void collect_files(FileMap &, const std::string &) const;
61         };
62
63         class File
64         {
65         public:
66                 class Loader: public ObjectLoader<File>
67                 {
68                 public:
69                         Loader(File &);
70                 private:
71                         virtual void finish();
72                         void object(const std::string &, const std::string &);
73                 };
74
75         private:
76                 const Pack &pack;
77                 std::string filename;
78                 unsigned offset;
79                 unsigned length;
80                 bool collection;
81                 std::list<Object> objects;
82                 bool loaded;
83
84         public:
85                 File(const Pack &, const std::string &);
86
87                 RefPtr<IO::Seekable> open() const;
88                 const std::string &get_filename() const { return filename; }
89                 FileInfo get_info() const;
90                 std::string get_full_name() const;
91                 bool is_collection() const { return collection; }
92
93                 void set_loaded();
94                 bool is_loaded() const { return loaded; }
95
96                 void collect_objects(ObjectMap &) const;
97         };
98
99         class Object
100         {
101         private:
102                 File &file;
103                 std::string name;
104                 std::string keyword;
105
106         public:
107                 Object(File &, const std::string &, const std::string &);
108
109                 File &get_file() const { return file; }
110                 const std::string &get_name() const { return name; }
111                 const std::string &get_keyword() const { return keyword; }
112         };
113
114         std::list<Pack> packs;
115         FileMap files;
116         ObjectMap objects;
117
118 public:
119         /// Adds a pack file to load objects from.  The index is read immediately.
120         void add_pack_file(const std::string &);
121
122         /** Adds a pack file with a regex to filter logical files.  The index is
123         read on the first call; subsequent calls will use cached data. */
124         void add_pack_file(const std::string &, const std::string &);
125
126         /// Returns information about the files in the pack.
127         std::list<FileInfo> list_files() const;
128
129         virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
130         virtual NameList get_names(const CollectionItemTypeBase &) const;
131         virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const;
132         virtual IO::Seekable *open(const std::string &) const;
133 };
134
135 } // namespace DataFile
136 } // namespace Msp
137
138 #endif