streaming_workbook_reader.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2017-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <fstream>
  24. #include <xlnt/cell/cell.hpp>
  25. #include <xlnt/packaging/manifest.hpp>
  26. #include <xlnt/utils/optional.hpp>
  27. #include <xlnt/workbook/streaming_workbook_reader.hpp>
  28. #include <xlnt/workbook/workbook.hpp>
  29. #include <xlnt/worksheet/worksheet.hpp>
  30. #include <detail/implementations/workbook_impl.hpp>
  31. #include <detail/serialization/open_stream.hpp>
  32. #include <detail/serialization/vector_streambuf.hpp>
  33. #include <detail/serialization/xlsx_consumer.hpp>
  34. namespace xlnt {
  35. streaming_workbook_reader::streaming_workbook_reader()
  36. {
  37. }
  38. streaming_workbook_reader::~streaming_workbook_reader()
  39. {
  40. close();
  41. }
  42. void streaming_workbook_reader::close()
  43. {
  44. if (consumer_)
  45. {
  46. consumer_.reset(nullptr);
  47. stream_buffer_.reset(nullptr);
  48. }
  49. }
  50. bool streaming_workbook_reader::has_cell()
  51. {
  52. return consumer_->has_cell();
  53. }
  54. cell streaming_workbook_reader::read_cell()
  55. {
  56. return consumer_->read_cell();
  57. }
  58. bool streaming_workbook_reader::has_worksheet(const std::string &name)
  59. {
  60. auto titles = sheet_titles();
  61. return std::find(titles.begin(), titles.end(), name) != titles.end();
  62. }
  63. void streaming_workbook_reader::begin_worksheet(const std::string &title)
  64. {
  65. if (!has_worksheet(title))
  66. {
  67. throw xlnt::exception("sheet not found");
  68. }
  69. worksheet_rel_id_ = workbook_->impl().sheet_title_rel_id_map_.at(title);
  70. const auto workbook_rel = workbook_->manifest()
  71. .relationship(path("/"), relationship_type::office_document);
  72. const auto worksheet_rel = workbook_->manifest()
  73. .relationship(workbook_rel.target().path(), worksheet_rel_id_);
  74. auto rel_chain = std::vector<relationship>{workbook_rel, worksheet_rel};
  75. const auto &manifest = consumer_->target_.manifest();
  76. const auto part_path = manifest.canonicalize(rel_chain);
  77. auto part_stream_buffer = consumer_->archive_->open(part_path);
  78. part_stream_buffer_.swap(part_stream_buffer);
  79. part_stream_.reset(new std::istream(part_stream_buffer_.get()));
  80. parser_.reset(new xml::parser(*part_stream_, part_path.string()));
  81. consumer_->parser_ = parser_.get();
  82. consumer_->current_worksheet_ = nullptr;
  83. for (auto &impl : workbook_->impl().worksheets_)
  84. {
  85. if (impl.title_ == title)
  86. {
  87. consumer_->current_worksheet_ = &impl;
  88. }
  89. }
  90. if (consumer_->current_worksheet_ == nullptr)
  91. {
  92. throw xlnt::exception("sheet not found");
  93. }
  94. consumer_->read_worksheet_begin(worksheet_rel_id_);
  95. }
  96. worksheet streaming_workbook_reader::end_worksheet()
  97. {
  98. return consumer_->read_worksheet_end(worksheet_rel_id_);
  99. }
  100. void streaming_workbook_reader::open(const std::vector<std::uint8_t> &data)
  101. {
  102. stream_buffer_.reset(new detail::vector_istreambuf(data));
  103. stream_.reset(new std::istream(stream_buffer_.get()));
  104. open(*stream_);
  105. }
  106. void streaming_workbook_reader::open(const std::string &filename)
  107. {
  108. stream_.reset(new std::ifstream());
  109. xlnt::detail::open_stream(static_cast<std::ifstream &>(*stream_), filename);
  110. open(*stream_);
  111. }
  112. #ifdef _MSC_VER
  113. void streaming_workbook_reader::open(const std::wstring &filename)
  114. {
  115. stream_.reset(new std::ifstream());
  116. xlnt::detail::open_stream(static_cast<std::ifstream &>(*stream_), filename);
  117. open(*stream_);
  118. }
  119. #endif
  120. void streaming_workbook_reader::open(const xlnt::path &filename)
  121. {
  122. stream_.reset(new std::ifstream());
  123. xlnt::detail::open_stream(static_cast<std::ifstream &>(*stream_), filename.string());
  124. open(*stream_);
  125. }
  126. void streaming_workbook_reader::open(std::istream &stream)
  127. {
  128. workbook_.reset(new workbook());
  129. consumer_.reset(new detail::xlsx_consumer(*workbook_));
  130. consumer_->open(stream);
  131. const auto workbook_rel = workbook_->manifest()
  132. .relationship(path("/"), relationship_type::office_document);
  133. const auto workbook_path = workbook_rel.target().path();
  134. }
  135. void streaming_workbook_reader::open(std::unique_ptr<std::streambuf> &&buffer)
  136. {
  137. stream_buffer_.swap(buffer);
  138. stream_.reset(new std::istream(stream_buffer_.get()));
  139. open(*stream_);
  140. }
  141. std::vector<std::string> streaming_workbook_reader::sheet_titles()
  142. {
  143. return workbook_->sheet_titles();
  144. }
  145. } // namespace xlnt