• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iomanip>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 
25 #include "absl/flags/flag.h"
26 #include "absl/flags/parse.h"
27 #include "absl/log/globals.h"
28 #include "absl/log/initialize.h"
29 #include "contrib/libxls/sandboxed.h"
30 #include "contrib/libxls/utils/utils_libxls.h"
31 
32 ABSL_FLAG(uint32_t, sheet, 0, "sheet number");
33 
main(int argc,char * argv[])34 int main(int argc, char* argv[]) {
35   std::string prog_name(argv[0]);
36   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
37   std::vector<char*> args = absl::ParseCommandLine(argc, argv);
38   absl::InitializeLog();
39 
40   if (args.size() != 2) {
41     std::cerr << "Usage:\n  " << prog_name << " INPUT\n";
42     return EXIT_FAILURE;
43   }
44 
45   LibxlsSapiSandbox sandbox(args[1]);
46   if (!sandbox.Init().ok()) {
47     std::cerr << "Unable to start sandbox\n";
48     return EXIT_FAILURE;
49   }
50 
51   absl::StatusOr<LibXlsWorkbook> wb = LibXlsWorkbook::Open(&sandbox, args[1]);
52 
53   uint32_t nr_sheet = absl::GetFlag(FLAGS_sheet);
54 
55   absl::StatusOr<LibXlsSheet> sheet = wb->OpenSheet(nr_sheet);
56   if (!sheet.ok()) {
57     std::cerr << "Unable to switch sheet: ";
58     std::cerr << sheet.status() << "\n";
59     return EXIT_FAILURE;
60   }
61 
62   for (size_t row = 0; row < sheet->GetRowCount(); ++row) {
63     for (size_t col = 0; col < sheet->GetColCount(); ++col) {
64       absl::StatusOr<LibXlsCell> cell = sheet->GetCell(row, col);
65       if (!cell.ok()) {
66         std::cerr << "Unable to get cell: ";
67         std::cerr << cell.status() << "\n";
68         return EXIT_FAILURE;
69       }
70       switch (cell->type) {
71         case XLS_RECORD_NUMBER:
72           std::cout << std::setw(16) << std::get<double>(cell->value) << " | ";
73           break;
74         case XLS_RECORD_STRING:
75           std::cout << std::setw(16) << std::get<std::string>(cell->value)
76                     << " | ";
77           break;
78         case XLS_RECORD_BOOL:
79           std::cout << std::setw(16) << std::get<bool>(cell->value) << " | ";
80           break;
81         case XLS_RECORD_BLANK:
82           std::cout << std::setw(16) << " | ";
83           break;
84         case XLS_RECORD_ERROR:
85           std::cout << "error"
86                     << "\n";
87           break;
88       }
89     }
90     std::cout << "\n";
91   }
92   return EXIT_SUCCESS;
93 }
94