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 #ifndef CONTRIB_LIBXLS_UTILS_UTILS_LIBXLS_H_ 16 #define CONTRIB_LIBXLS_UTILS_UTILS_LIBXLS_H_ 17 18 #include <fcntl.h> 19 20 #include "absl/log/die_if_null.h" 21 #include "contrib/libxls/sandboxed.h" 22 23 #define XLS_RECORD_FORMULA 0x0006 24 #define XLS_RECORD_MULRK 0x00BD 25 #define XLS_RECORD_BLANK 0x0201 26 #define XLS_RECORD_NUMBER 0x0203 27 #define XLS_RECORD_STRING 0x0207 28 #define XLS_RECORD_RK 0x027E 29 #define XLS_RECORD_BOOL 0x9998 30 #define XLS_RECORD_ERROR 0x9999 31 32 struct LibXlsCell { 33 int type; 34 std::variant<double, bool, std::string> value; 35 }; 36 37 class LibXlsSheet { 38 public: 39 size_t GetRowCount() const; 40 size_t GetColCount() const; 41 absl::StatusOr<LibXlsCell> GetCell(uint32_t row, uint32_t col); 42 43 ~LibXlsSheet(); 44 LibXlsSheet(LibXlsSheet && other)45 LibXlsSheet(LibXlsSheet&& other) { *this = std::move(other); } 46 47 LibXlsSheet& operator=(LibXlsSheet&& other) { 48 using std::swap; 49 50 if (this != &other) { 51 swap(sandbox_, other.sandbox_); 52 swap(rws_, other.rws_); 53 swap(row_, other.row_); 54 swap(col_, other.col_); 55 } 56 return *this; 57 } 58 59 private: 60 friend class LibXlsWorkbook; 61 LibXlsSheet(LibxlsSapiSandbox * sandbox,xlsWorkSheet * rws,size_t row,size_t col)62 LibXlsSheet(LibxlsSapiSandbox* sandbox, xlsWorkSheet* rws, size_t row, 63 size_t col) 64 : sandbox_(ABSL_DIE_IF_NULL(sandbox)), rws_(rws), row_(row), col_(col) {} 65 66 absl::StatusOr<std::string> GetStr(const sapi::v::Struct<xlsCell>& sapi_cell); 67 absl::StatusOr<LibXlsCell> GetNewCell( 68 const sapi::v::Struct<xlsCell>& sapi_cell); 69 70 LibxlsSapiSandbox* sandbox_; 71 xlsWorkSheet* rws_ = nullptr; 72 size_t row_; 73 size_t col_; 74 }; 75 76 class LibXlsWorkbook { 77 public: 78 static absl::StatusOr<LibXlsWorkbook> Open( 79 LibxlsSapiSandbox* sandbox, const std::string& filename, 80 const std::string& encode = "UTF-8"); 81 82 size_t GetSheetCount(); 83 absl::StatusOr<LibXlsSheet> OpenSheet(uint32_t index); 84 85 ~LibXlsWorkbook(); 86 LibXlsWorkbook(LibXlsWorkbook && other)87 LibXlsWorkbook(LibXlsWorkbook&& other) { *this = std::move(other); } 88 89 LibXlsWorkbook& operator=(LibXlsWorkbook&& other) { 90 using std::swap; 91 92 if (this != &other) { 93 swap(sandbox_, other.sandbox_); 94 swap(rwb_, other.rwb_); 95 swap(sheet_count_, other.sheet_count_); 96 } 97 return *this; 98 } 99 100 private: LibXlsWorkbook(LibxlsSapiSandbox * sandbox,xlsWorkBook * rwb,size_t count)101 LibXlsWorkbook(LibxlsSapiSandbox* sandbox, xlsWorkBook* rwb, size_t count) 102 : sandbox_(ABSL_DIE_IF_NULL(sandbox)), 103 rwb_(ABSL_DIE_IF_NULL(rwb)), 104 sheet_count_(count) {} 105 106 LibxlsSapiSandbox* sandbox_; 107 xlsWorkBook* rwb_ = nullptr; 108 size_t sheet_count_; 109 }; 110 111 #endif // CONTRIB_LIBXLS_UTILS_UTILS_LIBXLS_H_ 112