forked from jasp-stats/jasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchivereader.h
147 lines (119 loc) · 3.8 KB
/
archivereader.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Copyright (C) 2013-2018 University of Amsterdam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef ARCHIVEREADER_H
#define ARCHIVEREADER_H
#include <string>
#include <vector>
#include <stdlib.h>
#include <functional>
#include <archive.h>
/**
* @brief The ArchiveReader class - Reads archives.
*
* Reads and extracts entiries from the archive.
*
*/
class ArchiveReader
{
public:
ArchiveReader(){}
ArchiveReader(const std::string &archivePath, const std::string &entryPath);
ArchiveReader(ArchiveReader && other) = default;
~ArchiveReader();
/**
* @brief size Sizeof archive, or entry.
* @return size found.
*/
int size() const { return _exists ? _size : 0;
}
/**
* @brief pos The current position in the file.
* @return Bytes from start of file.
*/
int pos() const { return _currentRead; }
/**
* @brief bytes Available Bytes in the file or entry.
* @return Number bytes still to be read.
*/
int bytesAvailable() const { return _exists ? _size - _currentRead : 0; }
/**
* @brief readData Reads at most maxSize bytes to data.
* @param data Output buffer.
* @param maxSize Maximum number bytes to read.
* @param errorCode On success = 0, On Error < 0
* @return Number bytes read.
*/
int readData(char * data, int maxSize, int &errorCode);
/**
* @brief readAllData Read all file data from current postion.
* @param blockSize - Size of read blocks.
* @param errorCode - Success = 0, Error < 0
* @return
*/
std::string readAllData(int blockSize, int &errorCode);
void openEntry(const std::string &archivePath, const std::string &entryPath);
/**
* @brief close Closes archive/file.
*/
void close();
/**
* @brief reset Closes and reopens file/archive.
*
* Similar to, but slower than seek(0)
*/
void reset();
/**
* @brief isOpen Checks for file/archive being open.
* @return true if file opened...
*/
bool isOpen() {return _isOpen; }
/**
* @brief exists Checks if the archive/file existant.
* @return true if existant (and open).
*/
bool exists() const { return _exists; }
/**
* @brief archiveExists Check if is archive, and achives exists
* @return true if archive (not file) has been opened.
*/
bool archiveExists() const { return _archiveExists; }
/**
* @brief fileName The file name of the last archive entry.
* @return entryPath as passed to Ctor(). Zero length if simple file.
*/
std::string fileName() const;
/**
* @brief Saves the loaded entry as a file in tempfiles folder, progressCallback gets values from 0...1
*/
void writeEntryToTempFiles(std::function<void(float)> progressCallback = std::function<void(float)>());
/**
* @brief extension The file extension of the last archive entry.
* @return Extension suffice of entryPath as passed to Ctor(). Zero length if simple file.
*/
std::string extension() const;
static std::vector<std::string> getEntryPaths(const std::string &archivePath, const std::string &entryBaseDirectory = std::string());
private:
struct archive * _archive = nullptr;
bool _isOpen = false,
_exists = false,
_archiveExists = false;
int _size = 0,
_currentRead = 0;
std::string _archivePath,
_entryPath;
};
#endif // ARCHIVEREADER_H