clean up switches, closes #133

This commit is contained in:
Thomas Fussell 2017-03-20 19:22:46 -04:00
parent e617d140f0
commit cf7f4e6c83
8 changed files with 721 additions and 681 deletions

View File

@ -94,11 +94,10 @@ std::string to_string(relationship_type t)
case relationship_type::image:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
case relationship_type::unknown:
#ifndef __clang__
default:
#endif
return "unknown";
}
default_case("unknown");
}
std::string to_string(pattern_fill_type fill_type)

View File

@ -23,10 +23,12 @@
#pragma once
#include <xlnt/utils/exceptions.hpp>
#define EXCEPT_ON_UNHANDLED_SWITCH_CASE
#ifdef EXCEPT_ON_UNHANDLED_SWITCH_CASE
#define default_case(default_value) throw xlnt::exception("unhandled case");
#define default_case(default_value) throw xlnt::unhandled_switch_case();
#else
#define default_case(default_value) return default_value;
#endif

File diff suppressed because it is too large Load Diff

View File

@ -245,9 +245,9 @@ std::array<xlnt::optional<xlnt::rich_text>, 3> parse_header_footer(const std::st
tokens.push_back(token);
}
const auto parse_section = [&tokens, &result](hf_code code) {
const auto parse_section = [&tokens, &result](hf_code code)
{
std::vector<hf_code> end_codes{hf_code::left_section, hf_code::center_section, hf_code::right_section};
end_codes.erase(std::find(end_codes.begin(), end_codes.end(), code));
std::size_t start_index = 0;
@ -296,141 +296,197 @@ std::array<xlnt::optional<xlnt::rich_text>, 3> parse_header_footer(const std::st
switch (current_token.code)
{
case hf_code::text:
break; // already handled above
{
break; // already handled above
}
case hf_code::left_section:
break; // used below
{
break; // used below
}
case hf_code::center_section:
break; // used below
{
break; // used below
}
case hf_code::right_section:
break; // used below
{
break; // used below
}
case hf_code::current_page_number:
current_run.first = current_run.first + "&P";
break;
{
current_run.first = current_run.first + "&P";
break;
}
case hf_code::total_page_number:
current_run.first = current_run.first + "&N";
break;
{
current_run.first = current_run.first + "&N";
break;
}
case hf_code::font_size:
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
}
current_run.second.get().size(std::stod(current_token.value));
break;
case hf_code::text_font_color:
if (current_token.value.size() == 6)
{
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
}
current_run.second.get().color(xlnt::rgb_color(current_token.value));
current_run.second.get().size(std::stod(current_token.value));
break;
}
break;
case hf_code::text_font_color:
{
if (current_token.value.size() == 6)
{
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
}
current_run.second.get().color(xlnt::rgb_color(current_token.value));
}
break;
}
case hf_code::text_strikethrough:
break;
{
break;
}
case hf_code::text_superscript:
break;
{
break;
}
case hf_code::text_subscript:
break;
{
break;
}
case hf_code::date:
current_run.first = current_run.first + "&D";
break;
{
current_run.first = current_run.first + "&D";
break;
}
case hf_code::time:
current_run.first = current_run.first + "&T";
break;
{
current_run.first = current_run.first + "&T";
break;
}
case hf_code::picture_as_background:
current_run.first = current_run.first + "&G";
break;
{
current_run.first = current_run.first + "&G";
break;
}
case hf_code::text_single_underline:
break;
{
break;
}
case hf_code::text_double_underline:
break;
{
break;
}
case hf_code::workbook_file_path:
current_run.first = current_run.first + "&Z";
break;
{
current_run.first = current_run.first + "&Z";
break;
}
case hf_code::workbook_file_name:
current_run.first = current_run.first + "&F";
break;
{
current_run.first = current_run.first + "&F";
break;
}
case hf_code::sheet_tab_name:
current_run.first = current_run.first + "&A";
break;
{
current_run.first = current_run.first + "&A";
break;
}
case hf_code::add_to_page_number:
break;
{
break;
}
case hf_code::subtract_from_page_number:
break;
{
break;
}
case hf_code::text_font_name:
{
auto comma_index = current_token.value.find(',');
auto font_name = current_token.value.substr(0, comma_index);
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
}
auto comma_index = current_token.value.find(',');
auto font_name = current_token.value.substr(0, comma_index);
if (font_name != "-")
{
current_run.second.get().name(font_name);
}
if (comma_index != std::string::npos)
{
auto font_type = current_token.value.substr(comma_index + 1);
if (font_type == "Bold")
if (!current_run.second.is_set())
{
current_run.second.get().bold(true);
current_run.second = xlnt::font();
}
else if (font_type == "Italic")
{
}
else if (font_type == "BoldItalic")
{
current_run.second.get().bold(true);
}
}
}
break;
if (font_name != "-")
{
current_run.second.get().name(font_name);
}
if (comma_index != std::string::npos)
{
auto font_type = current_token.value.substr(comma_index + 1);
if (font_type == "Bold")
{
current_run.second.get().bold(true);
}
else if (font_type == "Italic")
{
// TODO
}
else if (font_type == "BoldItalic")
{
current_run.second.get().bold(true);
}
}
break;
}
case hf_code::bold_font_style:
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
if (!current_run.second.is_set())
{
current_run.second = xlnt::font();
}
current_run.second.get().bold(true);
break;
}
current_run.second.get().bold(true);
break;
case hf_code::italic_font_style:
break;
{
break;
}
case hf_code::outline_style:
break;
{
break;
}
case hf_code::shadow_style:
break;
{
break;
}
}
}

View File

@ -64,6 +64,7 @@ struct value_traits<xlnt::detail::hash_algorithm>
return xlnt::detail::hash_algorithm::ripemd160;
else if (hash_algorithm_string == "Whirlpool")
return xlnt::detail::hash_algorithm::whirlpool;
default_case(xlnt::detail::hash_algorithm::sha1);
}
@ -160,8 +161,9 @@ std::vector<std::uint8_t> crypto_helper::decode_base64(const std::string &encode
decoder.Put(reinterpret_cast<const std::uint8_t *>(encoded.data()), encoded.size());
decoder.MessageEnd();
std::vector<std::uint8_t> decoded(decoder.MaxRetrievable(), 0);
decoder.Get(decoded.data(), decoded.size());
const auto bytes_decoded = std::size_t(decoder.MaxRetrievable());
auto decoded = std::vector<std::uint8_t>(bytes_decoded, 0);
decoder.Get(decoded.data(), bytes_decoded);
return decoded;
}
@ -172,8 +174,9 @@ std::string crypto_helper::encode_base64(const std::vector<std::uint8_t> &decode
encoder.Put(reinterpret_cast<const std::uint8_t *>(decoded.data()), decoded.size());
encoder.MessageEnd();
std::vector<std::uint8_t> encoded(encoder.MaxRetrievable(), 0);
encoder.Get(encoded.data(), encoded.size());
const auto bytes_encoded = std::size_t(encoder.MaxRetrievable());
auto encoded = std::vector<std::uint8_t>(bytes_encoded, 0);
encoder.Get(encoded.data(), bytes_encoded);
return std::string(encoded.begin(), encoded.end());
}

View File

@ -193,7 +193,8 @@ void xlsx_producer::write_content_types()
write_end_element(xmlns, "Types");
}
void xlsx_producer::write_property(const std::string &name, const variant &value, const std::string &ns, bool custom, std::size_t pid)
void xlsx_producer::write_property(const std::string &name, const variant &value,
const std::string &ns, bool custom, std::size_t pid)
{
if (custom)
{
@ -208,115 +209,126 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
switch (value.value_type())
{
case variant::type::null:
break;
{
break;
}
case variant::type::boolean:
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "bool");
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "bool");
}
write_characters(value.get<bool>() ? "true" : "false");
if (custom)
{
write_end_element(constants::ns("vt"), "bool");
}
break;
}
write_characters(value.get<bool>() ? "true" : "false");
if (custom)
{
write_end_element(constants::ns("vt"), "bool");
}
break;
case variant::type::i4:
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "i4");
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "i4");
}
write_characters(value.get<std::int32_t>());
if (custom)
{
write_end_element(constants::ns("vt"), "i4");
}
break;
}
write_characters(value.get<std::int32_t>());
if (custom)
{
write_end_element(constants::ns("vt"), "i4");
}
break;
case variant::type::lpstr:
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "lpwstr");
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
write_attribute("pid", pid);
write_start_element(constants::ns("vt"), "lpwstr");
}
if (!custom && ns == constants::ns("dcterms") && (name == "created" || name == "modified"))
{
write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF");
}
write_characters(value.get<std::string>());
if (custom)
{
write_end_element(constants::ns("vt"), "lpwstr");
}
break;
}
if (!custom && ns == constants::ns("dcterms") && (name == "created" || name == "modified"))
{
write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF");
}
write_characters(value.get<std::string>());
if (custom)
{
write_end_element(constants::ns("vt"), "lpwstr");
}
break;
case variant::type::date:
write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF");
write_characters(value.get<datetime>().to_iso_string());
break;
{
write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF");
write_characters(value.get<datetime>().to_iso_string());
break;
}
case variant::type::vector:
{
write_start_element(constants::ns("vt"), "vector");
auto vector = value.get<std::vector<variant>>();
std::unordered_set<variant::type> types;
for (const auto &element : vector)
{
types.insert(element.value_type());
write_start_element(constants::ns("vt"), "vector");
auto vector = value.get<std::vector<variant>>();
std::unordered_set<variant::type> types;
for (const auto &element : vector)
{
types.insert(element.value_type());
}
const auto is_mixed = types.size() > 1;
const auto vector_type = !is_mixed ? to_string(*types.begin()) : "variant";
write_attribute("size", vector.size());
write_attribute("baseType", vector_type);
for (std::size_t i = 0; i < vector.size(); ++i)
{
const auto &vector_element = vector.at(i);
if (is_mixed)
{
write_start_element(constants::ns("vt"), "variant");
}
if (vector_element.value_type() == variant::type::lpstr)
{
write_element(constants::ns("vt"), "lpstr", vector_element.get<std::string>());
}
else if (vector_element.value_type() == variant::type::i4)
{
write_element(constants::ns("vt"), "i4", vector_element.get<std::int32_t>());
}
if (is_mixed)
{
write_end_element(constants::ns("vt"), "variant");
}
}
write_end_element(constants::ns("vt"), "vector");
break;
}
const auto is_mixed = types.size() > 1;
const auto vector_type = !is_mixed ? to_string(*types.begin()) : "variant";
write_attribute("size", vector.size());
write_attribute("baseType", vector_type);
for (std::size_t i = 0; i < vector.size(); ++i)
{
const auto &vector_element = vector.at(i);
if (is_mixed)
{
write_start_element(constants::ns("vt"), "variant");
}
if (vector_element.value_type() == variant::type::lpstr)
{
write_element(constants::ns("vt"), "lpstr", vector_element.get<std::string>());
}
else if (vector_element.value_type() == variant::type::i4)
{
write_element(constants::ns("vt"), "i4", vector_element.get<std::int32_t>());
}
if (is_mixed)
{
write_end_element(constants::ns("vt"), "variant");
}
}
write_end_element(constants::ns("vt"), "vector");
}
break;
}
if (custom)
@ -2589,6 +2601,7 @@ void xlsx_producer::write_worksheet(const relationship &rel)
path archive_path(worksheet_part.parent().append(child_rel.target().path()));
auto split_part_path = archive_path.split();
auto part_path_iter = split_part_path.begin();
while (part_path_iter != split_part_path.end())
{
if (*part_path_iter == "..")
@ -2599,85 +2612,19 @@ void xlsx_producer::write_worksheet(const relationship &rel)
++part_path_iter;
}
archive_path = std::accumulate(split_part_path.begin(), split_part_path.end(), path(""),
[](const path &a, const std::string &b) { return a.append(b); });
begin_part(archive_path);
switch (child_rel.type())
if (child_rel.type() == relationship_type::comments)
{
case relationship_type::comments:
write_comments(child_rel, ws, cells_with_comments);
break;
case relationship_type::vml_drawing:
}
else if (child_rel.type() == relationship_type::vml_drawing)
{
write_vml_drawings(child_rel, ws, cells_with_comments);
break;
case relationship_type::office_document:
break;
case relationship_type::thumbnail:
break;
case relationship_type::calculation_chain:
break;
case relationship_type::extended_properties:
break;
case relationship_type::core_properties:
break;
case relationship_type::worksheet:
break;
case relationship_type::shared_string_table:
break;
case relationship_type::stylesheet:
break;
case relationship_type::theme:
break;
case relationship_type::hyperlink:
break;
case relationship_type::chartsheet:
break;
case relationship_type::unknown:
break;
case relationship_type::custom_properties:
break;
case relationship_type::printer_settings:
break;
case relationship_type::connections:
break;
case relationship_type::custom_property:
break;
case relationship_type::custom_xml_mappings:
break;
case relationship_type::dialogsheet:
break;
case relationship_type::drawings:
break;
case relationship_type::external_workbook_references:
break;
case relationship_type::pivot_table:
break;
case relationship_type::pivot_table_cache_definition:
break;
case relationship_type::pivot_table_cache_records:
break;
case relationship_type::query_table:
break;
case relationship_type::shared_workbook_revision_headers:
break;
case relationship_type::shared_workbook:
break;
case relationship_type::revision_log:
break;
case relationship_type::shared_workbook_user_data:
break;
case relationship_type::single_cell_table_definitions:
break;
case relationship_type::table_definition:
break;
case relationship_type::volatile_dependencies:
break;
case relationship_type::image:
break;
}
}
}

View File

@ -266,14 +266,10 @@ public:
strm.next_in = reinterpret_cast<Bytef *>(in.data());
}
int ret = inflate(&strm, Z_NO_FLUSH); // decompress
const auto ret = inflate(&strm, Z_NO_FLUSH); // decompress
switch (ret)
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
{
case Z_STREAM_ERROR:
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
throw xlnt::exception("couldn't inflate ZIP, possibly corrupted");
}

View File

@ -233,7 +233,7 @@ std::string content_type(xlnt::relationship_type type)
case relationship_type::custom_properties:
return "application/vnd.openxmlformats-officedocument.custom-properties+xml";
case relationship_type::custom_property:
return "";
throw xlnt::unhandled_switch_case();
case relationship_type::custom_xml_mappings:
return "application/xml";
case relationship_type::dialogsheet:
@ -245,9 +245,9 @@ std::string content_type(xlnt::relationship_type type)
case relationship_type::external_workbook_references:
return "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml";
case relationship_type::hyperlink:
return "";
throw xlnt::unhandled_switch_case();
case relationship_type::image:
return "";
throw xlnt::unhandled_switch_case();
case relationship_type::office_document:
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
case relationship_type::pivot_table:
@ -265,7 +265,7 @@ std::string content_type(xlnt::relationship_type type)
case relationship_type::shared_string_table:
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml";
case relationship_type::shared_workbook:
return "";
throw xlnt::unhandled_switch_case();
case relationship_type::shared_workbook_revision_headers:
return "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml";
case relationship_type::shared_workbook_user_data: