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