-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
86 lines (72 loc) · 1.77 KB
/
main.cpp
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
#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 <xlnt/xlnt.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 sheet names of XLSX workbook given as filename or piped through stdin
// or 2. Print all cells of worksheet in aforementioned workbook
int main(int argc, char *argv[])
{
xlnt::workbook wb;
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 XLSX file path
{
if(args.size() < 1)
{
std::cout << "usage: " << argv[0] << " xlsx_file [sheet_to_print]" << std::endl;
std::cout << " (xlsx_file can be replaced by data piped from standard input)" << std::endl;
return 1;
}
wb.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
wb.load(std::cin);
}
if(args.empty())
{
for(auto ws : wb)
{
std::cout << ws.get_title() << " ";
}
std::cout << std::endl;
}
else
{
auto ws = wb.get_sheet_by_name(args.front());
for(auto row : ws.rows())
{
for(auto cell : row)
{
std::cout << cell << "| ";
}
std::cout << std::endl;
}
}
return 0;
}