/
iarchive.h
105 lines (87 loc) · 3.24 KB
/
iarchive.h
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
#pragma once
/**
* \file iarchive.h
* Types relating to the use of ZIP archives (PK4 files) and their contents.
* \ingroup vfs
*/
#include "ModResource.h"
#include "imodule.h"
#include <cstddef>
#include "itextstream.h"
#include <string>
class InputStream;
/**
* A file opened in binary mode.
* \ingroup vfs
*/
class ArchiveFile
{
public:
/// \brief destructor
virtual ~ArchiveFile() {}
/// \brief Returns the size of the file data in bytes.
virtual std::size_t size() const = 0;
/// \brief Returns the path to this file (relative to the filesystem root)
virtual const std::string& getName() const = 0;
/// \brief Returns the stream associated with this file.
/// Subsequent calls return the same stream.
/// The stream may be read forwards until it is exhausted.
/// The stream remains valid for the lifetime of the file.
virtual InputStream& getInputStream() = 0;
};
typedef std::shared_ptr<ArchiveFile> ArchiveFilePtr;
/**
* A file opened in text mode.
* \ingroup vfs
*/
class ArchiveTextFile :
public ModResource
{
public:
/// \brief Returns the path to this file (relative to the filesystem root)
virtual const std::string& getName() const = 0;
/// \brief Returns the stream associated with this file.
/// Subsequent calls return the same stream.
/// The stream may be read forwards until it is exhausted.
/// The stream remains valid for the lifetime of the file.
virtual TextInputStream& getInputStream() = 0;
};
typedef std::shared_ptr<ArchiveTextFile> ArchiveTextFilePtr;
/**
* Representation of an archive in the virtual filesystem.
* This might be a PK4/ZIP file or a regular mod directory.
*
* \ingroup vfs
*/
class IArchive
{
public:
typedef std::shared_ptr<IArchive> Ptr;
virtual ~IArchive() {}
class Visitor
{
public:
virtual ~Visitor() {}
// Invoked for each file in an Archive
virtual void visitFile(const std::string& name) = 0;
// Invoked for each directory in an Archive. Return true to skip the directory.
virtual bool visitDirectory(const std::string& name, std::size_t depth) = 0;
};
/// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
/// Name comparisons are case-insensitive.
virtual ArchiveFilePtr openFile(const std::string& name) = 0;
/// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
/// Name comparisons are case-insensitive.
virtual ArchiveTextFilePtr openTextFile(const std::string& name) = 0;
/// Returns true if the file identified by \p name can be opened.
/// Name comparisons are case-insensitive.
virtual bool containsFile(const std::string& name) = 0;
/// \brief Performs a depth-first traversal of the archive tree starting at \p root.
/// Traverses the entire tree if \p root is "".
/// When a file is encountered, calls \c visitor.file passing the file name.
/// When a directory is encountered, calls \c visitor.directory passing the directory name.
/// Skips the directory if \c visitor.directory returned true.
/// Root comparisons are case-insensitive.
/// Names are mixed-case.
virtual void traverse(Visitor& visitor, const std::string& root) = 0;
};