start architecting streaming read/write api

This commit is contained in:
Thomas Fussell 2017-06-12 10:34:35 -04:00
parent 2c5d579a1b
commit b3c044948f
3 changed files with 280 additions and 0 deletions

View File

@ -0,0 +1,103 @@
// Copyright (c) 2014-2017 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <cstddef>
#include <iterator>
#include <xlnt/xlnt_config.hpp>
namespace xlnt {
/// <summary>
/// workbook is the container for all other parts of the document.
/// </summary>
class XLNT_API streaming_workbook_reader
{
public:
~streaming_workbook_reader();
/// <summary>
/// Closes currently open read stream. This will be called automatically
/// by the destructor if it hasn't already been called manually.
/// </summary>
close();
/// <summary>
/// Registers callback as the function to be called when a cell is read.
/// </summary>
void on_cell(std::function<void(cell)> callback);
/// <summary>
/// Registers callback as the function to be called when a worksheet is
/// opened for reading. The callback will be called with the title of the
/// worksheet.
/// </summary>
void on_worksheet_start(std::function<void(std::string)> callback);
/// <summary>
/// Registers callback as the function to be called when a worksheet is
/// finished being read. The callback will be called with the worksheet
/// object (which will not contain any cells). Cells should be handled
/// via registering a callback with on_cell.
/// </summary>
void on_worksheet_end(std::function<void(worksheet)> callback);
/// <summary>
/// Interprets byte vector data as an XLSX file and sets the content of this
/// workbook to match that file.
/// </summary>
void open(const std::vector<std::uint8_t> &data);
/// <summary>
/// Interprets file with the given filename as an XLSX file and sets
/// the content of this workbook to match that file.
/// </summary>
void open(const std::string &filename);
#ifdef _MSC_VER
/// <summary>
/// Interprets file with the given filename as an XLSX file and sets
/// the content of this workbook to match that file.
/// </summary>
void open(const std::wstring &filename);
#endif
/// <summary>
/// Interprets file with the given filename as an XLSX file and sets the
/// content of this workbook to match that file.
/// </summary>
void open(const xlnt::path &filename);
/// <summary>
/// Interprets data in stream as an XLSX file and sets the content of this
/// workbook to match that file.
/// </summary>
void open(std::istream &stream);
private:
std::unique_ptr<xlnt::detail::xlsx_consumer> consumer_;
};
} // namespace xlnt

View File

@ -0,0 +1,93 @@
// Copyright (c) 2014-2017 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
#include <cstddef>
#include <iterator>
#include <xlnt/xlnt_config.hpp>
namespace xlnt {
/// <summary>
/// workbook is the container for all other parts of the document.
/// </summary>
class XLNT_API streaming_workbook_writer
{
public:
~streaming_workbook_writer();
/// <summary>
/// Finishes writing of the remaining contents of the workbook and closes
/// currently open write stream. This will be called automatically by the
/// destructor if it hasn't already been called manually.
/// </summary>
void close();
/// <summary>
/// Writes a cell to the currently active worksheet at the position given by
/// ref and with the given value. ref should be to the right of or below
/// the previously written cell.
/// </summary>
cell add_cell(const cell_reference &ref);
/// <summary>
/// Ends writing of data to the current sheet and begins writing a new sheet
/// with the given title.
/// </summary>
worksheet add_sheet(const std::string &title);
/// <summary>
/// Serializes the workbook into an XLSX file and saves the bytes into
/// byte vector data.
/// </summary>
void open(std::vector<std::uint8_t> &data) const;
/// <summary>
/// Serializes the workbook into an XLSX file and saves the data into a file
/// named filename.
/// </summary>
void open(const std::string &filename) const;
#ifdef _MSC_VER
/// <summary>
/// Serializes the workbook into an XLSX file and saves the data into a file
/// named filename.
/// </summary>
void open(const std::wstring &filename) const;
#endif
/// <summary>
/// Serializes the workbook into an XLSX file and saves the data into a file
/// named filename.
/// </summary>
void open(const xlnt::path &filename) const;
/// <summary>
/// Serializes the workbook into an XLSX file and saves the data into stream.
/// </summary>
void open(std::ostream &stream) const;
}
} // namespace xlnt

View File

@ -0,0 +1,84 @@
// Copyright (c) 2014-2017 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/workbook/streaming_workbook_reader.hpp>
namespace xlnt {
streaming_workbook_reader::~streaming_workbook_reader()
{
close();
}
streaming_workbook_reader::close()
{
if (consumer_)
{
consumer_.reset(nullptr);
}
}
void streaming_workbook_reader::on_cell(std::function<void(cell)> callback)
{
consumer_->on_cell(callback);
}
void streaming_workbook_reader::on_worksheet_start(std::function<void(std::string)> callback)
{
consumer_->on_worksheet_start(callback);
}
void streaming_workbook_reader::on_worksheet_end(std::function<void(worksheet)> callback)
{
consumer_->on_worksheet_end(callback);
}
void streaming_workbook_reader::open(const std::vector<std::uint8_t> &data)
{
}
void streaming_workbook_reader::open(const std::string &filename)
{
}
#ifdef _MSC_VER
void streaming_workbook_reader::open(const std::wstring &filename)
{
}
#endif
void streaming_workbook_reader::open(const xlnt::path &filename)
{
}
void streaming_workbook_reader::open(std::istream &stream)
{
}
} // namespace xlnt