-
Notifications
You must be signed in to change notification settings - Fork 3
/
tar.hpp
78 lines (64 loc) · 2.67 KB
/
tar.hpp
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
#ifndef TAR_HPP
#define TAR_HPP
#include <iostream>
#include <string>
namespace tar
{
typedef long long unsigned file_size_t;
extern const int FILE_NAME_LENGTH;
////////////////////////////////////////
// Writing raw data
////////////////////////////////////////
class writer {
std::ostream& _dst;
public:
writer(std::ostream& dst) : _dst(dst) {}
~writer() { finish(); }
// Append the data specified by |data| and |file_size| into the
// tar file represented by |dst|.
// In the tar file, the data will be available at |path_in_tar|.
void put(std::string path_in_tar,
char const * const data,
const file_size_t data_size);
// Write empty folder at |path_in_tar|.
// NOTE: to specify folders for files, just use / in the path
// passed to |put()|.
void put_directory(std::string path_in_tar);
// Call after everything has been added to the tar represented
// by |dst| to make it a valid tar file.
void finish();
};
////////////////////////////////////////
// Reading raw data
////////////////////////////////////////
class reader {
std::istream& _inp;
struct {
std::string file_name;
file_size_t file_size;
} _cached_header_data;
bool _cached_header_data_valid;
void _cache_header();
int _number_of_files;
public:
// Constructor, pass input stream |inp| pointing to a tar file.
reader(std::istream& inp)
: _inp(inp)
, _cached_header_data_valid(false)
, _number_of_files(-1) {}
// Returns true iff another file can be read from |inp|.
bool contains_another_file();
// Returns file name of next file in |inp|.
std::string get_next_file_name();
// Returns file size of next file in |inp|. Use to allocate
// memory for the |read_next_file()| call.
file_size_t get_next_file_size();
// Read next file in |inp| to |data|.
void read_next_file(char * const data);
// Skip next file in |inp|.
void skip_next_file();
// Returns number of files in tar at |inp|.
int number_of_files();
};
}
#endif // TAR_HPP