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

View File

@ -132,12 +132,15 @@ void number_format_parser::parse()
switch (token.type)
{
case number_format_token::token_type::end_section:
{
codes_.push_back(section);
section = format_code();
break;
}
case number_format_token::token_type::color:
{
if (section.has_color || section.has_condition || section.has_locale || !section.parts.empty())
{
throw std::runtime_error("color should be the first part of a format");
@ -145,7 +148,9 @@ void number_format_parser::parse()
section.has_color = true;
section.color = color_from_string(token.string);
break;
}
case number_format_token::token_type::locale:
{
@ -221,36 +226,47 @@ void number_format_parser::parse()
}
case number_format_token::token_type::text:
{
part.type = template_part::template_type::text;
part.string = token.string;
section.parts.push_back(part);
part = template_part();
break;
}
case number_format_token::token_type::fill:
{
part.type = template_part::template_type::fill;
part.string = token.string;
section.parts.push_back(part);
part = template_part();
break;
}
case number_format_token::token_type::space:
{
part.type = template_part::template_type::space;
part.string = token.string;
section.parts.push_back(part);
part = template_part();
break;
}
case number_format_token::token_type::number:
{
part.type = template_part::template_type::general;
part.placeholders = parse_placeholders(token.string);
section.parts.push_back(part);
part = template_part();
break;
}
case number_format_token::token_type::datetime:
{
section.is_datetime = true;
switch (token.string.front())
@ -403,13 +419,16 @@ void number_format_parser::parse()
part = template_part();
break;
}
case number_format_token::token_type::end:
{
codes_.push_back(section);
finalize();
return;
}
}
token = parse_next_token();
}
@ -1388,17 +1407,25 @@ std::string number_formatter::format_number(const format_code &format, long doub
switch (part.type)
{
case template_part::template_type::space:
{
result.push_back(' ');
break;
}
case template_part::template_type::text:
{
result.append(part.string);
break;
}
case template_part::template_type::fill:
{
fill = true;
fill_index = result.size();
fill_character = part.string;
break;
}
case template_part::template_type::general:
{
if (part.placeholders.type == format_placeholders::placeholders_type::fractional_part
@ -1447,10 +1474,15 @@ std::string number_formatter::format_number(const format_code &format, long doub
break;
}
case template_part::template_type::day_number:
{
result.append(std::to_string(dt.day));
break;
}
case template_part::template_type::day_number_leading_zero:
{
if (dt.day < 10)
{
result.push_back('0');
@ -1458,16 +1490,28 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.day));
break;
}
case template_part::template_type::month_abbreviation:
{
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1).substr(0, 3));
break;
}
case template_part::template_type::month_name:
{
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1));
break;
}
case template_part::template_type::month_number:
{
result.append(std::to_string(dt.month));
break;
}
case template_part::template_type::month_number_leading_zero:
{
if (dt.month < 10)
{
result.push_back('0');
@ -1475,7 +1519,10 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.month));
break;
}
case template_part::template_type::year_short:
{
if (dt.year % 1000 < 10)
{
result.push_back('0');
@ -1483,13 +1530,22 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.year % 1000));
break;
}
case template_part::template_type::year_long:
{
result.append(std::to_string(dt.year));
break;
}
case template_part::template_type::hour:
{
result.append(std::to_string(hour));
break;
}
case template_part::template_type::hour_leading_zero:
{
if (hour < 10)
{
result.push_back('0');
@ -1497,10 +1553,16 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(hour));
break;
}
case template_part::template_type::minute:
{
result.append(std::to_string(dt.minute));
break;
}
case template_part::template_type::minute_leading_zero:
{
if (dt.minute < 10)
{
result.push_back('0');
@ -1508,13 +1570,22 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.minute));
break;
}
case template_part::template_type::second:
{
result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0)));
break;
}
case template_part::template_type::second_fractional:
{
result.append(std::to_string(dt.second));
break;
}
case template_part::template_type::second_leading_zero:
{
if ((dt.second + (dt.microsecond > 500000 ? 1 : 0)) < 10)
{
result.push_back('0');
@ -1522,7 +1593,10 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0)));
break;
}
case template_part::template_type::second_leading_zero_fractional:
{
if (dt.second < 10)
{
result.push_back('0');
@ -1530,7 +1604,10 @@ std::string number_formatter::format_number(const format_code &format, long doub
result.append(std::to_string(dt.second));
break;
}
case template_part::template_type::am_pm:
{
if (dt.hour < 12)
{
result.append("AM");
@ -1541,8 +1618,10 @@ std::string number_formatter::format_number(const format_code &format, long doub
}
break;
}
case template_part::template_type::a_p:
{
if (dt.hour < 12)
{
result.append("A");
@ -1553,33 +1632,47 @@ std::string number_formatter::format_number(const format_code &format, long doub
}
break;
}
case template_part::template_type::elapsed_hours:
{
result.append(std::to_string(24 * static_cast<int>(number) + dt.hour));
break;
}
case template_part::template_type::elapsed_minutes:
result.append(std::to_string(24 * 60 * static_cast<int>(number) + (60 * dt.hour) + dt.minute));
{
result.append(std::to_string(24 * 60 * static_cast<int>(number)
+ (60 * dt.hour) + dt.minute));
break;
}
case template_part::template_type::elapsed_seconds:
result.append(std::to_string(
24 * 60 * 60 * static_cast<int>(number) + (60 * 60 * dt.hour) + (60 * dt.minute) + dt.second));
{
result.append(std::to_string(24 * 60 * 60 * static_cast<int>(number)
+ (60 * 60 * dt.hour) + (60 * dt.minute) + dt.second));
break;
}
case template_part::template_type::month_letter:
{
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1).substr(0, 1));
break;
}
case template_part::template_type::day_abbreviation:
{
result.append(day_names->at(static_cast<std::size_t>(dt.weekday()) - 1).substr(0, 3));
break;
}
case template_part::template_type::day_name:
{
result.append(day_names->at(static_cast<std::size_t>(dt.weekday()) - 1));
break;
}
}
}
const std::size_t width = 11;
@ -1603,75 +1696,19 @@ std::string number_formatter::format_text(const format_code &format, const std::
for (const auto &part : format.parts)
{
switch (part.type)
if (part.type == template_part::template_type::text)
{
case template_part::template_type::text:
result.append(part.string);
any_text_part = true;
break;
case template_part::template_type::general:
}
else if (part.type == template_part::template_type::general)
{
if (part.placeholders.type == format_placeholders::placeholders_type::general
|| part.placeholders.type == format_placeholders::placeholders_type::text)
{
result.append(text);
any_text_part = true;
}
break;
case template_part::template_type::fill:
break;
case template_part::template_type::space:
break;
case template_part::template_type::month_number:
break;
case template_part::template_type::month_number_leading_zero:
break;
case template_part::template_type::month_abbreviation:
break;
case template_part::template_type::month_name:
break;
case template_part::template_type::month_letter:
break;
case template_part::template_type::day_number:
break;
case template_part::template_type::day_number_leading_zero:
break;
case template_part::template_type::day_abbreviation:
break;
case template_part::template_type::day_name:
break;
case template_part::template_type::year_short:
break;
case template_part::template_type::year_long:
break;
case template_part::template_type::hour:
break;
case template_part::template_type::hour_leading_zero:
break;
case template_part::template_type::minute:
break;
case template_part::template_type::minute_leading_zero:
break;
case template_part::template_type::second:
break;
case template_part::template_type::second_fractional:
break;
case template_part::template_type::second_leading_zero:
break;
case template_part::template_type::second_leading_zero_fractional:
break;
case template_part::template_type::am_pm:
break;
case template_part::template_type::a_p:
break;
case template_part::template_type::elapsed_hours:
break;
case template_part::template_type::elapsed_minutes:
break;
case template_part::template_type::elapsed_seconds:
break;
}
}

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,89 +296,134 @@ 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
}
case hf_code::left_section:
{
break; // used below
}
case hf_code::center_section:
{
break; // used below
}
case hf_code::right_section:
{
break; // used below
}
case hf_code::current_page_number:
{
current_run.first = current_run.first + "&P";
break;
}
case hf_code::total_page_number:
{
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));
}
break;
}
case hf_code::text_strikethrough:
{
break;
}
case hf_code::text_superscript:
{
break;
}
case hf_code::text_subscript:
{
break;
}
case hf_code::date:
{
current_run.first = current_run.first + "&D";
break;
}
case hf_code::time:
{
current_run.first = current_run.first + "&T";
break;
}
case hf_code::picture_as_background:
{
current_run.first = current_run.first + "&G";
break;
}
case hf_code::text_single_underline:
{
break;
}
case hf_code::text_double_underline:
{
break;
}
case hf_code::workbook_file_path:
{
current_run.first = current_run.first + "&Z";
break;
}
case hf_code::workbook_file_name:
{
current_run.first = current_run.first + "&F";
break;
}
case hf_code::sheet_tab_name:
{
current_run.first = current_run.first + "&A";
break;
}
case hf_code::add_to_page_number:
{
break;
}
case hf_code::subtract_from_page_number:
{
break;
}
case hf_code::text_font_name:
{
@ -405,34 +450,45 @@ std::array<xlnt::optional<xlnt::rich_text>, 3> parse_header_footer(const std::st
}
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();
}
current_run.second.get().bold(true);
break;
}
case hf_code::italic_font_style:
{
break;
}
case hf_code::outline_style:
{
break;
}
case hf_code::shadow_style:
{
break;
}
}
}
if (!current_run.first.empty())
{

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,9 +209,12 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
switch (value.value_type())
{
case variant::type::null:
{
break;
}
case variant::type::boolean:
{
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
@ -226,8 +230,10 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
}
break;
}
case variant::type::i4:
{
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
@ -243,8 +249,10 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
}
break;
}
case variant::type::lpstr:
{
if (custom)
{
write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
@ -265,11 +273,15 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
}
break;
}
case variant::type::date:
{
write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF");
write_characters(value.get<datetime>().to_iso_string());
break;
}
case variant::type::vector:
{
@ -314,10 +326,10 @@ void xlsx_producer::write_property(const std::string &name, const variant &value
}
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: