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_writer.hpp>
31 : : #include <xlnt/workbook/workbook.hpp>
32 : : #include <xlnt/worksheet/worksheet.hpp>
33 : : #include <detail/implementations/cell_impl.hpp>
34 : : #include <detail/implementations/worksheet_impl.hpp>
35 : : #include <detail/serialization/open_stream.hpp>
36 : : #include <detail/serialization/vector_streambuf.hpp>
37 : : #include <detail/serialization/xlsx_producer.hpp>
38 : :
39 : : namespace xlnt {
40 : :
41 :CBC 1 : streaming_workbook_writer::streaming_workbook_writer()
42 : : {
43 : 1 : }
44 : :
45 : 1 : streaming_workbook_writer::~streaming_workbook_writer()
46 : : {
47 : 1 : close();
48 : 1 : }
49 : :
50 : 1 : void streaming_workbook_writer::close()
51 : : {
52 [ + - ]: 1 : if (producer_)
53 : : {
54 : 1 : producer_.reset(nullptr);
55 : 1 : stream_buffer_.reset(nullptr);
56 : : }
57 : 1 : }
58 : :
59 : 2 : cell streaming_workbook_writer::add_cell(const cell_reference &ref)
60 : : {
61 : 2 : return producer_->add_cell(ref);
62 : : }
63 : :
64 : 1 : worksheet streaming_workbook_writer::add_worksheet(const std::string &title)
65 : : {
66 : 1 : return producer_->add_worksheet(title);
67 : : }
68 : :
69 :UBC 0 : void streaming_workbook_writer::open(std::vector<std::uint8_t> &data)
70 : : {
71 [ # # # ]: 0 : stream_buffer_.reset(new detail::vector_ostreambuf(data));
72 [ # # # ]: 0 : stream_.reset(new std::ostream(stream_buffer_.get()));
73 : 0 : open(*stream_);
74 : 0 : }
75 : :
76 :CBC 1 : void streaming_workbook_writer::open(const std::string &filename)
77 : : {
78 [ + - - ]: 1 : stream_.reset(new std::ofstream());
79 : 1 : xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename);
80 : 1 : open(*stream_);
81 : 1 : }
82 : :
83 : : #ifdef _MSC_VER
84 : : void streaming_workbook_writer::open(const std::wstring &filename)
85 : : {
86 : : stream_.reset(new std::ofstream());
87 : : xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename);
88 : : open(*stream_);
89 : : }
90 : : #endif
91 : :
92 :UBC 0 : void streaming_workbook_writer::open(const xlnt::path &filename)
93 : : {
94 [ # # # ]: 0 : stream_.reset(new std::ofstream());
95 : 0 : xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename.string());
96 : 0 : open(*stream_);
97 : 0 : }
98 : :
99 :CBC 1 : void streaming_workbook_writer::open(std::ostream &stream)
100 : : {
101 [ + - - ]: 1 : workbook_.reset(new workbook());
102 [ + - - ]: 1 : producer_.reset(new detail::xlsx_producer(*workbook_));
103 : 1 : producer_->open(stream);
104 [ + + - - ]: 2 : producer_->current_worksheet_ = new detail::worksheet_impl(workbook_.get(), 1, "Sheet1");
105 [ + - - ]: 1 : producer_->current_cell_ = new detail::cell_impl();
106 : 1 : producer_->current_cell_->parent_ = producer_->current_worksheet_;
107 : 1 : }
108 : :
109 : : } // namespace xlnt
|