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