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