Repository navigation
Expand file tree
/
Copy pathpipe.cpp
More file actions
71 lines (60 loc) · 1.59 KB
/
Copy pathpipe.cpp
File metadata and controls
71 lines (60 loc) · 1.59 KB
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
#include <cstdio>
// Platform-specific includes
#ifdef _WIN32
#include <fcntl.h> // For O_BINARY
#include <io.h> // For _setmode, _fileno, _isatty
#else // POSIX
#include <unistd.h> // For fileno, isatty
#endif
#include "zip_file.hpp"
namespace {
// Returns true if program is being run directly
// If this is false, we assume that input will be given through stdin
bool is_tty()
{
#ifdef _WIN32
return _isatty(_fileno(stdin)) != 0;
#else
return isatty(fileno(stdin)) != 0;
#endif
}
} // namespace
// Either 1. Print directory structure of zip file given as filename or piped through stdin
// or 2. Print contents of file contained in aforementioned zip file
int main(int argc, char *argv[])
{
zip_file file;
std::vector<std::string> args;
for(int i = 1; i < argc; i++)
{
args.push_back(argv[i]);
}
if(is_tty()) // Expect first argument to be zip file path
{
if(args.size() < 1)
{
std::cout << "usage: " << argv[0] << " zip_file [file_to_print]" << std::endl;
std::cout << " (zip_file can be replaced by data piped from standard input)" << std::endl;
return 1;
}
file.load(args.front());
args.erase(args.begin());
}
else
{
#ifdef _WIN32
// Ensure that we are reading the raw binary data when using a pipe in Windows.
_setmode(_fileno(stdin), O_BINARY);
#endif
file.load(std::cin);
}
if(args.empty())
{
file.printdir();
}
else
{
std::cout << file.read(args.front()) << std::endl;
}
return 0;
}