finish unicode filename implementation for #64, also fixes #78

This commit is contained in:
Thomas Fussell 2016-11-07 21:55:40 -05:00
parent ca692790fa
commit 75e0d9475b
7 changed files with 57 additions and 18 deletions

View File

@ -362,7 +362,7 @@ public:
void save(const std::string &filename, const std::string &password); void save(const std::string &filename, const std::string &password);
void save(const xlnt::path &filename, const std::string &password); void save(const xlnt::path &filename, const std::string &password);
void save(std::istream &stream, const std::string &password); void save(std::ostream &stream, const std::string &password);
void save(const std::vector<std::uint8_t> &data, const std::string &password); void save(const std::vector<std::uint8_t> &data, const std::string &password);
void load(const std::vector<std::uint8_t> &data); void load(const std::vector<std::uint8_t> &data);

View File

@ -1883,20 +1883,22 @@ void xlsx_consumer::read_worksheet(const std::string &rel_id)
auto min = static_cast<column_t::index_t>(std::stoull(parser().attribute("min"))); auto min = static_cast<column_t::index_t>(std::stoull(parser().attribute("min")));
auto max = static_cast<column_t::index_t>(std::stoull(parser().attribute("max"))); auto max = static_cast<column_t::index_t>(std::stoull(parser().attribute("max")));
auto width = std::stold(parser().attribute("width")); auto width = std::stold(parser().attribute("width"));
bool custom = parser().attribute("customWidth") == std::string("1"); auto column_style = parser().attribute_present("style")
auto column_style = static_cast<std::size_t>(parser().attribute_present("style") ? std::stoull(parser().attribute("style")) : 0); ? parser().attribute<std::size_t>("style") : static_cast<std::size_t>(0);
auto custom = parser().attribute_present("customWidth")
? is_true(parser().attribute("customWidth")) : false;
for (auto column = min; column <= max; column++) for (auto column = min; column <= max; column++)
{ {
if (!ws.has_column_properties(column)) column_properties props;
{
ws.add_column_properties(column, column_properties());
}
ws.get_column_properties(min).width = width; props.width = width;
ws.get_column_properties(min).style = column_style; props.style = column_style;
ws.get_column_properties(min).custom = custom; props.custom = custom;
ws.add_column_properties(column, props);
} }
if (parser().attribute_present("bestFit")) if (parser().attribute_present("bestFit"))

View File

@ -27,6 +27,7 @@
#include <detail/vector_streambuf.hpp> #include <detail/vector_streambuf.hpp>
#include <detail/xlsx_consumer.hpp> #include <detail/xlsx_consumer.hpp>
#include <detail/xlsx_producer.hpp>
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp> #include <xlnt/workbook/workbook.hpp>
@ -576,6 +577,16 @@ struct crypto_helper
return decrypt_xlsx_standard(encryption_info, password, encrypted_package); return decrypt_xlsx_standard(encryption_info, password, encrypted_package);
} }
static std::vector<std::uint8_t> encrypt_xlsx(const std::vector<std::uint8_t> &bytes, const std::string &password)
{
if (bytes.empty())
{
throw xlnt::exception("empty file");
}
return {};
}
}; };
void xlsx_consumer::read(std::istream &source, const std::string &password) void xlsx_consumer::read(std::istream &source, const std::string &password)
@ -588,5 +599,21 @@ void xlsx_consumer::read(std::istream &source, const std::string &password)
read(decrypted_stream); read(decrypted_stream);
} }
void xlsx_producer::write(std::ostream &destination, const std::string &password)
{
std::vector<std::uint8_t> decrypted;
{
vector_ostreambuf decrypted_buffer(decrypted);
std::ostream decrypted_stream(&decrypted_buffer);
write(decrypted_stream);
}
const auto encrypted = crypto_helper::encrypt_xlsx(decrypted, password);
vector_istreambuf encrypted_buffer(encrypted);
destination << &encrypted_buffer;
}
} // namespace detail } // namespace detail
} // namespace xlnt } // namespace xlnt

View File

@ -56,6 +56,8 @@ public:
void write(std::ostream &destination); void write(std::ostream &destination);
void write(std::ostream &destination, const std::string &password);
private: private:
/// <summary> /// <summary>
/// Write all files needed to create a valid XLSX file which represents all /// Write all files needed to create a valid XLSX file which represents all

View File

@ -70,10 +70,10 @@ public:
#ifdef WIN32 #ifdef WIN32
xlnt::workbook wb; xlnt::workbook wb;
wb.load(L"data\\19_unicode_Λ.xlsx"); wb.load(L"data\\19_unicode_Λ.xlsx");
#else
xlnt::workbook wb;
wb.load("data/19_unicode_Λ.xlsx");
#endif
TS_ASSERT_EQUALS(wb.get_active_sheet().get_cell("A1").get_value<std::string>(), "unicode!"); TS_ASSERT_EQUALS(wb.get_active_sheet().get_cell("A1").get_value<std::string>(), "unicode!");
#endif
xlnt::workbook wb2;
wb2.load(u8"data/19_unicode_Λ.xlsx");
TS_ASSERT_EQUALS(wb2.get_active_sheet().get_cell("A1").get_value<std::string>(), "unicode!");
} }
}; };

View File

@ -21,8 +21,10 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <codecvt>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <set> #include <set>
@ -62,7 +64,7 @@ namespace {
#ifdef WIN32 #ifdef WIN32
std::wstring utf8_to_utf16(const std::string &utf8_string) std::wstring utf8_to_utf16(const std::string &utf8_string)
{ {
std::wstring_convert<std::codecvt_utf8<wchar_t> convert; std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
return convert.from_bytes(utf8_string); return convert.from_bytes(utf8_string);
} }
@ -83,7 +85,7 @@ void open_stream(std::ifstream &stream, const std::string &path)
void open_stream(std::ofstream &stream, const std::string &path) void open_stream(std::ofstream &stream, const std::string &path)
{ {
stream.open(path, utf8_to_utf16(path)); open_stream(stream, utf8_to_utf16(path));
} }
#else #else
void open_stream(std::ifstream &stream, const std::string &path) void open_stream(std::ifstream &stream, const std::string &path)
@ -811,6 +813,12 @@ void workbook::save(std::ostream &stream) const
producer.write(stream); producer.write(stream);
} }
void workbook::save(std::ostream &stream, const std::string &password)
{
detail::xlsx_producer producer(*this);
producer.write(stream, password);
}
#ifdef WIN32 #ifdef WIN32
void workbook::save(const std::wstring &filename) void workbook::save(const std::wstring &filename)
{ {
@ -837,7 +845,7 @@ void workbook::load(const std::wstring &filename, const std::string &password)
{ {
std::ifstream file_stream; std::ifstream file_stream;
open_stream(file_stream, filename); open_stream(file_stream, filename);
load(file_stream); load(file_stream, password);
} }
#endif #endif

2
third-party/botan vendored

@ -1 +1 @@
Subproject commit 73c2605f50e6192bf6cb560c51d32bc53b4c5597 Subproject commit 523b2a4ca48fa5cf04ea371aabe7167ce2e5cd13