]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.h
Remove the loaded flag from PackSource files
[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                 IO::Seekable *io;
52                 unsigned base_offset;
53                 std::list<File> files;
54
55         public:
56                 Pack(IO::Seekable *, const std::string &);
57
58                 const std::string &get_filename() const { return filename; }
59                 IO::Seekable *get_io() const { return io; }
60                 unsigned get_base_offset() const { return base_offset; }
61
62                 void collect_files(FileMap &, const std::string &) const;
63         };
64
65         class File
66         {
67         public:
68                 class Loader: public ObjectLoader<File>
69                 {
70                 public:
71                         Loader(File &);
72                 private:
73                         virtual void finish();
74                         void object(const std::string &, const std::string &);
75                 };
76
77         private:
78                 const Pack &pack;
79                 std::string filename;
80                 unsigned offset;
81                 unsigned length;
82                 bool collection;
83                 std::list<Object> objects;
84
85         public:
86                 File(const Pack &, const std::string &);
87
88                 RefPtr<IO::Seekable> open() const;
89                 const std::string &get_filename() const { return filename; }
90                 FileInfo get_info() const;
91                 std::string get_full_name() const;
92                 bool is_collection() const { return collection; }
93
94                 void collect_objects(ObjectMap &) const;
95         };
96
97         class Object
98         {
99         private:
100                 File &file;
101                 std::string name;
102                 std::string keyword;
103
104         public:
105                 Object(File &, const std::string &, const std::string &);
106
107                 File &get_file() const { return file; }
108                 const std::string &get_name() const { return name; }
109                 const std::string &get_keyword() const { return keyword; }
110         };
111
112         std::list<Pack> packs;
113         FileMap files;
114         ObjectMap objects;
115
116 public:
117         /// Adds a pack file to load objects from.  The index is read immediately.
118         void add_pack_file(const std::string &);
119
120         /** Adds a pack file with a regex to filter logical files.  The index is
121         read on the first call; subsequent calls will use cached data. */
122         void add_pack_file(const std::string &, const std::string &);
123
124         /** Adds a pack from an existing seekable I/O object.  The same object is
125         used for all accesses to the pack, so it must not be deleted before the
126         PackSource. */
127         void add_pack_io(IO::Seekable &, const std::string & = std::string());
128
129         /** Adds a pack from an I/O object with a regex to filter logical files.
130         Multiple filters for the same I/O object can be added with repeated calls. */
131         void add_pack_io(IO::Seekable &, const std::string &, const std::string &);
132
133 private:
134         void add_pack(IO::Seekable *, const std::string &, const std::string &);
135
136 public:
137         /// Returns information about the files in the pack.
138         std::list<FileInfo> list_files() const;
139
140         virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
141         virtual NameList get_names(const CollectionItemTypeBase &) const;
142         virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const;
143         virtual IO::Seekable *open(const std::string &) const;
144 };
145
146 } // namespace DataFile
147 } // namespace Msp
148
149 #endif