File: VirtualArchive.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (110 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _VIRTUAL_ARCHIVE_H
#define _VIRTUAL_ARCHIVE_H

#include "IArchiveFactory.h"
#include "IArchive.h"

#include <vector>
#include <string>


class CVirtualArchive;

/**
 * Creates virtual non-existing archives which can be created during runtime
 */
class CVirtualArchiveFactory : public IArchiveFactory {
public:
	CVirtualArchiveFactory();
	virtual ~CVirtualArchiveFactory();

	/**
	 * Registers and creates a new virtual archive to the factory
	 * @param filePath Path with which the archive can be found and opened
	 */
	CVirtualArchive* AddArchive(const std::string& fileName);

private:
	IArchive* DoCreateArchive(const std::string& fileName) const override;

	std::vector<CVirtualArchive*> archives;
};

extern CVirtualArchiveFactory* virtualArchiveFactory;

/**
 * A file in a virtual archive
 */
class CVirtualFile
{
public:
	CVirtualFile(int _fid, const std::string& _name): name(_name), fid(_fid) {}

	void WriteZip(void* zf) const;

public:
	std::vector<std::uint8_t> buffer;

	const std::string name;
	int fid;
};


/**
 * An opened virtual archive, as archives get deleted after being
 * opened with IArchiveFactory::DoCreateArchive all the data in a
 * virtual archive is lost.
 * Therefore an 'opened' class is created and the actual archive is
 * preserved
 */
class CVirtualArchiveOpen : public IArchive
{
public:
	CVirtualArchiveOpen(CVirtualArchive* archive, const std::string& fileName);

	int GetType() const override { return ARCHIVE_TYPE_SDV; }

	// virtual archives are stored in memory and as such always open
	bool IsOpen() override { return true; }
	unsigned int NumFiles() const override;
	bool GetFile(unsigned int fid, std::vector<std::uint8_t>& buffer) override;
	void FileInfo(unsigned int fid, std::string& name, int& size) const override;

private:
	CVirtualArchive* archive;
};


/**
 * A virtual archive
 */
class CVirtualArchive
{
public:
	CVirtualArchive(const std::string& _fileName): fileName(_fileName) {}

	CVirtualArchiveOpen* Open();
	CVirtualFile* GetFilePtr(unsigned int fid) { return &files[fid]; }

	unsigned int AddFile(const std::string& file);
	unsigned int NumFiles() const { return (files.size()); }

	bool GetFile(unsigned int fid, std::vector<std::uint8_t>& buffer);
	void FileInfo(unsigned int fid, std::string& name, int& size) const;

	const std::string& GetFileName() const { return fileName; }
	const spring::unordered_map<std::string, unsigned int>& GetNameIndex() const { return lcNameIndex; }

	void WriteToFile();

private:
	friend class CVirtualArchiveOpen;

	std::string fileName;
	std::vector<CVirtualFile> files;
	spring::unordered_map<std::string, unsigned int> lcNameIndex;
};

#endif // _VIRTUAL_ARCHIVE_H