1*ec63e07aSXin Li // Copyright 2022 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li // https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li
15*ec63e07aSXin Li #include <fcntl.h>
16*ec63e07aSXin Li #include <unistd.h>
17*ec63e07aSXin Li
18*ec63e07aSXin Li #include <cstdlib>
19*ec63e07aSXin Li #include <fstream>
20*ec63e07aSXin Li #include <iostream>
21*ec63e07aSXin Li #include <string>
22*ec63e07aSXin Li #include <vector>
23*ec63e07aSXin Li
24*ec63e07aSXin Li #include "absl/flags/flag.h"
25*ec63e07aSXin Li #include "absl/flags/parse.h"
26*ec63e07aSXin Li #include "absl/log/globals.h"
27*ec63e07aSXin Li #include "absl/log/initialize.h"
28*ec63e07aSXin Li #include "contrib/libxls/sandboxed.h"
29*ec63e07aSXin Li #include "contrib/libxls/utils/utils_libxls.h"
30*ec63e07aSXin Li
31*ec63e07aSXin Li ABSL_FLAG(uint32_t, sheet, 0, "sheet number");
32*ec63e07aSXin Li
main(int argc,char * argv[])33*ec63e07aSXin Li int main(int argc, char* argv[]) {
34*ec63e07aSXin Li std::string prog_name(argv[0]);
35*ec63e07aSXin Li absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
36*ec63e07aSXin Li std::vector<char*> args = absl::ParseCommandLine(argc, argv);
37*ec63e07aSXin Li absl::InitializeLog();
38*ec63e07aSXin Li
39*ec63e07aSXin Li if (args.size() != 2) {
40*ec63e07aSXin Li std::cerr << "Usage:\n " << prog_name << " INPUT\n";
41*ec63e07aSXin Li return EXIT_FAILURE;
42*ec63e07aSXin Li }
43*ec63e07aSXin Li
44*ec63e07aSXin Li LibxlsSapiSandbox sandbox(args[1]);
45*ec63e07aSXin Li if (!sandbox.Init().ok()) {
46*ec63e07aSXin Li std::cerr << "Unable to start sandbox\n";
47*ec63e07aSXin Li return EXIT_FAILURE;
48*ec63e07aSXin Li }
49*ec63e07aSXin Li
50*ec63e07aSXin Li absl::StatusOr<LibXlsWorkbook> wb = LibXlsWorkbook::Open(&sandbox, args[1]);
51*ec63e07aSXin Li
52*ec63e07aSXin Li uint32_t nr_sheet = absl::GetFlag(FLAGS_sheet);
53*ec63e07aSXin Li
54*ec63e07aSXin Li absl::StatusOr<LibXlsSheet> sheet = wb->OpenSheet(nr_sheet);
55*ec63e07aSXin Li if (!sheet.ok()) {
56*ec63e07aSXin Li std::cerr << "Unable to switch sheet: ";
57*ec63e07aSXin Li std::cerr << sheet.status() << "\n";
58*ec63e07aSXin Li return EXIT_FAILURE;
59*ec63e07aSXin Li }
60*ec63e07aSXin Li
61*ec63e07aSXin Li for (size_t row = 0; row < sheet->GetRowCount(); ++row) {
62*ec63e07aSXin Li for (size_t col = 0; col < sheet->GetColCount(); ++col) {
63*ec63e07aSXin Li absl::StatusOr<LibXlsCell> cell = sheet->GetCell(row, col);
64*ec63e07aSXin Li if (!cell.ok()) {
65*ec63e07aSXin Li std::cerr << "Unable to get cell: ";
66*ec63e07aSXin Li std::cerr << cell.status() << "\n";
67*ec63e07aSXin Li return EXIT_FAILURE;
68*ec63e07aSXin Li }
69*ec63e07aSXin Li switch (cell->type) {
70*ec63e07aSXin Li case XLS_RECORD_NUMBER:
71*ec63e07aSXin Li std::cout << std::setw(16) << std::get<double>(cell->value) << " | ";
72*ec63e07aSXin Li break;
73*ec63e07aSXin Li case XLS_RECORD_STRING:
74*ec63e07aSXin Li std::cout << std::setw(16) << std::get<std::string>(cell->value)
75*ec63e07aSXin Li << " | ";
76*ec63e07aSXin Li break;
77*ec63e07aSXin Li case XLS_RECORD_BOOL:
78*ec63e07aSXin Li std::cout << std::setw(16) << std::get<bool>(cell->value) << " | ";
79*ec63e07aSXin Li break;
80*ec63e07aSXin Li case XLS_RECORD_BLANK:
81*ec63e07aSXin Li std::cout << std::setw(16) << " | ";
82*ec63e07aSXin Li break;
83*ec63e07aSXin Li case XLS_RECORD_ERROR:
84*ec63e07aSXin Li std::cout << "error"
85*ec63e07aSXin Li << "\n";
86*ec63e07aSXin Li break;
87*ec63e07aSXin Li }
88*ec63e07aSXin Li }
89*ec63e07aSXin Li std::cout << "\n";
90*ec63e07aSXin Li }
91*ec63e07aSXin Li return EXIT_SUCCESS;
92*ec63e07aSXin Li }
93